Setting Up a Web Server in Linux – Apache & Nginx
🔹 Introduction
Whether you’re a Linux enthusiast, a sysadmin, or a web developer, setting up a web server is a foundational skill. Apache and Nginx are the two most widely used web servers in the world, powering millions of websites. This guide walks you through:
- Installing and configuring Apache and Nginx
- Hosting multiple websites (virtual hosts)
- Enabling SSL with Let’s Encrypt
- Optimizing performance and hardening security
- Choosing between Apache, Nginx, or both together
By the end of this guide, you’ll be fully capable of deploying and managing production-ready web servers.

📝Table Of Content:
- 🧰 1. What is a Web Server?
- 🖥️ 2. Pre-requisites for Setting Up a Web Server
- 📥 3. Installing Apache on Linux
- ⚙️ 4. Configuring Apache
- 🌐 5. Installing Nginx on Linux
- ⚙️ 6. Configuring Nginx
- 🛡️ 7. Enabling HTTPS with Let’s Encrypt
- 🧪 8. Apache vs Nginx – When to Use What?
- 🚀 9. Bonus: Nginx as a Reverse Proxy for Apache
- 🚀 10. Performance Optimization Tips
- 🔐 11. Web Server Security Hardening
- 🧠 12. Monitoring and Logs
- 📦 13. Hosting Multiple Websites (Virtual Hosts / Server Blocks)
- 🙋♂️ Frequently Asked Questions (FAQs)
- 🔚 14. Conclusion: You’re Now a Linux Web Server Ninja!
- 📚 Learn More:
- 📣 Call to Action
🧰 1. What is a Web Server?
A web server is software that serves web content (like HTML, CSS, and images) to users who access it via their browser. It receives HTTP/HTTPS requests and returns the appropriate response.
Popular Linux-based web servers include:
- Apache (httpd) – Flexible, module-rich, widely used.
- Nginx (engine-x) – Fast, lightweight, and optimized for handling many concurrent connections.
🖥️ 2. Pre-requisites for Setting Up a Web Server
Before diving in, ensure:
- You have a Linux system (Ubuntu, Debian, CentOS, or similar).
- You have root/sudo access.
- Basic knowledge of terminal commands.
- A domain name (optional for now but useful for SSL).
📥 3. Installing Apache on Linux
🔧 3.1 Apache Installation (Ubuntu/Debian)
sudo apt update
sudo apt install apache2
🔧 3.2 Apache Installation (CentOS/RHEL)
sudo yum install httpd
sudo systemctl enable httpd
sudo systemctl start httpd
🧪 3.3 Verify Installation
Visit your server’s IP in a browser:http://your-server-ip
You should see the Apache welcome page.
⚙️ 4. Configuring Apache
🗂️ 4.1 Apache Directory Structure
Path | Description |
---|---|
/etc/apache2/ | Main configuration directory (Debian) |
/etc/httpd/ | Main config on RHEL-based systems |
/var/www/html | Default web root directory |
/etc/apache2/sites-available/ | Virtual host files |
📝 4.2 Creating a Virtual Host (example.com)
sudo mkdir -p /var/www/example.com/public_html
sudo nano /etc/apache2/sites-available/example.com.conf
Add:
<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
✅ 4.3 Enable and Restart
sudo a2ensite example.com.conf
sudo systemctl reload apache2
🌐 5. Installing Nginx on Linux
🔧 5.1 Nginx Installation (Ubuntu/Debian)
sudo apt update
sudo apt install nginx
🔧 5.2 Nginx Installation (CentOS/RHEL)
sudo yum install epel-release
sudo yum install nginx
sudo systemctl enable nginx
sudo systemctl start nginx
🧪 5.3 Verify Nginx
Visit your server IP:http://your-server-ip
You should see the Nginx welcome page.
⚙️ 6. Configuring Nginx
🗂️ 6.1 Nginx Directory Overview
Path | Description |
---|---|
/etc/nginx/ | Main Nginx directory |
/etc/nginx/sites-available/ | Available server blocks |
/etc/nginx/sites-enabled/ | Enabled server blocks |
/var/www/html/ | Default web root |
📝 6.2 Creating a Server Block
sudo mkdir -p /var/www/example.com/html
sudo nano /etc/nginx/sites-available/example.com
Add:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Enable:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
🛡️ 7. Enabling HTTPS with Let’s Encrypt
🔧 7.1 Install Certbot
sudo apt install certbot python3-certbot-nginx # For Nginx
sudo apt install certbot python3-certbot-apache # For Apache
🔒 7.2 Generate SSL Certificate
Apache:
sudo certbot --apache
Nginx:
sudo certbot --nginx
Certificates are auto-renewed via cron.
🧪 8. Apache vs Nginx – When to Use What?
Feature | Apache | Nginx |
---|---|---|
Architecture | Process-based | Event-based |
Static Content | Good | Excellent |
Dynamic Content | Native (via PHP module) | Needs external processor |
Performance | High with tuning | High out-of-the-box |
Configuration | .htaccess files | Central config only |
Use Case | Dynamic-heavy apps (e.g. WordPress) | Static-heavy, high-concurrency |
📝 Tip: You can also use Apache + Nginx together, where Nginx acts as a reverse proxy in front of Apache.
🚀 9. Bonus: Nginx as a Reverse Proxy for Apache
Install both Apache and Nginx, and configure Nginx to forward traffic to Apache (running on a different port, like 8080).
🔁 Nginx Reverse Proxy Example
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
🚀 10. Performance Optimization Tips
- Enable Gzip compression
- Use Browser caching
- Enable HTTP/2
- Use a Content Delivery Network (CDN)
- Limit resource-heavy modules
- Use load balancing (via Nginx or HAProxy)
🔐 11. Web Server Security Hardening
- Disable directory listing
- Disable unnecessary modules
- Limit file upload size
- Set strong permissions:
chown -R www-data:www-data /var/www/
- Use firewalls like
ufw
oriptables
🧠 12. Monitoring and Logs
🔎 Apache Logs:
/var/log/apache2/access.log
/var/log/apache2/error.log
🔎 Nginx Logs:
/var/log/nginx/access.log
/var/log/nginx/error.log
Use tail -f
to watch logs live:
tail -f /var/log/nginx/access.log
📦 13. Hosting Multiple Websites (Virtual Hosts / Server Blocks)
Both Apache and Nginx support multiple websites on the same server using virtual hosts (Apache) or server blocks (Nginx).
Repeat the same structure for:
site1.com
site2.com
site3.com
🙋♂️ Frequently Asked Questions (FAQs)
❓ What is the difference between Apache and Nginx?
Answer:
Apache and Nginx are both powerful open-source web servers. Apache uses a process-driven approach and is highly compatible with .htaccess and older web technologies. Nginx, on the other hand, uses an event-driven, asynchronous architecture, making it faster and more efficient, especially under high traffic. Nginx is often used as a reverse proxy due to its speed and low resource consumption.
❓ Can I use both Apache and Nginx together on the same server?
Answer:
Yes, you can use Nginx and Apache together. A common setup is to use Nginx as a reverse proxy in front of Apache. Nginx handles client requests and passes them to Apache running on a different port. This setup combines the flexibility of Apache with the performance benefits of Nginx.
❓ Which is better for WordPress – Apache or Nginx?
Answer:
Both can run WordPress well, but Apache offers easier integration with .htaccess-based WordPress plugins. Nginx, however, provides better performance and is often preferred for high-traffic sites. Some WordPress hosting providers use Nginx as a reverse proxy with Apache in the backend.
❓ How do I secure my Apache or Nginx web server?
Answer:
To secure your web server:
- Use Fail2ban to prevent brute-force attacks
- Use HTTPS with a valid SSL/TLS certificate (via Let’s Encrypt)
- Disable unnecessary modules
- Use firewalls like UFW or firewalld
- Disable directory listing
- Configure secure permissions for files and directories
- Use Fail2ban to prevent brute-force attacks
❓ What is a virtual host or server block?
Answer:
A virtual host (Apache) or server block (Nginx) allows you to host multiple websites on a single server by assigning each site a unique configuration. This enables you to run multiple domains (like example.com and blog.example.com) on the same IP address.
❓ Do I need a domain name to set up a web server?
Answer:
You can test and run a web server using your server’s IP address, but to serve real users and set up HTTPS (SSL), having a domain name is highly recommended.
❓ How can I check if Apache or Nginx is running?
Answer:
Use the following commands:
sudo systemctl status apache2 # For Apache on Debian/Ubuntu
sudo systemctl status httpd # For Apache on CentOS/RHEL
sudo systemctl status nginx # For Nginx
❓ What ports do Apache and Nginx use?
Answer:
By default:
- HTTP traffic uses port 80
- HTTPS traffic uses port 443
If using both Apache and Nginx together, you can configure Apache to run on port 8080, and Nginx will continue to listen on port 80 or 443 as a proxy.
❓ How do I renew my SSL certificate from Let’s Encrypt?
Answer:
You can use the command:
sudo certbot renew
To test the renewal without making any changes:
sudo certbot renew --dry-run
❓ Is setting up a web server in Linux suitable for production websites?
Answer:
Absolutely. Linux is one of the most stable and secure platforms for hosting production websites. With proper configuration, regular updates, and security hardening, you can run small to enterprise-grade websites reliably.
🔚 14. Conclusion: You’re Now a Linux Web Server Ninja!
By now you’ve learned:
✅ How to install, configure, and secure both Apache and Nginx
✅ Host multiple domains with virtual hosts/server blocks
✅ Set up SSL, reverse proxy, and optimize performance
✅ Choose the right web server based on your project’s needs
💬 Next Steps
- Try hosting your first live project!
- Set up a free domain with tools like Freenom.
- Explore PHP-FPM or Node.js with Nginx next!
📚 Learn More:
📣 Call to Action
Enjoyed the post? 🎉
✅ Share it with your team
✅ Bookmark for future reference
✅ Subscribe to TechnoPS.com for more daily deep-dives into Linux topics!