Every online project, from a personal portfolio to a dynamic web application, relies on a fundamental piece of technology: the web server. It’s the engine that delivers your website’s content to visitors’ browsers. Mastering a web server setup on your VPS (Virtual Private Server) gives you full control over your environment, performance, and security.
This guide provides a clear, step-by-step process for installing and configuring either Nginx or Apache, the two most popular web servers, on a Linux-based VPS. We’ll cover everything from initial server preparation to securing your site with HTTPS.
Step 1: Initial Server Preparation
Before you begin the installation, it’s crucial to prepare your VPS for the new software.
- Connect via SSH: Use a terminal on your local machine to securely connect to your VPS.
ssh username@your_server_ip
- Update Your System: Always start by ensuring all existing packages are up to date. This is a best practice for both security and stability.
- For Ubuntu/Debian:
sudo apt update && sudo apt upgrade
- For CentOS/RHEL:
sudo yum update
- For Ubuntu/Debian:
- Set Up a Firewall: A firewall is your server’s first line of defense. It controls which network traffic is allowed to enter and exit.
- For Ubuntu, using UFW:
sudo ufw allow 'OpenSSH' sudo ufw allow 'Nginx Full' # or 'Apache Full' sudo ufw enable
- For CentOS, using firewalld:
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload
- For Ubuntu, using UFW:
Step 2: Installing Your Web Server
Now, choose your preferred web server and follow the corresponding instructions.
Installing Nginx
Nginx is known for its high performance, efficiency, and scalability, making it an excellent choice for serving static content and as a reverse proxy.
- For Ubuntu/Debian:
sudo apt install nginx
- For CentOS/RHEL:
sudo yum install nginx
After installation, Nginx should start automatically. You can verify its status:
sudo systemctl status nginx
To check if the installation was successful, simply enter your server’s IP address into a web browser. You should see the default “Welcome to nginx!” page.
Installing Apache
Apache is a classic, highly flexible, and widely used web server, known for its extensive module support and comprehensive features.
- For Ubuntu/Debian:
sudo apt install apache2
- For CentOS/RHEL:
sudo yum install httpd
Similar to Nginx, you can verify its status:
sudo systemctl status apache2 # or httpd for CentOS
Entering your server’s IP address into a web browser should show the default “Apache2 Ubuntu Default Page” or a similar test page, confirming a successful installation.
Step 3: Configuring Your Web Server for a Domain
To host a website, you need to create a dedicated configuration file—a server block (for Nginx) or a virtual host (for Apache)—to tell the web server where to find your website files.
- Create a Directory for Your Website Files:
sudo mkdir -p /var/www/your_domain
- Create a Configuration File:
- For Nginx: Create a new file at
/etc/nginx/sites-available/your_domain
and add the following content:
Nginxserver { listen 80; server_name your_domain www.your_domain; root /var/www/your_domain; index index.html; location / { try_files $uri $uri/ =404; } }
Then, link the file to thesites-enabled
directory:sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
- For Apache: Create a new file at
/etc/apache2/sites-available/your_domain.conf
(or/etc/httpd/conf.d/your_domain.conf
on CentOS) and add this content:
Apache<VirtualHost *:80> ServerName your_domain ServerAlias www.your_domain DocumentRoot /var/www/your_domain ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
Then, enable the new configuration:Bashsudo a2ensite your_domain.conf # Ubuntu only
- For Nginx: Create a new file at
- Set File Permissions: This is a crucial security step.Bash
sudo chown -R www-data:www-data /var/www/your_domain # Ubuntu/Debian sudo chown -R apache:apache /var/www/your_domain # CentOS/RHEL sudo chmod -R 755 /var/www/your_domain
- Test and Restart: Check the configuration for syntax errors and then reload the web server.
- For Nginx:
sudo nginx -t
thensudo systemctl reload nginx
- For Apache:
sudo apache2ctl configtest
thensudo systemctl reload apache2
- For Nginx:
Step 4: Securing Your Web Server with HTTPS
A secure website uses HTTPS (an SSL/TLS certificate), which encrypts communication between the browser and the server. We’ll use Certbot, a free and automated tool, to get a certificate from Let’s Encrypt.
- Install Certbot:
- For Nginx on Ubuntu:
sudo apt install certbot python3-certbot-nginx
- For Apache on Ubuntu:
sudo apt install certbot python3-certbot-apache
- For Nginx on CentOS:
sudo yum install certbot python3-certbot-nginx
- For Apache on CentOS:
sudo yum install certbot python3-certbot-apache
- For Nginx on Ubuntu:
- Run Certbot: The tool will automatically configure your web server and get the certificate.
- For Nginx:Bash
sudo certbot --nginx -d your_domain -d www.your_domain
- For Apache:Bash
sudo certbot --apache -d your_domain -d www.your_domain
- For Nginx:Bash
Conclusion
You have successfully completed a step-by-step web server setup on your VPS. You now have a solid, secure foundation for your website, with a properly configured web server ready to host your content. This hands-on process not only gives you full control over your environment but also ensures optimal performance and security for your projects.
For a reliable hosting environment where you can easily implement these steps, consider Hosting.International – your partner for robust and powerful VPS solutions.
