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:
Note: For the Nginx setting, the virtual environment is not needed as it's directly installed on the OS.
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>
:
Nginx has several configuration setting points.
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.
STATIC_ROOT
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 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.
p rm -r collectstatic_path
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)'
If all settings are properly done, you'll be able to see your app through the static IP address.
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:
Note: For the Nginx setting, the virtual environment is not needed as it's directly installed on the OS.
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>
:
Nginx has several configuration setting points.
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.
STATIC_ROOT
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 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.
p rm -r collectstatic_path
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)'
If all settings are properly done, you'll be able to see your app through the static IP address.