Author:
Ruby Abdullah
Jun 21, 2024
In the world of web servers, Nginx (pronounced “engine-x”) stands out as a powerful and flexible tool used by millions of websites worldwide. It’s known for its high performance, stability, rich feature set, simple configuration, and low resource consumption. Whether you’re a beginner or an experienced developer, this guide will walk you through setting up Nginx and deploying your first web application.
What is Nginx?
Nginx is an open-source web server that can also be used as a reverse proxy, load balancer, and HTTP cache. It is designed to handle high traffic with efficiency and speed, making it a popular choice for modern web applications.
Setting Up Nginx
Prerequisites
Before you start, ensure you have the following:
1. A server running a Unix-based OS (like Ubuntu or CentOS).
2. Root access or a user with sudo privileges.
3. Basic knowledge of the command line.
Step 1: Install Nginx
The installation process for Nginx is straightforward. Let’s go through the steps for Ubuntu and CentOS.
For Ubuntu:
1. Update your package list:
sudo apt update
2. Install Nginx:
sudo apt install nginx
3. Start and enable Nginx to run on boot:
sudo systemctl start nginx
sudo systemctl enable nginx
For CentOS:
1. Add the EPEL repository:
sudo yum install epel-release
2. Install Nginx:
sudo yum install nginx
3. Start and enable Nginx to run on boot:
sudo systemctl start nginx
sudo systemctl enable nginx
Step 2: Verify Installation
After installing Nginx, verify the installation by navigating to your server’s IP address in a web browser. You should see the default Nginx welcome page.
Configuring Nginx
Nginx’s configuration files are typically located in `/etc/nginx/`. The main configuration file is `nginx.conf`, and individual site configurations are located in `/etc/nginx/sites-available/` with symbolic links to `/etc/nginx/sites-enabled/`.
Step 3: Basic Configuration
1. Navigate to the Nginx configuration directory:
cd /etc/nginx/
2. Open the main configuration file:
sudo nano nginx.conf
3. Ensure the following lines are included in your configuration:
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/sites-enabled/*;
}
Step 4: Create a Server Block
Server blocks (similar to virtual hosts in Apache) allow you to host multiple websites on a single server.
Create a new configuration file for your site:
sudo nano /etc/nginx/sites-available/yourdomain.com
2. Add the following configuration:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/yourdomain.com/html;
index index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ =404;
}
}
3. Create the root directory for your website:
sudo mkdir -p /var/www/yourdomain.com/html
4. Assign ownership of the directory:
sudo chown -R $USER:$USER /var/www/yourdomain.com/html
5. Create a sample HTML file:
echo “<h1>Welcome to yourdomain.com!</h1>” > /var/www/yourdomain.com/html/index.html
6. Enable the server block by creating a symbolic link:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
7. Test your configuration for syntax errors:
sudo nginx -t
8. Reload Nginx to apply the changes:
sudo systemctl reload nginx
Deploying Your First Application
Now that Nginx is configured, let’s deploy a simple web application.
Step 5: Deploying a Static Website
For a static website, you simply need to place your HTML, CSS, and JavaScript files in the root directory specified in your server block.
1. Navigate to your website’s root directory:
cd /var/www/yourdomain.com/html
2. Add your static files (HTML, CSS, JS) to this directory.
3. Open your browser and navigate to your domain (http://yourdomain.com) to see your static website live.
Step 6: Deploying a Dynamic Application
For dynamic applications (e.g., those built with Node.js, Python, etc.), you will need to configure Nginx as a reverse proxy.
1. Install your application dependencies and start the application on a specific port (e.g., 3000 for a Node.js app).
2. Modify your server block to include a reverse proxy configuration:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection ‘upgrade’;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
3. Reload Nginx to apply the changes:
sudo systemctl reload nginx
4. Navigate to your domain in the browser to see your dynamic application live.
Conclusion
Nginx is a robust and versatile tool for serving static and dynamic content. By following this guide, you’ve learned how to install Nginx, configure basic server blocks, and deploy both static and dynamic web applications. With this knowledge, you’re well on your way to building efficient and scalable web infrastructure. Happy hosting!