Installing Nginx
On this page
Installing NginxWeb Server Installation:Configuring Nginx:Connecting an SSL Certificate to Nginx:Checking for Apache2:Removing NginxInstalling Nginx
Nginx is a web server with a wide range of features, built with the latest technology standards.
Before installation, make sure you do not have Apache2 installed!
Web Server Installation:
Its installation is extremely simple, use the following command:
apt-get install nginx -yConfiguring Nginx:
You need to navigate to the directory /etc/nginx/sites-available and create a file like "site.conf".
Insert and edit the following in the configuration:
server {
listen *:80;
server_name site.com; # site's domain
client_max_body_size 1000M; # maximum file size transmitted through the site
error_page 404 = @notfound;
location / {
root /home/site/spacecore; # path to the site
try_files $uri $uri.html $uri/ @extensionless-php;
index index.html index.php;
}
# PHP integration, remove lines 13 to 21 if not needed
location ~ .(php|html|htm)$ {
try_files $uri =404;
root /home/site/spacecore; # 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;
}
}Reload Nginx:
service nginx restartNotes on PHP
PHP is not mandatory for working with Nginx. This note is only important for those whose site uses PHP.
Additional information can be found here.
Connecting an SSL Certificate to Nginx:
Enabling SSL is optional and is done if you have an SSL certificate and want to use it. You should create or edit the site config as follows:
server {
listen 80;
server_name site.com; # site's domain
return 301 https://$server_name$request_uri; # redirect from HTTP to HTTPS
}
server {
listen 443 ssl http2;
server_name site.com; # site's domain
root /var/www/spacecore; # path to the site
index index.html index.htm index.php; # index pages
access_log /var/log/nginx/spacecore.app-access.log; # successful connection logs
error_log /var/log/nginx/spacecore.app-error.log error; # error logs
# If something needs to be disabled, replace the file path with "off"
client_max_body_size 1000m; # maximum file size transmitted through the site
client_body_timeout 120s; # timeout value
sendfile off; # after enabling, Nginx will send HTTP response headers in one packet, not separately.
# SSL Configuration
ssl_certificate /etc/letsencrypt/live/site.com/fullchain.pem; # public certificate key
ssl_certificate_key /etc/letsencrypt/live/site.com/privkey.pem; # private certificate 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 website loading time
location ~ .(php|html|htm)$ {
try_files $uri =404;
root /var/www/spacecore; # path to the site
fastcgi_pass unix:/run/php/php7.2-fpm.sock; # path to PHP
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include /etc/nginx/fastcgi_params;
}
}Reload Nginx:
service nginx restartChecking for Apache2:
If you install Nginx alongside Apache2, they will conflict due to port 80. So, you need to remove one of the web servers. Check if Apache2 is installed:
service apache2 statusIf you don't see a large message with information, it means Apache2 is not installed.
If Apache2 is installed, you need to remove it:
apt-get remove --purge apache2* -yRemoving Nginx
To completely remove Nginx from the server, you must first stop it:
service nginx stopThen, use the command that will irreversibly remove it:
apt-get remove --purge nginx*