- Ubuntu Server 18.04/16.04
- DNS
Run commands
sudo apt-get update
sudo apt-get install python3-pip python3-dev nginx
sudo apt-get install libpq-dev
sudo apt-get install libssl-dev
- Run Command
sudo apt-get install postgresql
- Run command
sudo su postgres
. Switches to postgres user - Run command
psql
in postgres user shell - Run command
CREATE USER <username> WITH SUPERUSER PASSWORD '<password>'
. Enter user username and password fot the DB. For ex:CREATE USER agmt WITH SUPERUSER PASSWORD 'pass&14'
- Open New Terminal and run command
createdb <db_name>
- Paste follwing with the credentials to the
bashrc
file.export AGMT_SENDINBLUE_KEY="<send_in_blue_key>" export AGMT_HS256_SECRET="<jwt_token_key>" export AGMT_POSTGRES_HOST="localhost" export AGMT_POSTGRES_PORT="5432" export AGMT_POSTGRES_USER="<db_user>" export AGMT_POSTGRES_PASSWORD="<db_password>" export AGMT_POSTGRES_DATABASE="<db_name>"
- Run command
sudo pip3 install virtualenv
- Run Command
virtualenv myprojectenv
. You can enter your custom name instead ofmyprojectenv
.
- Run Command
source myprojectenv/bin/activate
. Activate the virtual environment before installing dependencies.
- Clone project repo to server
- Navigate to project directory containing the
requirements.txt
file. - Install Python dependencies by running command
pip3 install -r requirements.txt
- Navigate to project directory containing the
db.sql
file. (Insideagmt
folder) - Run Command
psql -d <db_name> -f ./db.sql
- Run Command
gunicorn main:app
inside the project folder containing themain.py
file. - If the gunicorn server has started successfully, close and set up Nginx and Gunicorn WSGI.
- Assuming the Server user Name is
amt
, python virtual environment name isvenv3
, the project folder name isvachan-api
and themain.py
file is invachan-api/agmt/
folder then the config files will be like: - Save config files in project directory named
vachanconfig
- Copy and edit the files according to the project credentials and directories and save file as
gunicorn.service
. You could use your custom name for the file.[Unit] Description=gunicorn daemon After=network.target [Service] User=amt Group=amt WorkingDirectory=/home/amt/vachan-api/agmt Environment="AGMT_SENDINBLUE_KEY=<send_in_blue_key>" Environment="AGMT_HS256_SECRET=<jwt_algorithm_key>" Environment="AGMT_POSTGRES_USER=<db_user_name>" Environment="AGMT_POSTGRES_PASSWORD=<db_password>" Environment="AGMT_POSTGRES_DATABASE=<db_name>" Environment="AGMT_HOST_API_URL=<api_url>" Environment="AGMT_HOST_UI_URL=<UI_url>" ExecStart=/home/amt/venv3/bin/gunicorn --workers 3 --bind unix:/home/amt/vachan-api/agmt/agmt.sock main:app --timeout=18000 [Install] WantedBy=multi-user.target
- Run Command to create sym link of the
gunicorn.service
file insystemd
foldersudo ln -s /home/amt/vachan-api/vachnaconfig/gunicorn.service /etc/systemd/system/
- Run the commands
sudo systemctl start gunicorn.service
sudo systemctl enable gunicorn.service
-
Copy and edit the files according to the project credentials and directories and save file as
nginx.conf
. You could use your custom name for the file. -
For enabling ssl refer online documentation.
server { listen 80; server_name <server_domain_name>; location / { include proxy_params; proxy_pass http://unix:/home/amt/vachan-api/agmt/agmt.sock; proxy_read_timeout 18000; proxy_connect_timeout 18000; proxy_send_timeout 18000; send_timeout 18000; } }
nginx.conf
file has to be linked first tosites-available
and fromsites-available
tosites-enabled
.- Run the commands
sudo ln -s /home/amt/vachan-api/vachnaconfig/nginx.conf /etc/nginx/sites-available/
sudo ln -s /etc/nginx/sites-available/nginx.conf /etc/nginx/sites-enabled
- Run command
sudo nginx -t
to check for syntax errors in thenginx.conf
file.
- Run Command
sudo systemctl restart nginx