- copy and paste ssh-keygen on your local machine
- ssh root@your_server_ip
- adduser sammy
- usermod -aG sudo sammy - setup root privileges for the user
- ufw app list - setup a basic filewall
- ufw allow OpenSSH - setup a basic filewall
- ufw enable - setup a basic filewall
- ufw status
- rsync --archive --chown=sammy:sammy ~/.ssh /home/sammy
- sudo apt update
- sudo apt install python3-pip python3-dev libpq-dev postgresql postgresql-contrib nginx curl
- sudo apt update
- sudo apt install python-pip python-dev libpq-dev postgresql postgresql-contrib nginx curl
- sudo -u postgres psql
- CREATE USER db_user WITH PASSWORD 'userpassword';
- CREATE DATABASE db_name OWNER db_user;
- ALTER ROLE db_user SET client_encoding TO 'utf8';
- ALTER ROLE db_user SET default_transaction_isolation TO 'read committed';
- ALTER ROLE db_user SET timezone TO 'UTC';
- GRANT ALL PRIVILEGES ON DATABASE icoach18th TO icoachdbuser;
- sudo -H pip3 install --upgrade pip
- sudo -H pip3 install virtualenv
- mkdir ~/myprojectdir
- cd ~/myprojectdir
- within the project folder run
- virtualenv myprojectenv
- source myprojectenv/bin/activate - activate
- pip install django gunicorn psycopg2-binary
- sudo ufw allow 8000
- python manage.py makemigrations
- python manage.py migrate
- python manage.py runserver 0.0.0.0:8000
- gunicorn --bind 0.0.0.0:8000 icoach.wsgi
- sudo nano /etc/systemd/system/gunicorn.socket
- copy and paste this code on the empty file
Description=gunicorn socket [Socket] ListenStream=/run/gunicorn.sock [Install] WantedBy=sockets.target
- ctrl +x to close and save
- sudo nano /etc/systemd/system/gunicorn.service
- copy and paste this code
[Unit] Description=gunicorn daemon Requires=gunicorn.socket After=network.target [Service] User=sammy Group=www-data WorkingDirectory=/home/sammy/myprojectdir ExecStart=/home/sammy/myprojectdir/myprojectenv/bin/gunicorn \ --access-logfile - \ --workers 3 \ --bind unix:/run/gunicorn.sock \ myproject.wsgi:application [Install] WantedBy=multi-user.target
- sudo systemctl start gunicorn.socket
- sudo systemctl enable gunicorn.socket
- sudo systemctl status gunicorn.socket
- file /run/gunicorn.sock
- sudo journalctl -u gunicorn.socket
- sudo systemctl status gunicorn
- curl --unix-socket /run/gunicorn.sock localhost
- sudo systemctl status gunicorn
- sudo systemctl status gunicorn
- sudo systemctl daemon-reload
- sudo systemctl restart gunicorn
-
sudo nano /etc/nginx/sites-available/myproject
listen 80; server_name server_domain_or_IP; location = /favicon.ico { access_log off; log_not_found off; } location /static/ { root /home/sammy/myprojectdir; } location / { include proxy_params; proxy_pass http://unix:/run/gunicorn.sock; } }
-
sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled
-
sudo nginx -t
-
sudo systemctl restart nginx
-
sudo ufw delete allow 8000
-
sudo ufw allow 'Nginx Full'
-
reference
-
https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-20-04