Skip to content

This is a clean FastAPI project template, with instructions for setting up a debian server. Template includes systemd, nginx and gunicorn configs.

Notifications You must be signed in to change notification settings

neamaddin/debian-fastapi-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Content

Debian server set up for FastAPI

In this guide we will set up Debian server for FastAPI project. We will install all necessary packages, build Python3.11.3 (current version at time of repository creation) from sources, and run install.sh script to configure nginx and gunicorn daemon under systemd.

Install needed packages

Connect to Debian server and update repositories and install some initial needed packages:

sudo apt-get update
sudo apt-get install -y git nginx

Create user www (Situationally)

if when creating server you specified a login other than www or you didn't have opportunity to configure a user, then you need to create a user www.
Create user www, add www to sudoers and login as www:

sudo adduser www
sudo usermod -aG sudo www
su - www

Setup SSH for ssh-key authentication (Optional)

Using ssh keys is more secure than using a password for authentication.
If you don't have ssh keys, you need to generate them with following command (command must be executed locally).

ssh-keygen

To install keys on a remote server, use following command (command must be executed locally).

ssh-copy-id www@server_ip_address

Configure SSH in file /etc/ssh/sshd_config with settings below:

  • AllowUsers www
  • PermitRootLogin no
  • PasswordAuthentication no

Restart SSH server to update settings:

sudo service ssh restart

Install Python3.11

Build from source Python3.11, install with prefix to ~/.python folder:

sudo apt install -y build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev
wget https://www.python.org/ftp/python/3.11.3/Python-3.11.3.tgz
tar xvf Python-3.11.*
cd Python-3.11.3
mkdir ~/.python
./configure --enable-optimizations --prefix=/home/www/.python
make -j8
sudo make altinstall
sudo ln -s /home/www/.python/bin/python3.11 /bin/python3.11

Note: After installation, Python source folders can be deleted, they are no longer needed.

Pull Git project and run server

To quickly start server without editing configurations, you need download this repository and place FastAPI project files in src folder, while application file must be called main.py and application must be called app!!!
You also need append all dependencies in requirements.txt file.

git pull your_repository

Before running installation script, you need to verify python installation by running python command.

To start server, you need to run install.sh script and specify domain. For example:

$ ./install.sh
Your domain: my-domain.com

Hints

Gunicorn logs contains in gunicorn/access.log and gunicorn/error.log
Nginx logs contains in nginx/access.log and /nginx/error.log

For check status of gunicorn daemon, run:

sudo systemctl status gunicorn

For test new nginx config after change, run:

sudo nginx -t

When changing systemd or nginx configuration, you need to restart unit using restart.sh script:

./restart.sh

About

This is a clean FastAPI project template, with instructions for setting up a debian server. Template includes systemd, nginx and gunicorn configs.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published