{"id":38343375,"url":"https://github.com/nicknaskida/nginx-django-server","last_synced_at":"2026-01-17T03:01:09.522Z","repository":{"id":133304650,"uuid":"423868362","full_name":"NickNaskida/NGINX-django-server","owner":"NickNaskida","description":"Instruction to host Django project on ubuntu server with Nginx and Gunicorn.","archived":false,"fork":false,"pushed_at":"2024-05-29T12:02:44.000Z","size":57,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-05-30T00:58:14.493Z","etag":null,"topics":["django-gunicorn","django-nginx","django-server","gunicorn","gunicorn-web-server","nginx","nginx-configuration","nginx-proxy","nginx-server","server-configuration"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NickNaskida.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-11-02T14:07:00.000Z","updated_at":"2024-05-29T12:02:47.000Z","dependencies_parsed_at":null,"dependency_job_id":"d2672908-e8df-4b02-a292-c8f25bcb3f8e","html_url":"https://github.com/NickNaskida/NGINX-django-server","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/NickNaskida/NGINX-django-server","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NickNaskida%2FNGINX-django-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NickNaskida%2FNGINX-django-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NickNaskida%2FNGINX-django-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NickNaskida%2FNGINX-django-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NickNaskida","download_url":"https://codeload.github.com/NickNaskida/NGINX-django-server/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NickNaskida%2FNGINX-django-server/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28492593,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-17T02:39:23.645Z","status":"ssl_error","status_checked_at":"2026-01-17T02:34:19.649Z","response_time":85,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django-gunicorn","django-nginx","django-server","gunicorn","gunicorn-web-server","nginx","nginx-configuration","nginx-proxy","nginx-server","server-configuration"],"created_at":"2026-01-17T03:00:53.914Z","updated_at":"2026-01-17T03:01:09.507Z","avatar_url":"https://github.com/NickNaskida.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Django production server with Nginx and Gunicorn\n\n### Introduction\nDjango is an open-source Python framework that can be used for deploying Python applications. It comes with a development server to test your Python code in the local system. If you want to deploy a Python application on the production environment then you will need a powerful and more secure web server. In this case, you can use Gunicorn as a WSGI HTTP server and Nginx as a proxy server to serve your application securely with robust performance.\n\n## Install Required Packages\n\nFirst, you will need to install Nginx and other Python dependencies on your server. You can install all the packages with the following command:\n```\nsudo apt-get install vim python3-pip python3-dev libpq-dev curl nginx -y\n```\nOnce all the packages are installed, start the Nginx service and enable it to start at system reboot:\n\n```\nsudo systemctl start nginx\nsudo systemctl enable nginx\n```\n\n## Create new user and clone your repo\n\nChange user to root with following command\n```\nsudo su\n```\nCreate new user \n```\nsudo adduser djangouser\n```\nUse the usermod command to add the user to the sudo group\n```\nsudo usermod -aG sudo djangouser\n```\nChange user and clone github repo\n```\nsudo su - djangouser\nsudo git clone https://github.com/github-username/repo-name.git\n```\n\n## Install and Configure PostgreSQL\n\nNext, you will need to install the PostgreSQL server on your server. You can install it with the following command:\n```\nsudo apt-get install postgresql postgresql-contrib -y\n```\nAfter the installation, log in to PostgreSQL shell with the following command:\n```\nsudo su - postgres\npsql\n```\nNext, create a database and user for Django with the following command:\n\n```\nCREATE DATABASE djangodb;\nCREATE USER djangouser WITH PASSWORD 'password';\n```\nNext, grant some required roles with the following command:\n```\nALTER ROLE djangouser SET client_encoding TO 'utf8';\nALTER ROLE djangouser SET default_transaction_isolation TO 'read committed';\nALTER ROLE djangouser SET timezone TO 'UTC';\nGRANT ALL PRIVILEGES ON DATABASE djangodb TO djangouser;\n```\nNext, exit from the PostgreSQL shell using the following command:\n```\n\\q\n\n# to exit user\nexit\n```\n\n## Create a Python Virtual Environment\nNext, you will need to create a Python virtual environment for the Django project.\n\nFirst, upgrade the PIP package to the latest version:\n```\npip install --upgrade pip\n```\nNext, install the virtualenv package using the following command:\n```\npip install python3.9-dev python3.9-venv\n```\n_In case of error try this:_\n```\nsudo apt-get install python3.9-dev python3.9-venv\n```\n\nNext, create a directory for the Django project using the command below:\n```\nmkdir ~/django_project\n```\nNext, change the directory to django_project and create a Django virtual environment:\n```\ncd ~/django_project\npython3.9 -m venv venv\n```\nNext, activate the Django virtual environment:\n```\nsource venv/bin/activate\n```\nNext, install the Gunicorn and other packages with the following commands:\n```\npip install gunicorn psycopg2-binary\npip install -r requirements.txt\n```\n## Configure Django\n\nEdit the settings.py and define your database settings:\n```\nsudo vim ~/django_project/django_project/settings.py\n```\n\nFind and change the following lines:\n```\nALLOWED_HOSTS = ['django.example.com', 'localhost']\nDATABASES = {  \n\t'default': {     \n\t\t'ENGINE': 'django.db.backends.postgresql_psycopg2',       \n\t\t'NAME': 'djangodb',       \n\t\t'USER': 'djangouser',        \n\t\t'PASSWORD': 'password',        \n\t\t'HOST': 'localhost',       \n\t\t'PORT': '',    \n\t}\n}\nSTATIC_URL = '/static/'\nimport os\nSTATIC_ROOT = os.path.join(BASE_DIR, 'static/')\n```\n\nSave and close the file then migrate the initial database schema to the PostgreSQL database:\n```\npython manage.py makemigrations\npython manage.py migrate\n```\n\nNext, create an admin user with the following command:\n```\npython manage.py createsuperuser\n```\n\nNext, gather all the static content into the directory\n```\npython manage.py collectstatic\n```\n\n## Test the Django Development Server\nNow, start the Django development server using the following command:\n```\npython manage.py runserver 0.0.0.0:8000\n```\nYou should see the following output:\n```\nWatching for file changes with StatReloader\nPerforming system checks...\nSystem check identified no issues (0 silenced).\nJune 22, 2021 - 11:15:57\nDjango version 3.2.4, using settings 'django_project.settings'\nStarting development server at http://0.0.0.0:8000/\nQuit the server with CONTROL-C.\n```\n\nNow, open your web browser and access your Django app using the URL http://django.example.com:8000/admin/. You will be redirected to the Django login page.\nProvide your admin username, password and click on the Login. You should see the Django dashboard on the following page:\n\nNow, go back to your terminal and press CTRL + C to stop the Django development server.\n\n## Test Gunicorn\nNext, you will need to test whether the Gunicorn can serve the Django or not. You can start the Gunicorn server with the following command:\n```\ngunicorn --bind 0.0.0.0:8000 django_project.wsgi\n```\n\nIf everything is fine, you should get the following output:\n```\n[2021-06-22 11:20:02 +0000] [11820] [INFO] Starting gunicorn 20.1.0\n[2021-06-22 11:20:02 +0000] [11820] [INFO] Listening at: http://0.0.0.0:8000 (11820)\n[2021-06-22 11:20:02 +0000] [11820] [INFO] Using worker: sync\n[2021-06-22 11:20:02 +0000] [11822] [INFO] Booting worker with pid: 11822\n```\nPress CTRL + C to stop the Gunicorn server.\n\nNext, deactivate the Python virtual environment with the following command:\n```\ndeactivate\n```\n\n## Create a Systemd Service File for Gunicorn\nIt is a good idea to create a systemd service file for the Gunicorn to start and stop the Django application server.\n\nTo do so, create a socket file with the following command:\n```\nsudo vim /etc/systemd/system/gunicorn.socket\n```\nAdd the following lines:\n```\n[Unit]\nDescription=gunicorn socket\n[Socket]\nListenStream=/run/gunicorn.sock\n[Install]\nWantedBy=sockets.target\n```\n\nSave and close the file then create a service file for Gunicorn:\n```\nsudo vim /etc/systemd/system/gunicorn.service\n```\nAdd the following lines that match your Django project path:\n```\n[Unit]\nDescription=gunicorn daemon\nRequires=gunicorn.socket\nAfter=network.target\n\n[Service]\nUser=root\nGroup=www-data\nWorkingDirectory=/root/django_project\nExecStart=/root/django_project/venv/bin/gunicorn --access-logfile - --workers 5 --bind unix:/run/gunicorn.sock django_project.wsgi:application\n\n[Install]\nWantedBy=multi-user.target\n```\n\nSave and close the file then set proper permission to the Django project directory:\n```\nsudo chown -R www-data:root ~/django_project\n```\nNext, reload the systemd daemon with the following command:\n```\nsudo systemctl daemon-reload\n```\nNext, start the Gunicorn service and enable it to start at system reboot:\n```\nsudo systemctl start gunicorn.socket\nsudo systemctl enable gunicorn.socket\n```\nTo check the status of the Gunicorn, run the command below:\n```\nsudo systemctl status gunicorn.socket\n```\nYou should get the following output:\n```\n● gunicorn.socket - gunicorn socket     \nLoaded: loaded (/etc/systemd/system/gunicorn.socket; enabled; vendor preset: enabled)     \nActive: active (running) since Tue 2021-06-22 12:05:05 UTC; 3min 7s ago   Triggers: ● gunicorn.service     \nListen: /run/gunicorn.sock (Stream)     \nCGroup: /system.slice/gunicorn.socket\nJun 22 12:05:05 django systemd[1]: Listening on gunicorn socket.\n```\n\n## Configure Nginx as a Reverse Proxy to Gunicorn Application\nNext, you will need to configure Nginx as a reverse proxy to serve the Gunicorn application server.\n\nTo do so, create an Nginx configuration file:\n```\nsudo vim /etc/nginx/conf.d/django.conf\n```\n\nAdd the following lines:\n```\nserver {\n        server_name django.example.com;\n\n        client_body_buffer_size 200K;\n        client_header_buffer_size 2k;\n        client_max_body_size 100M;\n        large_client_header_buffers 3 1k;\n\n        client_body_timeout 5s;\n        client_header_timeout 5s;\n\n        location = /favicon.ico { access_log off; log_not_found off; }\n\n        location /static {\n                alias ~/django_project;\n        }\n\n        location /media {\n                alias ~/django_project;\n        }\n\n        location / {\n                include proxy_params;\n                proxy_pass http://unix:/run/gunicorn.sock;\n                proxy_set_header CLIENT-IP $remote_addr;\n                proxy_set_header X-Real-IP $remote_addr;\n                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n                limit_conn two 10;\n        }\n\n}\n\n```\nSave and close the file then verify the Nginx for any configuration error:\n```\nnginx -t\n```\nOutput:\n```\nnginx: the configuration file /etc/nginx/nginx.conf syntax is ok\nnginx: configuration file /etc/nginx/nginx.conf test is successful\n```\nFinally, restart the Nginx service to apply the changes:\n```\nsystemctl restart nginx\n```\nNow, you can access the Django application using the URL http://django.example.com.\n\n\n## SSL certificate with certbot\nVisit https://certbot.eff.org/, choose your machine configuration and comlete written commands.\n\n\nUbuntu 20.04 + Nginx https://certbot.eff.org/instructions?ws=nginx\u0026os=ubuntufocal\n\nTo confirm that your site is set up properly, visit https://django.example.com/ in your browser and look for the lock icon in the URL bar.\n\n## Automatic SSL certificate renewal crontab job\n\nOpen crontab with following command:\n```\nsudo crontab -e\n```\nAdd the following line to the end of the file:\n```\n30 4 1 * * sudo certbot renew --quiet\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnicknaskida%2Fnginx-django-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnicknaskida%2Fnginx-django-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnicknaskida%2Fnginx-django-server/lists"}