SysAdmin

Django application on an Nginx + cPanel Apache setup

JJ
Jobin Joseph · Founder and CEO
·Published 28 April 2014·1 min read

This is how you configure an Django application on your cPanel webserver with Nginx + Apache.

Here my should be running only over httpS URL for the public,

Steps

1. Install cPanel as normal,
2. reserve an IP for Nginx (say 123.123.123.123) 1. Show/Edit Reserved IPs >> Tick the IP 123.123.123.123 to use for Nginx and put a comment as Nginx >> Save
2. Apache Configuration >> Reserved IPs Editor >> Tick the IP 123.123.123.123 to choose reserved >> Save
3. Park a domain apache.example.com in cPanel for the hosting account
4. Add /etc/hosts entry on the serve
123.123.123.123 www.apache.example.com apache.example.com
5. Install Nginx
6. Disable HTTPS completely  from Django level.
7. Configure nginx to bind 80 port only.
8. vim /etc/nginx/nginx.conf

user nginx; worker_processes 4; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; multi_accept on; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘ ‘$status $body_bytes_sent “$http_referer” ‘ ‘”$http_user_agent” “$http_x_forwarded_for”‘; access_log /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 120; gzip on; gzip_buffers 16 8k; gzip_comp_level 4; gzip_http_version 1.0; gzip_min_length 1280; gzip_types text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon image/bmp; gzip_vary on; include /etc/nginx/conf.d/*.conf; }

9. Redirect All HTTP traffic to the correct HTTPS url

[/etc/nginx/conf.d]# cat domain.conf server { listen 123.123.123.123:80; server_name _; rewrite ^ https://www.example.com$request_uri? permanent; }

10. Configure the site to host the site as HTTPS

[/etc/nginx/conf.d]# cat domain-ssl.conf server { listen 123.123.123.123:443 ssl; server_name example.com www.example.com; ssl_certificate /etc/nginx/ssl/cert.pem; ssl_certificate_key /etc/nginx/ssl/ssl.key; location /static { alias /home/user/project/static; expires 10d; break; gzip on; } location /media { alias /home/user/project/media; gzip on; expires 10d; break; } location / { proxy_pass http://www.apache.example.com; #proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-for $remote_addr; } }

Feel Free to comment of any doubt / Issues

Jobin Joseph
Jobin Joseph, Founder and CEO
5x AWS Certified, ISO 27001:2022 Lead Implementer, 17+ years building cloud infrastructure for regulated industries.
About Jobin →

Reading this because your AWS bill or uptime is a problem?

30 minutes, direct with the founder, no sales pitch: one specific recommendation for your AWS workload.

Book a free 30-minute review
30 min Free Consultation →