{"id":29,"date":"2014-07-14T04:08:00","date_gmt":"2014-07-14T04:08:00","guid":{"rendered":"http:\/\/392@jodohost.com\/\/blog\/"},"modified":"2023-01-05T16:16:02","modified_gmt":"2023-01-05T16:16:02","slug":"setting-up-a-django-server-using-nginx-and-gunicorn","status":"publish","type":"post","link":"http:\/\/www.jodohost.com\/blog\/index.php\/2014\/07\/14\/setting-up-a-django-server-using-nginx-and-gunicorn\/","title":{"rendered":"Setting Up a Django Server Using Nginx and Gunicorn"},"content":{"rendered":"<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p>We&#8217;ll setup and configure the Django server on Ubuntu. We are going to configure it with Nginx and Gunicorn for good compatibility and performance.<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<h2>\u2022 Preparation<\/h2>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Make sure that your system is updated before proceeding with the installation.<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 sudo apt-get update<strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<h2>\u2022 Install and Create Virtualenv<\/h2>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Installing Virtualenv<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 sudo apt-get install python-virtualenv<strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Create our Virtualenv environment so we can install Django and other Python packages within it:<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 sudo virtualenv \/opt\/myenv<br \/><\/strong><\/p>\n<p style=\"padding-left: 60px;\"><strong><br \/><\/strong><\/p>\n<p style=\"padding-left: 60px;\"><strong><br \/><\/strong><\/p>\n<h2>\u2022 Install Django<\/h2>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Activate the Virtualenv environment so that when we install Python packages they install to our Virtualenv environment<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 source \/opt\/myenv\/bin\/activate<strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0&#8220;<strong>(myenv)<\/strong>&#8221; should have been appended to the beginning of your terminal prompt.<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Install Django<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 pip install django<\/strong><\/p>\n<p style=\"padding-left: 60px;\"><strong><br \/><\/strong><\/p>\n<p style=\"padding-left: 60px;\"><strong><br \/><\/strong><\/p>\n<h2>\u2022 Install PostgreSQL<\/h2>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Run the following command to deactivate Virtualenv first:<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 deactivate<strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Install dependencies for PostgreSQL to work with Django:<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 <strong>sudo apt-get install libpq-dev python-dev<strong><br \/><\/strong><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Install PostgreSQL:<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 sudo apt-get install postgresql postgresql-contrib<\/strong><\/p>\n<p style=\"padding-left: 60px;\"><strong><br \/><\/strong><\/p>\n<p style=\"padding-left: 60px;\"><strong><br \/><\/strong><\/p>\n<h2>\u2022 Install Nginx<\/h2>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Install Nginx to use it to serve up our static files for our Django app<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 sudo apt-get install nginx<strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<h2>\u2022 Install Gunicorn<\/h2>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">Gunicorn is a very powerful Python WSGI HTTP Server.\u00a0<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Since it is a Python package we need to first activate our virtualenv to install it:<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 source \/opt\/myenv\/bin\/activate<strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Install Gunicorn<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 <strong>pip install gunicorn<strong><br \/><\/strong><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<h2>\u2022 Configure PostgreSQL<\/h2>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 With PostgreSQL we need to create a database, create a user, and grant the user we created access to the database we created.<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 sudo su &#8211; postgres<strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 The terminal prompt should now say &#8220;postgres@yourserver&#8221;. If this is the case, then run this command to create your database:<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 <strong>createdb mydb<strong><br \/><\/strong><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Create your database user with the following command:<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 <\/strong><strong><strong>createuser \u2013P<\/strong><\/strong><\/p>\n<p style=\"padding-left: 60px;\"><strong><strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0You will now be met with a series of 6 prompts. The first one will ask you for the name of the new user. Use whatever name you would like. The next two prompts are for your password and confirmation of password for the new user. For the last 3 prompts just enter &#8220;n&#8221; and hit &#8220;enter&#8221;. This just ensures your new users only have access to what you give them access and to nothing else.<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Now activate the PostgreSQL command line interface like so:<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 p<\/strong><strong>sql<strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Grant this new user access to your new database with this command:<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 <\/strong><strong>GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;<strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Switch back to your user from the postgre user:<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 exit<\/strong><\/p>\n<p style=\"padding-left: 60px;\"><strong><br \/><\/strong><\/p>\n<p style=\"padding-left: 60px;\"><strong><br \/><\/strong><\/p>\n<h2>\u2022 Create a Django Project<\/h2>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Change directories into the directory of your virtualenv<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 cd \/opt\/myenv<strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Activate your virtualenv<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 <strong>source \/opt\/myenv\/bin\/activate<strong><br \/><\/strong><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Run the following command to start a new Django project:<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 <\/strong><strong>django-admin.py startproject myproject<strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0In order for Django to be able to talk to our database we need to install a backend for PostgreSQL.<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Make sure your virtualenv is active and run the following command in order to do this:<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 <\/strong><strong>pip install psycopg2<strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Change directories into the new &#8220;myproject&#8221; directory and then into its subdirectory which is also called &#8220;myproject&#8221; like this:<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 <\/strong><strong>cd \/opt\/myenv\/myproject\/myproject<strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Edit the settings.py file:<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 nano settings.py<\/strong><\/p>\n<p style=\"padding-left: 60px;\"><strong><br \/><\/strong><\/p>\n<p style=\"padding-left: 30px;\"><strong>Sample Configuration<\/strong><\/p>\n<p style=\"padding-left: 30px;\"><strong><br \/><\/strong><\/p>\n<h6 style=\"padding-left: 60px;\">DATABASES = {<br \/>\u00a0\u00a0\u00a0 &#8216;default&#8217;: {<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8216;ENGINE&#8217;: &#8216;django.db.backends.postgresql_psycopg2&#8217;, # Add &#8216;postgresql_psycopg2&#8217;, &#8216;mysql&#8217;,\u00a0 &#8216;sqlite3&#8217; or &#8216;oracle&#8217;.<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8216;NAME&#8217;: &#8216;mydb&#8217;, # Or path to database file if using sqlite3.<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # The following settings are not used with sqlite3:<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8216;USER&#8217;: &#8216;myuser&#8217;,<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8216;PASSWORD&#8217;: &#8216;password&#8217;,<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8216;HOST&#8217;: &#8216;localhost&#8217;, # Empty for localhost through domain sockets or &#8216;127.0.0.1&#8217; for localhost through TCP.<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 &#8216;PORT&#8217;: &#8221;, # Set to empty string for default.<br \/>\u00a0\u00a0\u00a0 }<br \/>}<\/h6>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\"><br style=\"padding-left: 30px;\" \/>o\u00a0\u00a0 Move up one directory so you&#8217;re in your main Django project directory (opt\/myenv\/myproject).<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 <strong><strong>cd \/opt\/myenv\/myproject<\/strong><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Activate your virtualenv if you haven&#8217;t already with the following command:<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 <\/strong><strong>source \/opt\/myenv\/bin\/activate<strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Run the following command so that Django can add its initial configuration and other tables to your database:<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 <\/strong><strong>python manage.py syncdb<strong><strong><br \/><\/strong><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">You should see some output describing what tables were installed, followed by a prompt asking if you want to create a superuser. This is optional and depends on if you will be using Django&#8217;s auth system or the Django admin.<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<h2>\u2022 Configure Gunicorn<\/h2>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 First let\u2019 s just go over running Gunicorn with default settings.<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 gunicorn_django &#8211;bind 0.0.0.0:8000<\/strong><\/p>\n<p style=\"padding-left: 60px;\"><strong><strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">You may provide the domain name or IP of your VPS instead of 0.0.0.0 to bind the server to a specific IP or domain. It&#8217;s a good practice to bind it to your VPS address. Now go to your web browser and visit VPS_IP:8000 and see what you get. You should get the Django welcome screen.<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 If you have a large-scale application on a large VPS then increase the worker processes:<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 <strong>gunicorn_django &#8211;workers=2 &#8211;0.0.0.0:8000<strong><br \/><\/strong><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 If you want to set more options for Gunicorn, then set up a config file that you can call when running Gunicorn. Navigate to the directory of your virtualenv.<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 <\/strong><strong>cd \/opt\/myenv<strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Open your config file<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 <\/strong><strong>sudo nano gunicorn_config.py<strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Add the following contents to the file:<\/p>\n<p style=\"padding-left: 60px;\"><strong><strong><br \/><\/strong><\/strong><\/p>\n<h6 style=\"padding-left: 60px;\">## Add these lines in your exising file<br \/>command = &#8216;\/opt\/myenv\/bin\/gunicorn&#8217;<br \/>pythonpath = &#8216;\/opt\/myenv\/myproject&#8217;<br \/>bind = &#8216;127.0.0.1:8000&#8217;<br \/>workers = 2<br \/>user = nobody<\/h6>\n<p style=\"padding-left: 60px;\"><strong><strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 60px;\">Save and exit the file.<\/p>\n<p style=\"padding-left: 60px;\"><strong><strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 60px;\"><strong><strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 To run the server, Enter the following command:<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 sudo \/opt\/myenv\/bin\/gunicorn -c \/opt\/myenv\/gunicorn_config.py myproject.wsgi<\/strong><\/p>\n<p style=\"padding-left: 60px;\"><strong><br \/><\/strong><\/p>\n<p style=\"padding-left: 30px;\">The &#8216;-c&#8217; flag is used to define the config file path.<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<h2>\u2022 Autostart gunicorn<\/h2>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Install the Supervisor package:<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 sudo apt-get install supervisor<strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Create a script to inititate gunicorn:<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 <strong>sudo nano \/usr\/local\/bin\/run_gunicorn.sh<\/strong><\/strong><\/p>\n<p style=\"padding-left: 60px;\"><strong><strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\"><strong><strong>Sample Config File<\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\"><strong><strong><br \/><\/strong><\/strong><\/p>\n<h6 style=\"padding-left: 60px;\">#!\/bin\/bash<br \/>echo `date`<br \/>sudo \/opt\/myenv\/bin\/gunicorn -c \/opt\/myenv\/gunicorn_config.py myproject.wsgi<\/h6>\n<p style=\"padding-left: 30px;\"><strong><strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Add the program to startup list:<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 nano \/etc\/supervisor\/conf.d\/myproject1_gunicorn.conf<strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\"><strong>Sample File<\/strong><\/p>\n<p style=\"padding-left: 30px;\"><strong><br \/><\/strong><\/p>\n<h6 style=\"padding-left: 60px;\">[program:myproject1_gunicorn]<br \/>command=\/usr\/local\/bin\/run_gunicorn.sh<br \/>autostart=true<br \/>autorestart=true<br \/>stderr_logfile=\/var\/log\/myproject1_gunicorn.err.log<br \/>stdout_logfile=\/var\/log\/myproject1_gunicorn.out.log<\/h6>\n<p style=\"padding-left: 60px;\"><strong><br \/><\/strong><\/p>\n<p style=\"padding-left: 60px;\"><strong><br \/><\/strong><\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Make the supervisor reread the setting and add your task:<\/p>\n<p style=\"padding-left: 60px;\"><strong><strong><strong>\u00a7\u00a0 <strong>sudo supervisorctl reread<\/strong><\/strong><\/strong><\/strong><\/p>\n<p style=\"padding-left: 60px;\"><strong><strong><strong><strong><strong>\u00a7\u00a0 <strong>sudo supervisorctl update<\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/p>\n<p style=\"padding-left: 60px;\"><strong><strong><strong><strong><strong><strong><br \/><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/p>\n<p style=\"padding-left: 60px;\"><strong><strong><strong><strong><strong><strong><br \/><\/strong><\/strong><\/strong><\/strong><\/strong><\/strong><\/p>\n<h2>\u2022 Configure Nginx<\/h2>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Start the Nginx server:<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 sudo service nginx start<strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Since we are setting NGINX to handle static files only we need to set our static files location first. Open your settings.py file for your Django project and edit the STATIC_ROOT line:<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 <strong>STATIC_ROOT = &#8220;\/opt\/myenv\/static\/&#8221;<strong><br \/><\/strong><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Configure NGINX to handle those files. Open up a new NGINX config file with the following command:<\/p>\n<p style=\"padding-left: 60px;\"><strong><strong>\u00a7\u00a0 <\/strong>sudo nano \/etc\/nginx\/sites-available\/myproject<\/strong><\/p>\n<p style=\"padding-left: 60px;\"><strong><br \/><\/strong><\/p>\n<p style=\"padding-left: 30px;\"><strong>Sample File<\/strong><\/p>\n<p style=\"padding-left: 30px;\"><strong><br \/><\/strong><\/p>\n<h6 style=\"padding-left: 60px;\">server {<br \/>\u00a0\u00a0\u00a0 server_name djangoserver.devops;<\/p>\n<p>\u00a0\u00a0\u00a0 access_log off;<\/p>\n<p>\u00a0\u00a0\u00a0 location \/static\/ {<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 alias \/opt\/myenv\/static\/;<br \/>\u00a0\u00a0\u00a0 }<br \/>\u00a0\u00a0 \u00a0<br \/>\u00a0\u00a0\u00a0 location \/ {<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 proxy_pass http:\/\/127.0.0.1:8000;<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 proxy_set_header X-Forwarded-Host $server_name;<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 proxy_set_header X-Real-IP $remote_addr;<br \/>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 add_header P3P &#8216;CP=&#8221;ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV&#8221;&#8216;;<br \/>\u00a0\u00a0\u00a0 }<br \/>}<\/h6>\n<p style=\"padding-left: 60px;\"><strong><br \/><\/strong><\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Now we need to set up a symbolic link in the \/etc\/nginx\/sites directory that points to this configuration file. That is how NGINX knows this site is active. Change directories to \/etc\/nginx\/sites-available like this:<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 cd \/etc\/nginx\/sites-available<\/strong><\/p>\n<p style=\"padding-left: 60px;\"><strong><strong><strong>\u00a7\u00a0 <\/strong>sudo ln -s ..\/sites-available\/myproject<\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Remove the default nginx server block:<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 <strong>sudo rm default<strong><br \/><\/strong><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Now restart nginx<\/p>\n<p style=\"padding-left: 60px;\"><strong>\u00a7\u00a0 <\/strong><strong>sudo service nginx restart<strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<p style=\"padding-left: 30px;\">o\u00a0\u00a0 Navigate to the IP address of your server and you will see the default Django page there:<\/p>\n<p style=\"padding-left: 60px;\"><strong><strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 60px;\"><strong><strong><\/p>\n<div class=\"pivotx-wrapper\"><a href='http:\/\/www.jodohost.com\/blog\/images\/2014-07\/screenshot_037.jpg' class=\"thickbox\" title=\"Django welcome screen\" rel=\"entry-0\" ><img decoding=\"async\" src=\"http:\/\/www.jodohost.com\/blog\/images\/2014-07\/screenshot_037.thumb.jpg\" alt=\"Django welcome screen\" title=\"Django welcome screen\" class='pivotx-popupimage'\/><\/a><\/div>\n<p><\/strong><\/strong><\/p>\n<p style=\"padding-left: 60px;\"><strong><strong><br \/><\/strong><\/strong><\/p>\n<p style=\"padding-left: 30px;\">\u00a0<\/p>\n<h3 style=\"padding-left: 30px;\">We have configured a Django server with Nginx and Gunicorn successfully.<\/h3>\n<p style=\"padding-left: 30px;\"><strong><strong><\/strong><br \/><\/strong><\/p>\n<p> ]]><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u00a0 \u00a0 We&#8217;ll setup and configure the Django server on Ubuntu. We are going to configure it with Nginx and Gunicorn for good compatibility and performance. \u00a0 \u00a0 \u2022 Preparation \u00a0 \u00a0 o\u00a0\u00a0 Make sure that your system is updated before proceeding with the installation. \u00a7\u00a0 sudo apt-get update \u00a0 \u00a0 \u2022 Install and Create [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"http:\/\/www.jodohost.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/29"}],"collection":[{"href":"http:\/\/www.jodohost.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.jodohost.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.jodohost.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.jodohost.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=29"}],"version-history":[{"count":1,"href":"http:\/\/www.jodohost.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/29\/revisions"}],"predecessor-version":[{"id":323,"href":"http:\/\/www.jodohost.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/29\/revisions\/323"}],"wp:attachment":[{"href":"http:\/\/www.jodohost.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=29"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.jodohost.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=29"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.jodohost.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=29"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}