You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

生产环境如何修改端口?Django部署亚马逊服务器端口配置咨询

How to Access Your Django Site at mysite.com (No Port Required)

Hey there! Let's break this down for you—right now you’re seeing mysite.com:8000 because you’re probably using Django’s built-in runserver command, which is great for local testing but not designed for production. To get your site accessible at just mysite.com, you’ll need a mix of small code tweaks and server-side configuration, with most of the heavy lifting happening on the server.

1. Code-Level Tweaks (Small but Critical)

These changes go in your Django project’s settings.py file:

  • Update ALLOWED_HOSTS: Django blocks requests from unapproved domains to prevent security issues. Add your domain here:
    ALLOWED_HOSTS = ['mysite.com', 'www.mysite.com']
    
  • Configure Static Files: If your site uses CSS, JS, or images, set STATIC_ROOT to a directory where Django can collect all static assets for production:
    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
    
    Then run this command to gather all static files into that directory:
    python manage.py collectstatic
    

2. Server-Side Configuration (The Main Work)

Assuming you’re using an AWS EC2 instance (the most common Amazon server setup), follow these steps:

Step 1: Install a WSGI Server (Gunicorn)

Django’s dev server isn’t built to handle production traffic. Gunicorn will act as the bridge between your Django app and the web:

pip install gunicorn

Test it first to make sure it works with your project:

gunicorn --bind 0.0.0.0:8000 your_project_name.wsgi:application

Replace your_project_name with the name of your Django project (the folder containing wsgi.py).

Step 2: Set Up Nginx as a Reverse Proxy

Nginx will listen on port 80 (the default HTTP port) and forward traffic to Gunicorn running on port 8000. This is what lets visitors access your site without typing the port number.

  • Install Nginx:
    sudo apt update && sudo apt install nginx
    
  • Create an Nginx site configuration file:
    sudo nano /etc/nginx/sites-available/mysite
    
    Paste this config (replace paths and domain with your own):
    server {
        listen 80;
        server_name mysite.com www.mysite.com;
    
        # Handle favicon and static files directly with Nginx (faster than Gunicorn)
        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
            root /path/to/your/django/project; # Path where your staticfiles folder lives
        }
    
        # Forward all other requests to Gunicorn
        location / {
            include proxy_params;
            proxy_pass http://127.0.0.1:8000;
        }
    }
    
  • Enable the configuration and restart Nginx:
    sudo ln -s /etc/nginx/sites-available/mysite /etc/nginx/sites-enabled/
    sudo nginx -t # Test for config errors
    sudo systemctl restart nginx
    

Step 3: Make Gunicorn a System Service (Auto-Start on Boot)

To ensure Gunicorn runs automatically when your server restarts:

  • Create a systemd service file:
    sudo nano /etc/systemd/system/gunicorn.service
    
    Paste this (adjust paths, user, and project name to match your setup):
    [Unit]
    Description=gunicorn daemon for my Django site
    After=network.target
    
    [Service]
    User=ubuntu # Replace with your server's username
    Group=www-data
    WorkingDirectory=/path/to/your/django/project
    ExecStart=/path/to/your/virtualenv/bin/gunicorn --workers 3 --bind 127.0.0.1:8000 your_project_name.wsgi:application
    
    [Install]
    WantedBy=multi-user.target
    
  • Start and enable the service:
    sudo systemctl start gunicorn
    sudo systemctl enable gunicorn
    

Step 4: Update AWS Security Group

Don’t forget to allow incoming traffic on port 80 in your EC2 instance’s security group:

  • Go to the AWS EC2 console, find your instance, and open its security group settings.
  • Add an inbound rule for HTTP (port 80) from 0.0.0.0/0 (or restrict to specific IPs if you want tighter security).

For a secure site, use HTTPS (port 443). You can get a free SSL certificate from Let’s Encrypt:

  • Install Certbot:
    sudo apt install certbot python3-certbot-nginx
    
  • Run Certbot to configure SSL automatically:
    sudo certbot --nginx -d mysite.com -d www.mysite.com
    

This will update your Nginx config to use HTTPS and redirect all HTTP traffic to HTTPS.

Key Takeaway

Most of the configuration happens on the server (setting up Gunicorn, Nginx, and system services). The code changes are minimal but essential to ensure Django accepts requests from your domain and serves static files correctly.

内容的提问来源于stack exchange,提问作者CAB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:16:56