Skip to content

Installing and configuring Nginx on a server with SSL and PHP

Complete instructions for installing Nginx on a server: configuration, connecting PHP, enabling SSL and checks. Suitable for VPS and dedicated servers.

Installing and configuring Nginx on a server with SSL and PHP

Install Nginx by running the following command:

bash
apt-get install nginx -y

Configuring Nginx

In sFTP, go to /etc/nginx/sites-available and create the file server_name.conf (the name can be anything), then add the following text to it, replacing the details with your own:

nginx
server {
    listen       *:80;
    server_name  f3cloud.com; # site domain
    client_max_body_size 1000M; # maximum size of a file transferred through the site
    error_page 404 = @notfound;
    location / {
        root   /home/site/example; # path to the site
        try_files $uri $uri.html $uri/ @extensionless-php;
        index  index.html index.php;
    }
    # PHP connection, if you don't need it, delete lines 13 to 21
    location ~ \.(php|html|htm)$ {
        try_files $uri =404;
        root   /home/site/example; # path to the site
        fastcgi_pass unix:/run/php/php7.0-fpm.sock; # path to php 
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        include /etc/nginx/fastcgi_params;
    }
}

Restart Nginx:

service nginx restart

Connecting PHP to Nginx

INFO

PHP is not required for Nginx to work. Include this block only for sites that need to run PHP scripts.

Let's run the following commands one after another:

wget -q https://packages.sury.org/php/apt.gpg -O- | apt-key add -
echo "deb https://packages.sury.org/php/ stretch main" | tee /etc/apt/sources.list.d/php.list
sudo apt-get -y install php7.4 php7.4-{mcrypt,mysql,fpm}

Restart Nginx:

service nginx restart

Enabling SSL (encryption protocol)

INFO

This is an optional step that increases trust in your site

Edit the previously created configuration file so that it looks like this:

server {
    listen 80;
    server_name f3cloud.com; # site domain
    return 301 https://$server_name$request_uri; # redirect from http to https
}

server {
    listen 443 ssl http2;
    server_name f3cloud.com; # site domain

    root /var/www/example; # path to the site
    index index.html index.htm index.php; # index pages

    access_log /var/log/nginx/example.app-access.log; # logs of successful connections
    error_log  /var/log/nginx/example.app-error.log error; # logs of failed connections

    # if you need to disable something, write “off” instead of the file path

    client_max_body_size 1000m; # maximum size of a file transferred through the site
    client_body_timeout 120s; # timeout value

    sendfile off; # once enabled, Nginx will send HTTP response headers in one packet rather than in separate parts.

    # SSL Configuration
    ssl_certificate /etc/letsencrypt/live/f3cloud.com/fullchain.pem; # SSL certificate public key
    ssl_certificate_key /etc/letsencrypt/live/f3cloud.com/privkey.pem; # SSL certificate private key
    ssl_session_cache shared:SSL:10m; # SSL session cache size
    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; # reduces site page load time

    location ~ \.(php|html|htm)$ {
try_files $uri =404;
root /var/www/example; # path to the site
fastcgi_pass unix:/run/php/php7.2-fpm.sock; # path to the php file
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include /etc/nginx/fastcgi_params;
    }
}

Restart Nginx:

service nginx restart

Checking for Apache2

INFO

If Nginx and Apache2 are used at the same time, they will conflict over port 80, which will lead to incorrect operation. Therefore, you need to remove one of the web servers.

Let's check for Apache2:

service apache2 status

INFO

If you don't see a large message with information, then Apache2 is not installed.

To remove Apache2, let's enter:

apt-get remove --purge apache2* -y

Removing Nginx

To remove Nginx, let's enter the command to stop it:

service nginx stop

And then the command to completely remove Nginx:

apt-get remove --purge nginx*

Our resources

Telegram channel: https://t.me/
F3 Cloud: https://f3cloud.com

F3 Cloud Knowledge Base