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 7.4. There will be no control panel like cPanel or Direct Admin.
Your server does not need to be powerful. A basic VPS with NGINX will allow you to host a website.
Host a website using Nginx
Requirements – 1 x NVMe VPS Server or 1 x Business Dedicated Server
First, update your 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 7.4 with FPM and some additional modules. You can use apt search php7.4* to search for more PHP modules.
apt install nginx apache2 php7.4 php7.4-fpm php7.4-curl php7.4-mbstring php7.4-xml php7.4-zip -y
systemctl stop apache2
systemctl enable nginx php7.4-fpm
systemctl start nginx php7.4-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/php7.4-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 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 and 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 an NVMe VPS Server with PHP 7.4 and NGINX. An SSL has been installed and all connections are secure.
How was this article?
You might also like
More from Dedicated Servers
Enable Mod_RemoteIP – See Visitors Real IP address when using Cloudflare & Apache
If you are using Cloudflare on your Apache server you will always see Cloudflare IPs in your logs and not …
Cloud-init Modules That Automate and Customize Deployments
Cloud-init is a popular way to automate deployments of instances in a cloud or none cloud environment. To save having …
Install Ioncube Loaders In Ubuntu, Debian, CentOS and AlmaLinux
Ioncube Loaders are a piece of software that is used to protect the underlying code in PHP applications. Its aim …