Wednesday, 5 February 2025

How to Install and Set Up Apache or Nginx on a Linux VPS Like a Pro

When it comes to hosting a website or web application on a Linux VPS, choosing the right web server is crucial. Apache and Nginx are the two most widely used web servers, each offering unique benefits for different use cases. Apache is known for its flexibility and compatibility, while Nginx is celebrated for its high performance and ability to handle large traffic loads efficiently.

In this guide, we’ll walk you through the step-by-step process of installing and configuring Apache or Nginx on a Linux VPS. Whether you’re setting up a personal blog, an e-commerce store, or a corporate website, this tutorial will help you get your web server up and running smoothly.

If you don’t have a VPS yet, you can check out 99RDP, a trusted provider of high-performance Linux VPS solutions. Their servers offer reliable uptime, robust security, and excellent customer support, making them ideal for hosting your website or application.

1. Prerequisites

Before we dive into the installation process, make sure you have the following:

A Linux VPS – You can get an affordable and high-performance VPS from 99RDP.
SSH access – You need to connect to your VPS using SSH (via PuTTY or Terminal).
A user with sudo privileges – Root access is required for installation.
A registered domain name (optional but recommended for website hosting).

2. Choosing Between Apache and Nginx

Before installing a web server, it's important to understand the key differences between Apache and Nginx:

Feature Apache Nginx
Performance Good for small to medium sites Excellent for high-traffic sites
Concurrency Thread-based handling Event-driven, handles multiple requests efficiently
Configuration Supports .htaccess Uses server blocks
Reverse Proxy Possible, but not optimal Designed for reverse proxying
Flexibility Highly customizable Optimized for speed and scalability

Which One Should You Choose?

  • Choose Apache if you need .htaccess support, ease of use, and compatibility with many applications.
  • Choose Nginx if you need better performance, scalability, and efficient resource management.

Now, let’s proceed with installing your preferred web server.

3. Installing Apache on a Linux VPS

For Ubuntu/Debian

Step 1: Update System Packages

Before installing Apache, update your system to ensure you have the latest packages:

sudo apt update && sudo apt upgrade -y

Step 2: Install Apache

Install Apache using the following command:

sudo apt install apache2 -y

Step 3: Start and Enable Apache

Once installed, start the Apache service and enable it to launch on boot:

sudo systemctl start apache2
sudo systemctl enable apache2

Step 4: Verify Installation

Check if Apache is running with:

systemctl status apache2

Step 5: Allow Apache Through the Firewall

If you have UFW (Uncomplicated Firewall) enabled, allow Apache traffic:

sudo ufw allow 'Apache Full'

Step 6: Test Apache Installation

