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:
- Install Nginx
- Add an Nginx configuration file to sites-available
- Add a symbolic link of the configuration file to sites-enabled
- Update static file location
- Adjust firewall settings
- 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.
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.
systemctl status nginx
Unless you encounter an issue, you'll see the message below.
● 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.
- nginx.conf file: the original settings are written here. Usually, we don't touch this file.
- 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.
- 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.
- 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.
sudo vim /etc/nginx/sites-available/project_d
Edit the file like in the example below.
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.
- server name: the static IP address attached to the Ubuntu instance
- location /static: the address should be aligned with
STATIC_ROOT
- 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.
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.
sudo nginx -t
If the configuration file is properly written, you'll see the message below.
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.
:
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.
sudo mkdir /usr/share/nginx/html/static
sudo chown -R ubuntu /usr/share/nginx/html/static
cd ~/project_d
source d_env/bin/activate
python manage.py collectstatic
You'll see that the static files are successfully copied to the new directory.
138 static files copied to '/usr/share/nginx/html/static'
As the old static file directory is not needed anymore, delete it.
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.
sudo ufw enable
You'll see the message below. This explains the importance of opening an SSH port to keep the SSH connection.
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.
sudo ufw allow ssh
sudo ufw allow 'Nginx Full'
Run the status command to check the status.
sudo ufw status
You can see that the UFW is active now with two allowed ports settings.
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.