Chapter 6. Deploy Django App

Web Server Setup – Nginx

Web Server Setup – Nginx
Tag:

Nginx is one of the most popular web servers. With a focus on static content handling, Nginx can be more scalable and faster than other web servers. This lesson will explain how to set up Nginx for Django app deployment.

There are five steps to set up Nginx:

  1. Install Nginx
  2. Add an Nginx configuration file to sites-available
  3. Add a symbolic link of the configuration file to sites-enabled
  4. Update static file location
  5. Adjust firewall settings
  6. Check if the Django app is running

Note: For the Nginx setting, the virtual environment is not needed as it's directly installed on the OS.

Install Nginx

Install Nginx using the apt-get command.

Command Line - INPUT
sudo apt-get update
sudo apt-get install -y nginx

As Nginx runs when it is installed, check if it is running by the systemctl status command.

Command Line - INPUT
systemctl status nginx

Unless you encounter an issue, you'll see the message below.

Command Line - RESPONSE
 nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2023-04-21 16:05:47 UTC; 21h ago
Docs: man:nginx(8)
Process: 123945 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (cod>
:

Add an Nginx configuration file

Nginx has several configuration setting points.

  1. nginx.conf file: the original settings are written here. Usually, we don't touch this file.
  2. conf.d directory: you can save your custom configuration file in this directory. The nginx.conf file will include your custom configuration. As the configuration files saved in this directory are always read by Nginx, there is limited flexibility to switch configurations.
  3. sites-available directory: this is another directory where you can save your custom configuration files; however, Nginx does not read the files directly. To enable the configuration, you need to create a symbolic link of the file in the sites-enabled directory. By following this rule, you can easily switch configuration files by controlling symbolic links.
  4. sites-enabled directory: the location where you can put the symbolic link of the configuration file that was created in the sites-available directory.

To create a new configuration file, run the command below.

Command Line - INPUT
sudo vim /etc/nginx/sites-available/project_d

Edit the file like in the example below.

/etc/nginx/sites-available/ project_d
server {
listen 80;
server_name xx.xx.xx.xx;

  location = /favicon.ico { access_log off; log_not_found off;}
  location /static{
  alias /usr/share/nginx/html/static;
}

location / {
  include proxy_params;
  proxy_pass http://unix:/home/ubuntu/project_d/project_d.sock;
  }
}

There are three key parts that you need to type carefully.

  1. server name: the static IP address attached to the Ubuntu instance
  2. location /static: the address should be aligned with STATIC_ROOT
  3. proxy_pass: the path of the sock file defined in the Gunicorn service and socket unit files in the previous section

Add a symbolic link and verify the syntax

To enable the new settings, you need to create a symbolic link of the file in the sites-enabled directory. Run the command below to create the symbolic link.

Command Line - INPUT
sudo ln -s /etc/nginx/sites-available/project_d /etc/nginx/sites-enabled 

Use the Nginx command to check if the configuration file is aligned with the Nginx syntax.

Command Line - INPUT
sudo nginx -t

If the configuration file is properly written, you'll see the message below.

Command Line - RESPONSE
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Update static file location

Update the static file location to the directory that Nginx usually uses. To update the location, edit STATIC_ROOT in the settings file and run the collectstatic command.

Update STATIC_ROOT to the directory path we used in the Nginx configuration file.

sconfig/settings/production.py
:
STATIC_ROOT = '/usr/share/nginx/html/static'

Create the static directory and change the owner to ubuntu (the user of the instance). Then, run the collectstatic command under the virtual environment.

Command Line - INPUT
sudo mkdir /usr/share/nginx/html/static
sudo chown -R ubuntu /usr/share/nginx/html/static
cd ~/project_d
source d_env/bin/activate
Command Line - INPUT
python manage.py collectstatic

You'll see that the static files are successfully copied to the new directory.

Command Line - RESPONSE
138 static files copied to '/usr/share/nginx/html/static'

As the old static file directory is not needed anymore, delete it.

Command Line - INPUT
p rm -r collectstatic_path

Firewall settings

This process is optional as AWS Lightsail has its firewall. On Ubuntu OS, you can use UFW (Uncomplicated Firewall) to manage iptables.

To enable UFW, run the command below.

Command Line - INPUT
sudo ufw enable

You'll see the message below. This explains the importance of opening an SSH port to keep the SSH connection.

Command Line - INTERACTIVE
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup

Open the SSH port and ports for Nginx by running the following commands.

Command Line - INPUT
sudo ufw allow ssh
sudo ufw allow 'Nginx Full'

Run the status command to check the status.

Command Line - INPUT
sudo ufw status

You can see that the UFW is active now with two allowed ports settings.

Command Line - RESPONSE
Status: active

To Action From
-- ------ ----
22 ALLOW Anywhere 
Nginx Full ALLOW Anywhere 

For more details about UFW, please check our 'Linux OS Introduction' course.

Linux OS Introduction 'UFW (Uncomplicated Firewall)'

Check if the app is running

If all settings are properly done, you'll be able to see your app through the static IP address.

Web-Server-Setup--Nginx

Nginx is one of the most popular web servers. With a focus on static content handling, Nginx can be more scalable and faster than other web servers. This lesson will explain how to set up Nginx for Django app deployment.

There are five steps to set up Nginx:

  1. Install Nginx
  2. Add an Nginx configuration file to sites-available
  3. Add a symbolic link of the configuration file to sites-enabled
  4. Update static file location
  5. Adjust firewall settings
  6. Check if the Django app is running

Note: For the Nginx setting, the virtual environment is not needed as it's directly installed on the OS.

Install Nginx

Install Nginx using the apt-get command.

Command Line - INPUT
sudo apt-get update
sudo apt-get install -y nginx

As Nginx runs when it is installed, check if it is running by the systemctl status command.

Command Line - INPUT
systemctl status nginx

Unless you encounter an issue, you'll see the message below.

Command Line - RESPONSE
 nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2023-04-21 16:05:47 UTC; 21h ago
Docs: man:nginx(8)
Process: 123945 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (cod>
:

Add an Nginx configuration file

Nginx has several configuration setting points.

  1. nginx.conf file: the original settings are written here. Usually, we don't touch this file.
  2. conf.d directory: you can save your custom configuration file in this directory. The nginx.conf file will include your custom configuration. As the configuration files saved in this directory are always read by Nginx, there is limited flexibility to switch configurations.
  3. sites-available directory: this is another directory where you can save your custom configuration files; however, Nginx does not read the files directly. To enable the configuration, you need to create a symbolic link of the file in the sites-enabled directory. By following this rule, you can easily switch configuration files by controlling symbolic links.
  4. sites-enabled directory: the location where you can put the symbolic link of the configuration file that was created in the sites-available directory.

To create a new configuration file, run the command below.

Command Line - INPUT
sudo vim /etc/nginx/sites-available/project_d

Edit the file like in the example below.

/etc/nginx/sites-available/ project_d
server {
listen 80;
server_name xx.xx.xx.xx;

  location = /favicon.ico { access_log off; log_not_found off;}
  location /static{
  alias /usr/share/nginx/html/static;
}

location / {
  include proxy_params;
  proxy_pass http://unix:/home/ubuntu/project_d/project_d.sock;
  }
}

There are three key parts that you need to type carefully.

  1. server name: the static IP address attached to the Ubuntu instance
  2. location /static: the address should be aligned with STATIC_ROOT
  3. proxy_pass: the path of the sock file defined in the Gunicorn service and socket unit files in the previous section

Add a symbolic link and verify the syntax

To enable the new settings, you need to create a symbolic link of the file in the sites-enabled directory. Run the command below to create the symbolic link.

Command Line - INPUT
sudo ln -s /etc/nginx/sites-available/project_d /etc/nginx/sites-enabled 

Use the Nginx command to check if the configuration file is aligned with the Nginx syntax.

Command Line - INPUT
sudo nginx -t

If the configuration file is properly written, you'll see the message below.

Command Line - RESPONSE
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Update static file location

Update the static file location to the directory that Nginx usually uses. To update the location, edit STATIC_ROOT in the settings file and run the collectstatic command.

Update STATIC_ROOT to the directory path we used in the Nginx configuration file.

sconfig/settings/production.py
:
STATIC_ROOT = '/usr/share/nginx/html/static'

Create the static directory and change the owner to ubuntu (the user of the instance). Then, run the collectstatic command under the virtual environment.

Command Line - INPUT
sudo mkdir /usr/share/nginx/html/static
sudo chown -R ubuntu /usr/share/nginx/html/static
cd ~/project_d
source d_env/bin/activate
Command Line - INPUT
python manage.py collectstatic

You'll see that the static files are successfully copied to the new directory.

Command Line - RESPONSE
138 static files copied to '/usr/share/nginx/html/static'

As the old static file directory is not needed anymore, delete it.

Command Line - INPUT
p rm -r collectstatic_path

Firewall settings

This process is optional as AWS Lightsail has its firewall. On Ubuntu OS, you can use UFW (Uncomplicated Firewall) to manage iptables.

To enable UFW, run the command below.

Command Line - INPUT
sudo ufw enable

You'll see the message below. This explains the importance of opening an SSH port to keep the SSH connection.

Command Line - INTERACTIVE
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup

Open the SSH port and ports for Nginx by running the following commands.

Command Line - INPUT
sudo ufw allow ssh
sudo ufw allow 'Nginx Full'

Run the status command to check the status.

Command Line - INPUT
sudo ufw status

You can see that the UFW is active now with two allowed ports settings.

Command Line - RESPONSE
Status: active

To Action From
-- ------ ----
22 ALLOW Anywhere 
Nginx Full ALLOW Anywhere 

For more details about UFW, please check our 'Linux OS Introduction' course.

Linux OS Introduction 'UFW (Uncomplicated Firewall)'

Check if the app is running

If all settings are properly done, you'll be able to see your app through the static IP address.

Web-Server-Setup--Nginx

Tag: