Linux 115: Setting Up a Web Server with Apache or Nginx
Hosting a website or web application on a Linux server requires a reliable and performant web server. In this article, you'll learn how to install, configure, and manage two of the most popular web servers on Linux: Apache (httpd) and Nginx.
1. Introduction to Web Servers
A web server is software that handles HTTP(S) requests and serves web content (HTML, CSS, JS, etc.) to users' browsers.
- Apache: A mature, flexible server with powerful modules and widespread usage.
- Nginx: Lightweight, high-performance server known for its speed and ability to handle many concurrent connections.
2. Installing Apache
On Debian/Ubuntu
On RHEL/CentOS/Fedora
Start and Enable Apache
Verify Installation
Visit http://your_server_ip
in a browser. You should see the default Apache welcome page.
3. Installing Nginx
On Debian/Ubuntu
On RHEL/CentOS/Fedora
Start and Enable Nginx
Verify Installation
Visit http://your_server_ip
— you should see the Nginx welcome page.
4. Basic Web Server Configuration
Apache Configuration Files
-
Main config:
/etc/apache2/apache2.conf
or/etc/httpd/conf/httpd.conf
-
Virtual Hosts:
/etc/apache2/sites-available/
(Debian) or/etc/httpd/conf.d/
(RHEL)
Example Virtual Host (Debian-based):
Enable and reload:
Nginx Configuration Files
- Main config:
/etc/nginx/nginx.conf
- Server blocks (similar to virtual hosts):
/etc/nginx/sites-available/
(Debian) or/etc/nginx/conf.d/
(RHEL)
Example server block:
Enable and reload:
5. Serving Content
Default Web Root
-
Apache:
/var/www/html
-
Nginx:
/usr/share/nginx/html
or/var/www/html
Place your website files (e.g., index.html
) in the appropriate directory.
6. Firewall Configuration
Allow web traffic:
Or open port 80 manually:
7. Enable HTTPS (SSL/TLS)
Use Certbot to enable HTTPS with Let’s Encrypt.
Install Certbot
Obtain and Install a Certificate
Set up auto-renewal:
8. Apache vs. Nginx: When to Use Which
Feature | Apache | Nginx |
---|---|---|
Performance | Good, less with many users | Excellent concurrency |
Configuration Style | .htaccess + conf files | Clean and concise conf files |
Dynamic Content | Built-in PHP handling | Uses PHP-FPM |
Reverse Proxy | Possible | Very strong |
Use Apache if:
- You rely on
.htaccess
- Compatibility with legacy software matters
Use Nginx if:
- You want speed and scalability
- You're building a modern web app
9. Conclusion
Apache and Nginx are both excellent web servers, each with its strengths. Choosing the right one depends on your project needs. With basic setup and configuration, you can now serve websites, host apps, or set up reverse proxies efficiently on your Linux server.
Next Steps:
- Learn to configure Apache with PHP and MySQL (LAMP stack)
- Set up Nginx as a reverse proxy for Node.js or Python apps
- Use load balancing and caching with Nginx for performance
Up next: Linux 116: Configuring SSH for Secure Remote Access