NGINX Installation

- Posted in Server Configuration by

General setup;

apt-get remove --purge apache2 apache2-utils
The second will delete the Apache2 directory:
rm -rf /etc/apache2
Now you are ready to install the LEMP software stack

Install the Nginx web server.
$ sudo apt-get install nginx -y 

Start the Nginx service.
$ sudo systemctl start nginx 

Enable the Nginx service to start at system reboot.
$ sudo systemctl enable nginx 

Check the Nginx version to verify the installation.
$ sudo nginx -v 

http://your_domain_or_IP
**Debian 12 ships with an older version of Nginx.**     

To install the latest version, you need to download Nginx repository.
Import Nginx's signing key.
        
 $ curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \ 
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
        
Add the repository for Nginx's stable version.
        
$ echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \ 
http://nginx.org/packages/debian `lsb_release -cs` nginx" \ 
| sudo tee /etc/apt/sources.list.d/nginx.list
        
Update the system repositories.
$ sudo apt update
        
**Install Nginx.**        
$ sudo apt install nginx
        
Verify the installation. 
$ sudo nginx -v
    nginx version: nginx/1.24.0
        
Start Nginx.        
        $ sudo systemctl start nginx
        $ sudo systemctl status nginx
        
Create and open a test page for editing.
        $ sudo nano /var/www/html/example.com/index.php
        to your website...!

I. Create an Nginx Virtual Host---Tested, Good.

 Remove the default Nginx configuration.  
 $ sudo rm -rf   /etc/nginx/sites-enabled/default  
 $ sudo rm -rf   /etc/nginx/sites-available/default
        
 Create an Nginx virtual host configuration file. 
        Replace your-domain.com with your domain name.
    sudo mkdir /var/www/your_domain
    sudo chown -R $USER:$USER /var/www/your_domain

    $ sudo nano /etc/nginx/sites-available/your_domain
    In case, nginx sites-available folder not found 
    create the sites-available and sites-enabled folder 
     sudo mkdir /etc/nginx/sites-available 
    $ sudo mkdir /etc/nginx/sites-enabled 
    $ nano /etc/nginx/nginx.conf and add this line at the end 
    include /etc/nginx/sites-enabled/*; 
    Now, create a server configuration
    Paste this into the file. Replace example.com with your domain name.

    server {
      listen 80;
      server_name your domain;

      root /var/www/html/crunch;
      index index.php index.html index.nginx-debian.html;
      access_log /var/log/nginx/example_access.log;
      error_log /var/log/nginx/example_error.log;

      location / {
        try_files $uri $uri/ /index.php$is_args$args;
      }

      location ~ \.php$ {
        try_files $uri =404;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      }
     } 

    Enable the new Nginx configuration. Replace example.com with your domain name.
     $ sudo ln -s /etc/nginx/sites-available/crunch.riverfire.net /etc/nginx/sites-enabled/crunch.riverfire.net
    Reload the Nginx service.
     $ sudo systemctl reload nginx
    sudo service nginx restart
    nginx -t
    output:
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successf

    sudo systemctl restart nginx

II. Create another Nginx Virtual Host

 sudo mkdir /var/www/your_domain
 sudo chown -R $USER:$USER /var/www/your_domain
 sudo nano /etc/nginx/sites-available/your_domain
 This will create a new blank file. 
 Paste in the following bare-bones configuration:
 server {
    listen 80;
    listen [::]:80;
    root /var/www/your_domain;
    index index.php index.html index.htm;
    server_name your_domain;

    location / {
        try_files $uri $uri/ =404;
    }
 location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php-fpm.sock;
    }
}
Activate:
sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/

Test:
sudo nginx -t

sudo systemctl reload nginx

III. Setting multiple hosts

Now that you have your main website up, setting up multiple virtual hosts is also possible.
Copy the default server block to create a second server block (virtual host)

$ sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/domain_name
$ sudo nano /etc/nginx/sites-available/domain_name
...
server {
  listen 80;
  listen [::]:80;
  root /var/www/domain_name/public;
  index index.php index.html index.htm;

  server_name domain_name;
  access_log /var/log/nginx/domain_name-access.log;
  error_log /var/log/nginx/domain_name-error.log;

  location / {
    try_files $uri $uri/ @mvcrewrite;
  }
  location @mvcrewrite {
    rewrite ^(.+)$ /index.php?url=$1 last;
  }
  error_page 404 /404.html;
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root /usr/share/nginx/html;
  }
  location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
  } 
  # deny access to .htaccess files to prevent conflict
  location ~ /\.ht {
    deny all;
  }
}

Sites must be activated at /etc/nginx/sites-enabled. 
To do this, simply create a symbolic link to sites-activated that you have just created earlier.
$ sudo ln -s /etc/nginx/sites-available/domain_name /etc/nginx/sites-enabled/domain_name
  sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/
$ sudo service nginx restart
And that's it! Simply go to domain_name and nginx's server block will automatically direct 
your domain name to the relevant folders!

IV. Set an NGINX Site Configuration File

sudo mkdir -p /var/www/html/example.com/public_html
sudo cp /etc/nginx/sites-enabled/default /etc/nginx/sites-available/example.com.conf
nano /etc/nginx/sites-available/example.com.conf

server {

   listen         80;
   listen         [::]:80;
   server_name    example.com www.example.com;
   root           /var/www/html/example.com/public_html;
   index          index.html;

   location / {
     try_files $uri $uri/ =404;
   }

   location ~* \.php$ {
     fastcgi_pass unix:/run/php/php7.3-fpm.sock;
     include         fastcgi_params;
     fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
     fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
   }
}

sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/

sudo systemctl restart php7.3-fpm
sudo nginx -s reload
Test the NGINX configuration:
sudo nginx -t