How to host a website on NGINX with PHP/MySQL on Ubuntu 22
In a previous article, we talked about how to host a website using Apache Virtual Hosts. We set up a basic website with an SSL certificate. In this article, we are going to show you how to host a website using NGINX with an SSL certificate configured. This server will use NGINX and PHP 8.1. There will be no control panel like cPanel or Direct Admin. Your server does not need to be powerful. A basic Ubuntu Linux VPS with NGINX will allow you to host a website.
How to host a website on NGINX Ubuntu
First, update your NVMe Server. Make sure your packages are on the latest versions.
# Initial Update
apt update && apt upgrade -y
# Install Dependencies
apt install software-properties-common ca-certificates lsb-release apt-transport-https
LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
# Update
apt update
Install NGINX, Apache & PHP
Next, we need to install our services. This includes NGINX, Apache and PHP. To install these at the same time. Enter the below command. So this installs PHP 8.1 with FPM and some additional modules. You can use apt search php8.1* to search for more PHP modules. If you need a different version of PHP. Just replace the 8.1 section with the version required. For example 7.4.
apt install nginx apache2 php8.1 php8.1-fpm php8.1-curl php8.1-mbstring php8.1-xml php8.1-zip -y
systemctl stop apache2
systemctl enable nginx php8.1-fpm
systemctl start nginx php8.1-fpm
Create Website Directory and Test Page
Next, create a directory where your website’s files will be stored. In our example, our files are going to be stored at /var/www/html/websiteA. Also, create a test HTML webpage with some random text inside it.
# Make Website Directory
mkdir -p /var/www/html/websiteA
# Allow FPM
chown -R www-data:www-data /var/www/html/
# Create Test HTML file
nano /var/www/html/websiteA/index.html
Configure NGINX
So now we need to create a configuration file. This will tell NGINX what site we are going to be hosting and where to find the website’s files. To do this we create a sites-available file.
# Create config file
nano /etc/nginx/sites-available/website.website.com
# Stipulate website details inside the file
server {
listen 80;
root /var/www/html/websiteA;
index index.php index.html index.htm index.nginx-debian.html;
server_name website.com www.website.com;
location / {
try_files $uri $uri/ =404;
proxy_busy_buffers_size 512k;
proxy_buffers 4 512k;
proxy_buffer_size 512k;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
}
location ~ \.php$ {
# Next line for CMS Perma
try_files $uri $uri/ /index.php?q=$uri&$args;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
}
add_header X-Content-Type-Options nosniff;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
location ~ /\.ht {
deny all;
}
}
So you need to edit the following sections;
- root
- server_name
Save and close the file.
Enable NGINX Website
Next, we need to disable the default configuration file and enable the configuration we just created. You can see a list of configurations in /etc/nginx/sites-available/.
unlink /etc/nginx/sites-enabled/default
# Link new configuration. Ensure you use the correct name of the file you saved
# in the previous setp
ln -s /etc/nginx/sites-available/website.website.com /etc/nginx/sites-enabled/
# Reload NGINX
systemctl restart nginx
At this point. If you visit your website in a browser, you should have a working website without an SSL certificate. In the final step, we will secure the site with an SSL.
Setup SSL Certificate
Unlike Apache, installing an SSL on NGINX is really simple. Install Certbot and generate your certificate.
# Install Certbot
apt install certbot python3-certbot-nginx
# Generate Certificate. Specify option 2 to send all traffic to SSL
sudo certbot --nginx -d website.com
That’s it. You now have a website running on a High Availability NVMe VPS Server with PHP 8.1 and NGINX. An SSL has been installed and all connections are secure.