- Allow HTTP (Port 80), SSH (Port 22), HTTPS (Port 443) inbound traffic of your server.
- Save your server connection files in a secure location and connect through the SSH port of your server.
- Windows users follow this guide to connect using the Putty client.
- Once logged in run these commands to update the server ubuntu os in to the latest stable version.
sudo apt-get update
- Install following packages to the server.
sudo apt install python3-pip python3-dev python3-venv libpq-dev postgresql postgresql-contrib nginx gunicorn curl
- Then enable firewall for nginx and openssh(Optional).
sudo ufw enable
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
- Install Github CLI
curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
sudo apt update
sudo apt install gh
- Then upgrade CLI
sudo apt update
sudo apt install gh
- Add your Github to server(When generating tokens add a token with a limited amount of time).
gh auth login
- Finally clone your repository
git clone <YOUR-PROJECT-GIT>
- Open the psql terminal.
sudo -u postgres psql
- Execute following commands one at a time to create a db and a user.
CREATE DATABASE dbname;
CREATE USER user WITH ENCRYPTED PASSWORD 'pass';
GRANT ALL PRIVILEGES ON DATABASE dbname TO user;
- Open the cloned the repository and execute following commandsto install venv.
cd <YOUR-PROJECT-NAME>
python3 -m venv env
source env/bin/activate
pip install wheel gunicorn
pip install -r requirements.txt
- Add the .env file content to the server environment(Keep no spaces in variable value assigning) and restart shell.
sudo nano /etc/environment
- Then do the migrations for django.
python manage.py makemigrations
python manage.py migrate
python manage.py collectstatic
- Create a superuser to django.
python manage.py createsuperuser
- Create a socket file for gunicorn.
sudo nano /etc/systemd/system/gunicorn.socket
- Add the content of the gunicorn.socket file to the editor window.
- Next configure the gunicorn.service file.
sudo nano /etc/systemd/system/gunicorn.service
- Add the content of the gunicorn.service file to the editor window with replacing necessary content.
- Run following to make gunicorn service start and enabled.
sudo systemctl start gunicorn.socket
sudo systemctl enable gunicorn.socket
sudo systemctl status gunicorn.socket
- Create a seperate file for the project
sudo nano /etc/nginx/sites-available/<YOUR-PROJECT-NAME>
- Add the content of nginx file to the editor window after replacing the necessary content.
- Once your NGINX config is set up, make sure there are no syntax errors.
sudo nginx -t
- Next, create a soft link of your config file from sites-available to the sites-enabled directory.
sudo ln -s /etc/nginx/sites-available/<YOUR-PROJECT-NAME> /etc/nginx/sites-enabled
- Run following to make gunicorn service start and enabled.
sudo systemctl restart nginx
- Copy the deployment file(if available) to the root directory with other required files.
cp <YOUR-PROJECT-NAME>/deploy.sh .
- Then excute deployment.
/bin/sh deploy.sh