3

I'm trying to deploy a readthedocs instance on my own server. The recommended way to deploy is using Gunicorn + nginx with postgres. Because there's basically no documentation on how to do this (except from their fabfiles which of course, only works on their server), I've been trying to setup my own server, manually.

Here's my nginx.conf:

server { listen 80 default; server_name mysite.com; access_log /var/log/nginx/mysite.access.log; error_log /var/log/nginx/mysite.error.log; location /favicon.ico { root /home/mysite/Code/checkout/readthedocs.org/media/images; break; } location robots.txt { root /home/mysite/Code/checkout/readthedocs.org/media; break; } location /static/ { alias /home/mysite/Code/checkout/readthedocs.org/media/; expires 30d; break; } location /media/ { alias /home/mysite/Code/checkout/readthedocs.org/media/; expires 30d; break; } location / { proxy_pass_header Server; proxy_set_header Host $http_host; proxy_redirect off; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Scheme $scheme; proxy_connect_timeout 10; proxy_read_timeout 10; proxy_pass http://127.0.0.1:8888; } } 

The settings.py for django configuration can be found here. The only thing I changed on settings.py is I add gunicorn on INSTALLED_APPS so I can run gunicorn with it.

The command I use to run the gunicorn server is:

./manage.py run_gunicorn -b 127.0.0.1:8888 

Then if I try to access 127.0.0.1 from a local browser, it would work, but always show a 404 page, no matter what URL I entered. Running ./manage.py runserver will run everything correctly.

Now I am not a sysadmin, and have basically 0 experience with django, gunicorn, or nginx before. I've been googling and playing around the configuration for weeks, with 0 result. My question is:

  1. How do I know which django's route called by gunicorn? Can I debug this? All log files that I can find didn't show this.
  2. Do you see anything wrong with my configuration file? If so, could you please tell me what's wrong?

Thank you very much.

2 Answers 2

0

Have you tried loading the site at port 8888 to ensure that it's running correctly?

Does the 404 message you're getting have nginx in the text? This would indicate that nginx is having the problem finding the resource and not the service on port 8888. In which case you can troubleshoot your config file.

You're logging the access in nginx, so you can look at your log for some more information. tail -n 10 /var/log/nginx/mysite.access.log will let you see the last 10 lines of that log.

By default, nginx uses the combined log format:

log_format combined '$remote_addr - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent ' '"$http_referer" "$http_user_agent"'; 

That should help you figure out what each column in your log file means.

0

Make sure you are running Gunicorn correctly. It should be running from your project's directory, and you may want to specify the Django WSGI application. Example:

gunicorn --bind 127.0.0.1:8888 myproject.wsgi:application 

Replace myproject.wsgi:application with the correct import path for your Django project's WSGI application.

Debug the routes and check which views are being called, you could add logging to your Django project by adding this to your settings.py:

LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'file': { 'level': 'DEBUG', 'class': 'logging.FileHandler', 'filename': '/path/to/your/logs/django_debug.log', }, }, 'loggers': { 'django': { 'handlers': ['file'], 'level': 'DEBUG', 'propagate': True, }, }, } 

Make sure the log file path exists and is writable.

When you run Gunicorn, you can direct its output to a log file to capture any issues. For example:

gunicorn --bind 127.0.0.1:8888 myproject.wsgi:application --log-file /path/to/your/logs/gunicorn.log 

Make sure your PostgreSQL settings in settings.py are correct and that the database is accessible. Incorrect database settings can cause issues that might not be immediately obvious.

Make sure your static and media files are correctly collected and served. Run these:

./manage.py collectstatic ./manage.py migrate 

Confirm that Nginx, Gunicorn, and PostgreSQL are all running without errors.

sudo systemctl status nginx sudo systemctl status gunicorn sudo systemctl status postgresql 

Make sure your Nginx process has read permissions for the directories where your static and media files are stored.

Now you should be able to identify the cause of the 404 errors and confirm your deployment is set up correctly.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.