How to Set Up a Web Server on Linux: Nginx vs. Apache
Setting up a web server on Linux is essential for hosting websites, applications, or APIs. Two of the most popular web server software options are Nginx and Apache. But which one should you choose?
In this guide, we’ll compare Nginx vs. Apache, walk you through the installation and configuration of both, and help you decide the best web server for your needs.
1. Nginx vs. Apache: Key Differences
Feature | Nginx | Apache |
---|---|---|
Performance | Faster under high traffic | Good for moderate traffic |
Architecture | Event-driven (efficient for many requests) | Process-based (creates new processes for each connection) |
Static Content Handling | Highly efficient | Slightly slower |
Dynamic Content Handling | Requires external processors (FastCGI, PHP-FPM) | Processes dynamic content natively via modules |
Configuration | Uses block-based configuration (nginx.conf) | Uses .htaccess and httpd.conf |
Best Use Case | High-traffic websites, reverse proxy, microservices | Traditional web hosting, PHP-heavy applications |
Performance Graph: Requests per Second
Requests per Second (Higher is Better) Nginx: ██████████████████████ 80K Apache: ████████████ 40K
2. How to Install and Configure Nginx on Linux
Step 1: Install Nginx
Run the following commands based on your Linux distribution:
On Debian/Ubuntu:
sudo apt update sudo apt install nginx -y
On RHEL/CentOS:
sudo yum install epel-release -y sudo yum install nginx -y
Start and enable Nginx:
sudo systemctl start nginx sudo systemctl enable nginx
Step 2: Configure Nginx Server Block
Create a new server block:
sudo nano /etc/nginx/sites-available/example.com
Add the following configuration:
server { listen 80; server_name example.com www.example.com; root /var/www/example.com; index index.html index.htm; location / { try_files $uri $uri/ =404; } }
Save the file and enable the configuration:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ sudo systemctl restart nginx
3. How to Install and Configure Apache on Linux
Step 1: Install Apache
On Debian/Ubuntu:
sudo apt update sudo apt install apache2 -y
On RHEL/CentOS:
sudo yum install httpd -y
Start and enable Apache:
sudo systemctl start apache2 sudo systemctl enable apache2
Step 2: Configure Apache Virtual Host
Create a new virtual host file:
sudo nano /etc/apache2/sites-available/example.com.conf
Add the following configuration:
ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined
Save the file and enable the new site:
sudo a2ensite example.com sudo systemctl restart apache2
4. Which One Should You Choose?
Your Needs | Best Choice |
---|---|
High-traffic websites | Nginx |
Reverse proxy or load balancing | Nginx |
Hosting PHP applications (WordPress, Laravel) | Apache |
Easy .htaccess configuration | Apache |
Low memory usage and high efficiency | Nginx |
Final Verdict:
- Use Nginx for high performance, load balancing, and modern web services.
- Use Apache for compatibility with legacy applications and .htaccess support.
- Use both (Nginx as a reverse proxy for Apache) for the best of both worlds.
Ready to deploy your web server? Choose Nginx or Apache and start hosting your site today!