Apply your Django project's database schema and collect static files required for the web service. Manage the server application based on your chosen integration method (Gunicorn or mod_wsgi), start the Apache web server, and configure the system firewall to allow access to the website.
7. Migration and Static Files Collection
Run Django database migrations and collect static files needed for the web service.
source /home/hello/venv/bin/activate # Activate virtual environment
python /home/hello/manage.py migrate # Apply database schema (create/modify tables)
python /home/hello/manage.py collectstatic --noinput # Collect all static files within the project
8. Gunicorn Service Setup and Start (Gunicorn Integration)
Register Gunicorn as a system service so it can automatically run and be managed upon server boot. (Recommended)
Create or Modify gunicorn.service
:
cat | sudo tee /etc/systemd/system/gunicorn.service > /dev/null << EOF
[Unit]
Description=Gunicorn daemon for Django project
After=network.target
[Service]
User=root # User to run Gunicorn (project owner or web server user)
Group=root # Group to run Gunicorn
# Note: For enhanced security, it's recommended to specify a least-privileged user like www-data or apache.
# Gunicorn execution command
ExecStart=/home/hello/venv/bin/gunicorn \
--workers 3 \
--bind 127.0.0.1:8000 \
config.wsgi:application
WorkingDirectory=/home/hello # Django project root directory
ExecReload=/bin/kill -s HUP $MAINPID
KillMode=mixed
TimeoutStopSec=5
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
Enable and Start Gunicorn Service:
sudo systemctl daemon-reload # Reload systemd configurations
sudo systemctl enable gunicorn # Enable gunicorn service
sudo systemctl start gunicorn # Start gunicorn service
sudo systemctl status gunicorn # Check gunicorn service status
8. Apache Start and Firewall Access (Common)
Restart the Apache web server to apply the web server configuration and allow HTTP traffic through the firewall.
sudo firewall-cmd --permanent --add-port=8000/tcp # Permanently allow Gunicorn port (8000/tcp) in Firewalld (if using Gunicorn HTTP method)
sudo firewall-cmd --reload # Reload firewall settings (apply immediately)
sudo systemctl enable httpd # Set Apache to start automatically on boot (may already be enabled)
sudo systemctl restart httpd # Restart Apache service (Gunicorn/mod_wsgi integration settings)
sudo firewall-cmd --permanent --add-service=http # Permanently allow HTTP (port 80) traffic in firewall
sudo firewall-cmd --reload # 방화벽 설정 다시 로드 (즉시 적용)
# If using HTTPS (port 443) also: sudo firewall-cmd --permanent --add-service=https && sudo firewall-cmd --reload