Skip to content

Instantly share code, notes, and snippets.

@devbkhadka
Last active September 16, 2019 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devbkhadka/f317f129e3cb5a5940f9daab28925767 to your computer and use it in GitHub Desktop.
Save devbkhadka/f317f129e3cb5a5940f9daab28925767 to your computer and use it in GitHub Desktop.
provision ubuntu server to host django site using nginx and gunicorn
# this command is necessary to tell Systemd to load our new config file
elspeth@server:$ sudo systemctl daemon-reload
# this command tells Systemd to always load our service on boot
elspeth@server:$ sudo systemctl enable gunicorn-superlists-staging.ottg.eu
# this command actually starts our service
elspeth@server:$ sudo systemctl start gunicorn-superlists-staging.ottg.eu
# Check the Systemd logs for using
sudo journalctl -u gunicorn-superlists-staging.ottg.eu.
# You can ask Systemd to check the validity of your service configuration:
systemd-analyze verify /path/to/my.service.
# Remember to restart both services whenever you make changes.
# If you make changes to the Systemd config file, you need to run daemon-reload before systemctl restart to see the effect of your changes.
[Unit]
Description=Gunicorn server for superlists-staging.ottg.eu
[Service]
Restart=on-failure 
User=elspeth 
WorkingDirectory=/home/elspeth/sites/superlists-staging.ottg.eu/source
# needs full path of executable
ExecStart=/home/elspeth/sites/superlists-staging.ottg.eu/virtualenv/bin/gunicorn \
--bind unix:/tmp/superlists-staging.ottg.eu.socket \
superlists.wsgi:application 
[Install]
WantedBy=multi-user.target 
Provisioning
add-apt-repository ppa:fkrull/deadsnakes
apt-get install nginx python3.6 python3.6-venv
Add Nginx config for virtual host at /etc/nginx/sites-available and create its symlink at /etc/nginx/sites-enabled
server {
listen 80;
server_name ec2-3-15-174-164.us-east-2.compute.amazonaws.com;
location /static {
alias /home/ubuntu/uat/static;
}
location / {
proxy_set_header HOST $host;
proxy_pass http://unix:/tmp/ec2-3-15-174-164.us-east-2.compute.amazonaws.com.socket;
}
}
Add Systemd job for Gunicorn
Deployment
Create directory structure in ~/sites
Pull down source code into folder named source
Start virtualenv in ../virtualenv
pip install -r requirements.txt
manage.py migrate for database
collectstatic for static files
Set DEBUG = False and ALLOWED_HOSTS in settings.py
Restart Gunicorn job
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment