Skip to main content

Ubuntu, NGINX, PHP, SQLite

Install NGINX:

sudo apt-get update -y
sudo apt-get upgrade -y
sudo apt-get install nginx -y

Verify NGINX is running:

sudo systemctl status nginx

● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running)

If you need to change the default port of NGINX or run the service only on localhost:

sudo vim /etc/nginx/sites-enabled/default

server {
        listen 127.0.0.1:8000 default_server;
        # listen [::]:80 default_server;
        port_in_redirect off;
...

sudo nginx -t
sudo systemctl restart nginx

Set port_in_redirect off; to prevent the port from being added to the URL after a redirect.

Install PHP:

sudo apt-get install php8.3-fpm -y

Verify PHP is running:

sudo systemctl status php8.3-fpm

● php8.3-fpm.service - The PHP 8.3 FastCGI Process Manager
     Loaded: loaded (/lib/systemd/system/php8.3-fpm.service; enabled; vendor preset: enabled)
     Active: active (running)

Enable PHP in NGINX:

sudo vim /etc/nginx/sites-enabled/default

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/run/php/php8.3-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
                deny all;
        }

In the default server configuration:

  1. Add index.php to the index list.
  2. Uncomment the location php block and fastcgi_pass for php-fpm. Ensure correct PHP version.
  3. Uncomment block to deny access to .htaccess files.

Validate the NGINX configuration changes and restart NGINX.

Install SQLite3 libraries and restart NGINX:

sudo apt-get install php-sqlite3 sqlite3 libsqlite3-dev -y

Finally change the permission of the root directory:

sudo chmod -R 777 /var/www/html

 

References:

  1. https://www.theserverside.com/blog/Coffee-Talk-Java-News-Stories-and-Opinions/Nginx-PHP-FPM-config-example
  2. https://forums.raspberrypi.com/viewtopic.php?t=347015