Replace Apache with NGinx

Assumptions:

  • This is an existing Apache VPS with live sites.
  • You want to completely remove Apache and replace with NGinx

Optional Step:

Add nginx ppa for the latest stable version.

sudo add-apt-repository ppa:nginx/stable
This step is optional as nginx is available in the ubuntu repositories

Let's begin

Lets start by upgrading our system, adding nginx ppa repository (optional) and installing nginx.

sudo apt update && sudo apt upgrade && sudo apt dist-upgrade
sudo apt install nginx

Now let's make sure to allow nginx through our firewall.

sudo ufw allow 'Nginx Full'

If you use php for your site install php-fpm

sudo apt install php-fpm

Let's now configure our nginx server.

sudo nano /etc/nginx/sites-available/default
server {
	# listen 80 default_server;
    listen 8080;

for now lets just comment out "listen 80 default_server;" and add the line "listen 8080" so that we can test the default installation. Using http://your.domain:8080.

You'll get something like:

Note that up to this point Apache is still running and configured to listen on por 80 (default html port)

Next let's create our server config using Nginx Syntax. Refer to below table for nginx equivalent of apache syntax.

Apache Nginx
<VirtualHost *:80> server {
    listen 80;
ServerName your.domain
ServerAlias www.your.domain
server_name your.domain www.your.domain;
DocumentRoot /path/to/root root /path/to/root;
DirectoryIndex index.php index index.php;
ErrorLog /path/to/log error_log /path/to/log error;
CustomLog /path/to/log combined access_log /path/to/log main;
Alias /url/ "/path/to/files" location /url/ {
<Directory "/path/to/files"> alias /path/to/files;

We will end up with something like:

server {
    listen 8080;

    root /var/www;
    index index.html index.htm index.nginx-debian.html index.php;
    
    server_name your.domain www.your.domain;

    location / {
        try_files $uri $uri/ /index.html;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php7.4-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location /doc/ {
        alias /usr/share/doc/;
        autoindex on;
        allow 127.0.0.1;
        deny all;
    }

    location ~/\.ht {
        deny all;
    }

}

Now let's start our nginx

sudo systemctl restart nginx

Now let's configure php7-fpm.

sudo nano /etc/php/7.4/fpm/php.ini

Press ctrl-w  and type cgi.fix_pathinfo then ctrl-w again (search function of nano). Chances are this attribute is preceded with a semicolon ";" (commented out) and that the default value is 1. So we first remove the semicolon and replace 1 with 0;

cgi.fix_pathinfo=0

Let's restart php-fpm

sudo systemctl restart php7.4-fpm

Now let's create a php file in /var/www/html to test.

echo "<php phpinfo(); ?>" > /var/www/html/phpinfo.php

If everything is in order. Convert all of your apache sites' configuration to nginx, then set nginx to listen on port 80

sudo nano /etc/nginx/sites-available/default

Uncomment the "listen 80 default_server;" line  and comment the "listen 8080;"

Next let's stop apache and reload nginx config

sudo systemctl disable apache2
sudo systemctl reload nginx

When you are satisfied with the result you can go a step further by uninstalling apache and deleting it's config folder.

sudo apt purge apache2 apache2-utils
sudo apt autoremove
sudo rm -rf /etc/apache2

Our work is done. Enjoy