How do I deploy Django app to (AWS) domain name?

2019-05-19 21:37发布

问题:

I've only been working with Django for a few weeks, and just learned deployment. I have an AWS EC2 Instance, which I'm able to deploy my Django website/app to (i.e., if I go to the IP address in my browser, I'm able to access my website). However, I can't figure out how to deploy that same website to my domain name, which is registered on AWS.

I followed AWS's documentation (http://docs.aws.amazon.com/Route53/latest/DeveloperGuide/routing-to-ec2-instance.html), and https://cachecheck.opendns.com/ shows that my domain name routes to my EC2 instance. However, when I go to my domain name in my browser, it shows a 400 Bad Request. Is there something else I need to do when I setup nginx or gunicorn? I've tried replacing the {{yourEC2.public.ip}} in the nginx code to be my domain name instead of the IP address.

I haven't been able to find any other resources online regarding deployment to domain names, specifically with AWS.

This is what I have for deploying to the EC2 instance:

settings.py:

DEBUG = False
ALLOWED_HOSTS = ['{{yourEC2.public.ip}}']
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
(venv) ubuntu@54.162.31.253:~myRepoName$ python manage.py collectstatic

gunicorn

[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/{{repoName}}
ExecStart=/home/ubuntu/{{repoName}}/venv/bin/gunicorn --workers 3 --bind 
unix:/home/ubuntu/{{repoName}}/{{projectName}}.sock 
{{projectName}}.wsgi:application
[Install]
WantedBy=multi-user.target

nginx:

server {
    listen 80;
    server_name {{yourEC2.public.ip}};
    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/ubuntu/{{myRepoName}};
    }
    location / {
        include proxy_params;
        proxy_pass http://unix:/home/ubuntu/{{myRepoName}}/{{projectName}}.sock;
    }
}

回答1:

You should set ALLOWED_HOSTS to your hostname(s), such as ['example.com', 'www.example.com']:

A list of strings representing the host/domain names that this Django site can serve. This is a security measure to prevent HTTP Host header attacks, which are possible even under many seemingly-safe web server configurations.



回答2:

I had this exact same problem, and actually found your various posts also seeking solutions. Funny how we both had the exact same issue in such a similar window of time.

I did three things:

  • First, I updated my ALLOWED_HOSTS just as Selcuk noted:

    • ALLOWED_HOSTS = ['12.345.67.890', 'sub.domain.com', 'www.sub.domain.com']
  • Then, I also edited the server_name setting in my nginx configuration:

    • server_name 12.345.67.890 sub.domain.com www.sub.domain.com;
  • Lastly, I restarted nginx and rebooted the machine to make sure it all worked:

    • sudo service nginx restart
    • sudo reboot

After this, I was able to load my domain and the application would load. I know you already found your solution, but wanted to formalize this, as I found your question on StackOverflow and hopefully others will too so they may be spared!