How to Host a Website on Your VPS (Nginx/Apache + PHP + SSL)

Estimated reading: 3 minutes

This guide shows you how to host a website on your VeerHost VPS from scratch — installing a web server (Nginx or Apache), PHP, creating a virtual host, uploading your files, and securing it with a free Let’s Encrypt SSL certificate. Works on Ubuntu 22.04, Ubuntu 24.04, and Debian.

Option A: Nginx (Recommended for Most Sites)

1. Install Nginx and PHP

apt update
apt install -y nginx php-fpm php-mysql php-cli php-curl php-zip php-mbstring php-xml php-gd

Check PHP version:

php -v

2. Create the Website Directory

mkdir -p /var/www/yourdomain.com/html
chown -R www-data:www-data /var/www/yourdomain.com
chmod -R 755 /var/www/yourdomain.com

3. Create an Nginx Virtual Host

nano /etc/nginx/sites-available/yourdomain.com

Paste this config (replace yourdomain.com with your actual domain and adjust the PHP version if needed):

server {
    listen 80;
    listen [::]:80;
    server_name yourdomain.com www.yourdomain.com;
    root /var/www/yourdomain.com/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }
}

Note: Check your PHP-FPM socket version: ls /var/run/php/ and use the correct filename (e.g., php8.1-fpm.sock or php8.3-fpm.sock).

Enable the site and test:

ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/
nginx -t
systemctl reload nginx

Option B: Apache

1. Install Apache and PHP

apt update
apt install -y apache2 php libapache2-mod-php php-mysql php-curl php-zip php-mbstring php-xml php-gd

2. Create the Website Directory

mkdir -p /var/www/yourdomain.com/html
chown -R www-data:www-data /var/www/yourdomain.com

3. Create an Apache Virtual Host

nano /etc/apache2/sites-available/yourdomain.com.conf
<VirtualHost *:80>
    ServerName yourdomain.com
    ServerAlias www.yourdomain.com
    DocumentRoot /var/www/yourdomain.com/html

    <Directory /var/www/yourdomain.com/html>
        AllowOverride All
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/yourdomain_error.log
    CustomLog ${APACHE_LOG_DIR}/yourdomain_access.log combined
</VirtualHost>
a2ensite yourdomain.com.conf
a2enmod rewrite
apache2ctl configtest
systemctl reload apache2

Install MySQL (If Needed)

apt install -y mysql-server
mysql_secure_installation

Create a database and user for your website:

mysql -u root -p

CREATE DATABASE mywebsite;
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'StrongPassword123!';
GRANT ALL PRIVILEGES ON mywebsite.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Upload Your Website Files

From your local machine, upload files via SCP:

scp -r /path/to/local/files root@YOUR_VPS_IP:/var/www/yourdomain.com/html/

Or use an FTP client like FileZilla with SFTP (port 22) and your SSH credentials.

Install a Free SSL Certificate (Let’s Encrypt)

Make sure your domain’s DNS is already pointing to your VPS IP before running this. Install Certbot:

apt install -y certbot python3-certbot-nginx
# or for Apache:
apt install -y certbot python3-certbot-apache

Get and install the certificate:

# Nginx:
certbot --nginx -d yourdomain.com -d www.yourdomain.com

# Apache:
certbot --apache -d yourdomain.com -d www.yourdomain.com

Certbot will automatically configure HTTPS and set up auto-renewal. Test renewal:

certbot renew --dry-run

Verify It’s Working

Create a test PHP file to confirm everything is working:

echo '<?php phpinfo(); ?>' > /var/www/yourdomain.com/html/info.php

Visit http://yourdomain.com/info.php in your browser. You should see the PHP info page. Delete it after confirming:

rm /var/www/yourdomain.com/html/info.php