Open your browser and enter your VPS IP address (http://your-vps-ip). If Apache is running correctly, you’ll see the default Apache welcome page.

For CentOS/RHEL

Step 1: Install Apache

sudo yum install httpd -y

Step 2: Start and Enable Apache

sudo systemctl start httpd
sudo systemctl enable httpd

Step 3: Allow Apache Through the Firewall

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

Step 4: Verify Installation

systemctl status httpd

Apache is now installed and running on your Linux VPS! 🚀

4. Installing Nginx on a Linux VPS

For Ubuntu/Debian

Step 1: Update System Packages

Before installing Nginx, update your system to get the latest package lists:

sudo apt update && sudo apt upgrade -y

Step 2: Install Nginx

Run the following command to install Nginx:

sudo apt install nginx -y

Step 3: Start and Enable Nginx

After the installation is complete, start Nginx and enable it to launch on boot:

sudo systemctl start nginx
sudo systemctl enable nginx

Step 4: Verify Installation

Check if Nginx is running with:

systemctl status nginx

Step 5: Allow Nginx Through the Firewall

If you are using UFW, allow HTTP and HTTPS traffic:

sudo ufw allow 'Nginx Full'

Step 6: Test Nginx Installation

Open your web browser and enter your VPS IP address (http://your-vps-ip). If Nginx is running correctly, you’ll see the default Nginx welcome page.

For CentOS/RHEL

Step 1: Install Nginx

First, install the EPEL repository and then install Nginx:

sudo yum install epel-release -y
sudo yum install nginx -y

Step 2: Start and Enable Nginx

sudo systemctl start nginx
sudo systemctl enable nginx

Step 3: Allow Nginx Through the Firewall

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

Step 4: Verify Installation

systemctl status nginx

Nginx is now successfully installed and running on your Linux VPS! 🎉

5. Configuring Apache or Nginx

Now that you have installed Apache or Nginx, let's configure them to serve websites properly.

Apache Configuration Basics

Creating a Virtual Host (For Hosting Multiple Websites)

  1. Navigate to the Apache sites directory:
    cd /etc/apache2/sites-available/
    
  2. Create a new virtual host file:
    sudo nano mywebsite.conf
    
  3. Add the following configuration (replace mywebsite.com with your domain):
    <VirtualHost *:80>
        ServerAdmin admin@mywebsite.com
        ServerName mywebsite.com
        ServerAlias www.mywebsite.com
        DocumentRoot /var/www/mywebsite
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    
  4. Save and exit the file.
  5. Enable the virtual host and restart Apache:
    sudo a2ensite mywebsite.conf
    sudo systemctl restart apache2
    

Nginx Configuration Basics

Creating a Server Block (Virtual Host for Hosting Multiple Websites)

  1. Navigate to the Nginx configuration directory:
    cd /etc/nginx/sites-available/
    
  2. Create a new server block file:
    sudo nano mywebsite
    
  3. Add the following configuration (replace mywebsite.com with your domain):
    server {
        listen 80;
        server_name mywebsite.com www.mywebsite.com;
        root /var/www/mywebsite;
        index index.html index.htm;
        access_log /var/log/nginx/mywebsite_access.log;
        error_log /var/log/nginx/mywebsite_error.log;
    }
    
  4. Save and exit the file.
  5. Enable the server block and restart Nginx:
    sudo ln -s /etc/nginx/sites-available/mywebsite /etc/nginx/sites-enabled/
    sudo systemctl restart nginx
    

Now, your website is properly configured on Apache or Nginx! 🚀

6. Setting Up a Firewall for Security

To enhance your server security, configure UFW (Uncomplicated Firewall):

sudo ufw allow 'Apache Full'  # For Apache
sudo ufw allow 'Nginx Full'  # For Nginx
sudo ufw enable
sudo ufw status

For CentOS/RHEL, use firewalld:

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

7. Enabling SSL with Let’s Encrypt (HTTPS Setup)

To secure your website, you should enable SSL with Let’s Encrypt.

Step 1: Install Certbot

For Apache:

sudo apt install certbot python3-certbot-apache -y

For Nginx:

sudo apt install certbot python3-certbot-nginx -y

Step 2: Obtain and Install an SSL Certificate

For Apache:

sudo certbot --apache

For Nginx:

sudo certbot --nginx

Step 3: Set Up Automatic Renewal

sudo certbot renew --dry-run

Now, your website is secured with HTTPS! 🔒

8. Performance Optimization Tips

  • Enable Gzip Compression

    sudo nano /etc/nginx/nginx.conf  # For Nginx
    sudo nano /etc/apache2/apache2.conf  # For Apache
    

    Add this inside the configuration file:

    gzip on;
    gzip_types text/plain application/json;
    

    Restart the server:

    sudo systemctl restart nginx  # or apache2
    
  • Use a Caching Mechanism (e.g., FastCGI cache for Nginx, Varnish for Apache).

9. Troubleshooting Common Issues

Here are some common problems and how to fix them:

🔴 Apache/Nginx Not Starting?

  • Run:
    sudo systemctl status apache2  # For Apache
    sudo systemctl status nginx  # For Nginx
    

🔴 Port Conflict Issues?

  • Check which service is using the port:
    sudo netstat -tulnp | grep ':80'
    

🔴 Permission Issues?

  • Fix ownership and permissions:
    sudo chown -R www-data:www-data /var/www/
    sudo chmod -R 755 /var/www/
    

🔴 Check Logs for Errors

sudo tail -f /var/log/apache2/error.log  # Apache
sudo tail -f /var/log/nginx/error.log  # Nginx

10. Conclusion

Congratulations! You have successfully installed and configured Apache or Nginx on your Linux VPS. 🎉

If you need high-performance VPS hosting, check out 99RDP for affordable, reliable, and fast VPS solutions tailored for web hosting.

0 comments:

Post a Comment

Popular Posts

Blog Archive