How to Install NGINX and Configure it as a Web Server on Your VPS
NGINX is a lightweight, high-performance web server often used to serve websites and applications. This guide will help you install and configure NGINX on your VPS.
Step 1: Update Your System
Before installing any software, ensure your VPS is up to date.
- Run the following command to update the package lists and upgrade installed packages:
sudo apt update && sudo apt upgrade -y
Step 2: Install NGINX
NGINX is available in the default repositories for most Linux distributions.
- On Ubuntu/Debian-based systems:
sudo apt install nginx -y
- On CentOS/RHEL-based systems:
sudo yum install epel-release -y sudo yum install nginx -y
Step 3: Start and Enable NGINX
Ensure NGINX starts automatically after a reboot and verify it is running.
- Start NGINX:
sudo systemctl start nginx
- Enable NGINX to start on boot:
sudo systemctl enable nginx
- Check the status of NGINX:
sudo systemctl status nginx
Step 4: Adjust Firewall Settings
If your VPS uses a firewall, allow HTTP and HTTPS traffic.
- On Ubuntu/Debian with UFW:
sudo ufw allow 'Nginx Full'
- On CentOS/RHEL with firewalld:
sudo firewall-cmd --permanent --add-service=http sudo firewall-cmd --permanent --add-service=https sudo firewall-cmd --reload
Step 5: Verify NGINX Installation
- Open your web browser and enter your VPS IP address (e.g.,
http://your-vps-ip
). - You should see the default NGINX welcome page.
Step 6: Configure NGINX as a Web Server
To host your website, you’ll need to set up a server block (similar to virtual hosts in Apache).
- Create a directory for your website files:
sudo mkdir -p /var/www/yourdomain.com/html
- Set permissions for the directory:
sudo chown -R $USER:$USER /var/www/yourdomain.com/html sudo chmod -R 755 /var/www/yourdomain.com
- Create a sample index.html file:
echo "<h1>Welcome to yourdomain.com!</h1>" | sudo tee /var/www/yourdomain.com/html/index.html
Step 7: Create an NGINX Server Block
NGINX server blocks handle different websites on the same server.
- Create a new configuration file:
sudo nano /etc/nginx/sites-available/yourdomain.com
- Add the following configuration:
server { listen 80; server_name yourdomain.com www.yourdomain.com; root /var/www/yourdomain.com/html; index index.html; location / { try_files $uri $uri/ =404; } }
- Save and close the file.
Step 8: Enable the Server Block
- Create a symbolic link to enable the server block:
sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
- Test the configuration for syntax errors:
sudo nginx -t
- Reload NGINX to apply changes:
sudo systemctl reload nginx
Step 9: Update Your DNS Settings
Ensure your domain points to your VPS. Update your DNS A records to use your VPS's public IP address.
Step 10: Test Your Website
- Open your browser and navigate to
http://yourdomain.com
. - You should see the content of your
index.html
file.
Conclusion
By following these steps, you’ve successfully installed and configured NGINX as a web server on your VPS. You can now host websites and applications efficiently. Regularly monitor your server and update NGINX to maintain performance and security.