- Django Project uploaded on Git
- AWS EC2 Account
- Log in to AWS
- Go to AWS Console
- On Instances, Launch Instance
- Choose (Free Tier) Ubuntu Server 18.04 LTS (HVM)
- Choose Free Tier Instance Type
- Click on Step 6 - Configure Security Group
- Add Rule HTTP
- Review and Launch , Ignore warning, Launch
- Choose a key pair or create a new one your choice
- EC2 Setup - Done *
- update permissions on keypair file
chmod 400 keypair.pem
- ssh to the server: example AWS EC2 Public DNS = ec2-54-219-133-135.us-west-1.compute.amazonaws.com
ssh -i "keypair.pem" ubuntu@<AWS EC2 Public DNS>
- Choose Yes, Enter
- Run some commands
sudo apt-get update
sudo apt-get install python-pip python-dev nginx git
* yes *
sudo apt-get update
sudo pip install virtualenv
git clone https://github.com/YourDjangoProjectRepo/YourDjangoProjectRepo.git
cd YourDjangoProjectRepo
virtualenv venv
source venv/bin/activate
pip install -r requirements.txt
pip install django bcrypt django-extensions
pip install gunicorn
cd YourDjangoProjectRepo *(yes, it's the same name as the parent folder)*
sudo vim settings.py
ALLOWED_HOSTS = ['<AWS EC2 Public DNS>']
Add this line to the bottom of the file
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
Save Changes by pressing Escape, type :wq
cd ..
python manage.py collectstatic
gunicorn --bind 0.0.0.0:8000 JCmain.wsgi:application
Quit Process CTRL + C (if no errors)
sudo vim /etc/systemd/system/gunicorn.service
Copy below code
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/YourDjangoProjectName
ExecStart=/home/ubuntu/YourDjangoProjectName/venv/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/YourDjangoProjectName/YourDjangoProjectName.sock YourDjangoProjectName.wsgi:application
[Install]
WantedBy=multi-user.target
Now on Terminal
i
Shift + Insert (or type code manually)
ESC
:wq
More Setup
sudo systemctl daemon-reload
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
sudo vim /etc/nginx/sites-available/YourDjangoProjectName
Copy Code
server {
listen 80;
server_name <AWS EC2 Public DNS>;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/YourDjangoProjectName;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/ubuntu/YourDjangoProjectName/YourDjangoProjectName.sock;
}
}
On terminal
i
Shift + Insert (or write manually)
ESC
:wq
Some more setup commands
sudo ln -s /etc/nginx/sites-available/YourDjangoProjectName /etc/nginx/sites-enabled
sudo nginx -t
sudo rm /etc/nginx/sites-enabled/default
sudo service nginx restart