This post is a quick checklist for a simple deployment of django with postgres on an aws EC2 instance. –>
Initial set up of the project on an ubuntu instance
# ssh and install what you need
ssh -i ~/.ssh/aws-eb ubuntu@<instance-ip>
# pulling from github
git init
git remote add origin [email protected]:<github-user-and-ripo>
git pull origin production
# pip install
cd my_project
source .venv/bin/activate
pip3 install -r requirements/production.txt
# setting enviroment variables
touch .env
echo "export DJANGO_SETTINGS_MODULE=myapp.production_settings" >> .env
echo "source /home/ubuntu/my_project/.env" >> ~/.bashrc
Setting up the postgres
you can either serve your own postgres on an instance or use a service similar to AWS RDS.
# setting up your own postgres
sudo apt update
sudo apt install postgresql libpq-dev postgresql-client postgresql-client-common
sudo systemctl enable postgresql.service
sudo -u postgres psql postgres
# connecting to your postgres and running commands
psql -h <local or remote-server> --port=5432 -U <user-name> -d <database-name>
psql -h <local or remote-server> --port=5432 -U <user-name> -d <database-name> < queries.sql
psql -h <local or remote-server> --port=5432 -U <user-name> -d <database-name> -c "SQL COMMANDS"
>>> CREATE DATABASE myproject;
>>> CREATE USER my_project_user WITH PASSWORD 'password';
dump a local postgres to a remote machine
ssh and dump
pg_dump -h localhost --username=my_user -d my_project\
| ssh -i ~/.ssh/aws-eb ubuntu@**__**.amazonaws.com 'sudo -u postgres psql database_name'
dump to AWS RDS
pg_dump --no-privileges --no-owner -h localhost --username=my_user -d my_project\
| psql --host **__***.rds.amazonaws.com --port=5432 --username=postgres -d database_name
Remember to add your EC2 instance security group and also your own local computer IP to the Inbound rules of your postgres on RDS.
Setting up gunicorn and nginx
pip3 install django gunicorn
sudo vim /etc/systemd/system/gunicorn.service
------------------------------------------------------
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/my_project
ExecStart=/home/ubuntu/my_project/.venv/bin/gunicorn --env DJANGO_SETTINGS_MODULE=my_project.production_settings \
--access-logfile /var/log/gunicorn.log --workers 3 --bind unix:/home/ubuntu/my_project/my_project.sock my_project.wsgi:application
[Install]
WantedBy=multi-user.target
------------------------------------------------------
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
sudo apt install nginx
sudo vim /etc/nginx/sites-available/myproject
------------------------------------------------------
server {
listen 80;
server_name < domain-name or >;
location /static/ {
root /home/ubuntu/my_project;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/my_project/my_project.sock;
}
}
------------------------------------------------------
sudo ln -s /etc/nginx/sites-available/my_project /etc/nginx/sites-enabled
sudo systemctl restart nginx
sudo ufw allow 'Nginx Full'