Nginx is a powerful and widely used web server known for its performance, stability, and scalability. Whether you’re setting up a basic static website or hosting complex applications, Nginx is a great choice. This guide will walk you through the steps to install and configure Nginx on an Ubuntu server.
Prerequisites
- An Ubuntu server (version 20.04 or newer is recommended).
- A user with
sudo
privileges. - Access to the terminal or SSH to connect to your server.
Step 1: Update the Package List
Before installing any software, ensure your package lists are updated to fetch the latest versions of packages. Run the following commands:
sudo apt update
sudo apt upgrade -y
Step 2: Install Nginx
Ubuntu’s default package repository includes Nginx, so installing it is straightforward. Use the following command:
sudo apt install nginx -y
This installs Nginx along with its dependencies.
Step 3: Start and Enable the Nginx Service
After installation, Nginx may start automatically. To verify its status, use:
sudo systemctl status nginx
If it’s not running, start it manually:
sudo systemctl start nginx
Enable Nginx to start automatically at boot:
sudo systemctl enable nginx
Step 4: Adjust Firewall Settings
If your server has a firewall enabled (like UFW), allow Nginx traffic. Nginx provides predefined profiles for the firewall.
View available profiles:
sudo ufw app list
Allow HTTP and HTTPS traffic:
sudo ufw allow 'Nginx Full'
Verify the changes:
sudo ufw status
Step 5: Verify Installation
Open your browser and navigate to your server’s public IP address (e.g., http://your-server-ip
). You should see the default Nginx welcome page, indicating that Nginx is successfully installed and running.
To find your server’s IP address, use:
ip addr show
Look for an IP address associated with your network interface.
Step 6: Configure Nginx
The main configuration file for Nginx is located at:
/etc/nginx/nginx.conf
For specific site configurations, use the directory:
/etc/nginx/sites-available/
To create a new configuration file for your site:
Create a new file in the sites-available
directory:
sudo nano /etc/nginx/sites-available/your-site
Add the following basic configuration (modify as needed):
server {
listen 80;
server_name your-domain.com www.your-domain.com;
root /var/www/your-site;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
Create a symbolic link to enable the configuration:
sudo ln -s /etc/nginx/sites-available/your-site /etc/nginx/sites-enabled/
Test the Nginx configuration for syntax errors:
sudo nginx -t
Reload Nginx to apply changes:
sudo systemctl reload nginx
Step 7: Manage Nginx (Optional Commands)
Here are additional commands to manage Nginx:
Stop Nginx:
sudo systemctl stop nginx
Restart Nginx:
sudo systemctl restart nginx
Reload Nginx (apply changes without downtime):
sudo systemctl reload nginx
Step 6: Monitor Nginx Processes Using ps aux
After installation, it’s essential to monitor Nginx to ensure it’s running smoothly. The ps aux
command provides a detailed list of processes on your server.
What is ps aux
?
The ps aux
command is a tool that displays active processes, including system services like Nginx. It shows detailed information, such as the process ID (PID), memory and CPU usage, and the command that started the process.
Using ps aux
to Monitor Nginx
To check if Nginx is running:
ps aux | grep nginx
Example Output
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
www-data 1234 0.0 0.5 105432 5236 ? S 10:20 0:01 nginx: worker process
root 5678 0.0 0.3 97856 3220 ? Ss 10:20 0:00 nginx: master process /usr/sbin/nginx
Key Points:
- Master process: The root process managing worker processes.
- Worker processes: Handle client requests.
You can monitor resource usage or troubleshoot by identifying the PID and other details. For instance, to stop a specific Nginx process, use:
sudo kill <PID>
Conclusion
You’ve successfully installed and configured Nginx on your Ubuntu server. From here, you can customize it further to suit your needs, such as setting up SSL/TLS with Let’s Encrypt, configuring reverse proxy for applications, or optimizing for performance.
If you encounter issues, check the Nginx logs for insights:
sudo tail -f /var/log/nginx/error.log
Feel free to explore and harness the full potential of Nginx!