Issue
I'm trying to deploy a django app on AWS EC2 and also using gunicorn and Nginx in the process, I followed this tutorial link. I'm not sure what is going wrong, this is the first time I've used AWS EC2, when I try to launch the IP address of the instance it comes back as:
"can't open page because safari can't establish secure connection to the server"
in my aws console the EC2 'instance state' shows it's running
it has been setup in ubuntu 18.04 , I've also set up security groups in my EC2 instance with http, https, 443, 80, 8000, 5432 and 22 all allowed from any ip address except the 22 which only has my ip address as access. Also in my django app settings 'ALLOWED_HOSTS' , i've added the instance ip address and also '*'.
this is my gunicorn.socket file:
[Unit]
Description=gunicorn socket
[Socket]
ListenStream=/run/gunicorn.sock
[Install]
WantedBy=sockets.target
and my gunicorn service file:
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=djangoadmin
Group=www-data
WorkingDirectory=/home/ubuntu/djangoapp1 /
ExecStart=/home/djangoadmin/pyapps/venv/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
djangoapp1.wsgi:application
[Install]
WantedBy=multi-user.target
ngnix conf file :
server {
listen 80;
listen 443;
server_name **.***.***.*;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/djangoapp1;
}
location /media/ {
root /home/ubuntu/djangoapp1;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
I check the gunicorn socket with :
sudo systemctl start gunicorn.socket
sudo systemctl enable gunicorn.socket
sudo systemctl status gunicorn.socket
and it shows it running fine
I've also created a nginx conf file :
sudo vim /etc/nginx/sites-available/djangoapp1
and linked it with :
sudo ln -s /etc/nginx/sites-available/djangoapp1 /etc/nginx/sites-enabled
then restarted the nginx server:
sudo systemctl restart nginx
still nothing works! would appreciate any help
Solution
Solution:
Wrong server_name configuration in the nginx config
Did you assign an elastic IP address to your EC2 instance?
Select EC2 instance> Actions>Networking> Manage Ip Addresses. Then use option "To add or edit an IPv4 public IP Allocate an Elastic IP to this instance or network interface".
Create an elastic IP Navigate to Elastic IP address link> click Associate IP Select the instance to associate IP and save. Now you will have EC2 instance with public IP without restarting/deleting EC2 instance.
Without this your EC2 instance won't be available outside the Aws network.
It costs money so make sure to check pricing.
Answered By - Lucasz