How do I host a website on my VPS?
Hosting a website on a Virtual Private Server (VPS) provides you with greater control, flexibility and performance compared to shared hosting. This guide will walk you through the process of setting up a VPS to host a website using Nginx, serve PHP files, and set up a MariaDB database.
Prerequisites
Before you begin, ensure you have:
- A VPS running a Linux distribution (Ubuntu 24.04 is used in this guide).
- SSH & sFTP access to your VPS.
- A domain name pointing to your VPS's IP address.
Step 1: Update Your Server
First, log into your VPS via SSH and update your package list:
sudo apt update
sudo apt upgrade -y
Step 2: Install Nginx
To install Nginx, use the following commands:
sudo apt install nginx -y
Once installed, start and enable Nginx:
sudo systemctl start nginx
sudo systemctl enable nginx
Step 3: Install PHP
Install PHP and common modules:
apt -y install php8.3 php8.3-{common,cli,gd,mysql,mbstring,bcmath,xml,fpm,curl,zip}
Step 4: Configure Nginx to Serve PHP Files
Create a new Nginx server block configuration file for your website:
sudo nano /etc/nginx/sites-available/your_domain
Add the following configuration to the file:
server {
listen 80;
server_name your_domain;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name your_domain;
root /var/www/your_domain;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
# SSL Configuration - Replace the example your_domain with your domain
ssl_certificate /etc/letsencrypt/live/your_domain/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/your_domain/privkey.pem;
ssl_session_cache shared:SSL:10m;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384";
ssl_prefer_server_ciphers on;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param PHP_VALUE "upload_max_filesize = 100M \n post_max_size=100M";
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTP_PROXY "";
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
include /etc/nginx/fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
Replace your_domain
(5) with your actual domain name, if you installed a different php version you also need to change the PHP version here.
Create the root directory for your website:
sudo mkdir -p /var/www/your_domain
Transfer the website files inside this directory using sFTP.
Set the proper permissions:
sudo chown -R www-data:www-data /var/www/your_domain/*
Enable the new website config:
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/
Step 5: Install SSL
One of the easiest ways to install an SSL is by using certbot.
Install certbot:
sudo apt install -y certbot
Stop nginx:
sudo systemctl stop nginx
Generate SSL:
certbot certonly --standalone -d your_domain
Replace your_domain
with your actual domain name.
Start nginx:
sudo nginx -t
sudo systemctl start nginx
Step 6: Install MariaDB
Install MariaDB:
sudo apt install mariadb-server -y
Secure the installation:
sudo mysql_secure_installation
Follow the prompts to set the root password and secure your database server.
Step 7: Create a MariaDB Database and User
Log in to MariaDB:
sudo mariadb -u root -p
Create a new database and user:
CREATE DATABASE your_database;
CREATE USER 'your_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON your_database.* TO 'your_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Replace your_database
, your_user
, and your_password
with your preferred database name, username, and password.
Conclusion
You've successfully set up your VPS to host a website, serve PHP files, and manage a MariaDB database. You can now deploy your website and manage your server. Remember to keep your server and software updated for security and performance.
Updated on: 08/08/2024
Thank you!