1 . Server Update
sudo apt-get update
sudo apt-get upgrade -y
2 . Install venv
sudo apt-get install python3-venv -y
3 . Clone the project and navigate to the project folder
4 . Create vertual env and activate it
python3 -m venv venv
source venv/bin/activate
5 . sudo apt install gunicorn
6 . sudo apt-get install -y nginx
[Note: If you want to install dlib/face-recognition library do not use vertual env. Workon system installed python3 and install them when your are at the root path otherwise you will get errors. If you want to deploy your app on AWS EC2 instance with these ML packages you need atleast t2.medium ]
7 . sudo nginx
If you see this kind of error -
ubuntu@ip-172-31-28-221:~/adobe-face-recognition-api-django$ sudo nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
...
Then use these commands-
sudo pkill -f nginx & wait $!
sudo systemctl start nginx
Still same problem! Check Here
8 . gunicorn --bind 0.0.0.0:8000 core.wsgi:application
1 .
sudo apt-get install -y supervisor
cd /etc/supervisor/conf.d/
sudo touch gunicorn.conf
sudo nano gunicorn.conf
2 . Paste these lines- [Make sure you change 'ProjectFolderName' to your corresponding app folder]
[program:gunicorn]
directory=/home/ubuntu/ProjectFolderName
command=/home/ubuntu/ProjectFolderName/venv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/ProjectFolderName/app.sock core.wsgi:application
autostart=true
autorestart=true
stderr_logfile=/var/log/gunicorn/gunicorn.err.log
stdout_logfile=/var/log/gunicorn/gunicorn.out.log
[group:guni]
programs:gunicorn
Make your changes and press CTRL+O and Enter to save then CTRL+X to exit ###Create a Gunicorn systemd Service File
- Open and create systemd service file:
$ sudo nano /etc/systemd/system/gunicorn.service
- Write the following content to the file(Make sure you have put 'name_of_user' and 'myProject' according to actual username and project folder ):
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=name_of_user
Group=name_of_user
WorkingDirectory=/home/name_of_user/myproject
ExecStart=/home/name_of_user/myproject/virtualenv_directory/bin/gunicorn --
access-logfile - --workers 3 --bind unix:/home/name_of_user/myproject/myproject.sock myproject.wsgi:application
[Install]
WantedBy=multi-user.target
- Update changes:
$ sudo systemctl daemon-reload
- Starting the service:
$ sudo systemctl start gunicorn
- Enabling the service:
$ sudo systemctl enable gunicorn
- Check the status of the process:
$ sudo systemctl status gunicorn
3 . Updating django.conf
sudo mkdir /var/log/gunicorn
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl status
cd
cd /etc/nginx/sites-available/
sudo touch django.conf
sudo nano django.conf
4 . Paste these lines-
server{
listen 8080;
server_name YourServerAddress;
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/ProjectFolderName/app.sock;
}
}
Then update django.conf like this -
server{
listen 8080;
server_name YourServerAddress;
location / {
proxy_set_header Host $host;
proxy_pass http://unix:/home/ubuntu/ProjectFolderName/app.sock;
}
}
5 .
sudo nano /etc/nginx/nginx.conf
6 . Add this at the top of http block-
server_names_hash_bucket_size 164;
Or, Uncomment this -
server_names_hash_bucket_size 64;
And increase the size like this -
server_names_hash_bucket_size 164;
- By default file size upload limit is 1M. In order to change it just add this extra line in the same http block -
client_max_body_size 100M;
Otherwise you will get '413 Request Entity Too Large' error. Stackoverflow link for this error fixing.
7 .
cd /etc/nginx/sites-available/
sudo nginx -t
sudo ln -S django.conf /etc/nginx/sites-enabled/
sudo ln django.conf /etc/nginx/sites-enabled/
sudo service nginx restart
##Static files serving
sudo nano /etc/nginx/sites-available/django.conf
- And add the static location location-
location /static/{
autoindex on;
alias /home/ubuntu/ProjectFolderName/staticfiles/;
}
sudo systemctl reload nginx
same as static file serving..Just add the media files location-
location /Data/{
alias /home/ubuntu/ProjectFolderName/Data/;
}
sudo supervisorctl reload
If it is too much difficult for you to understand this documentation you can follow This tutorial