Somik's Home

Server admins cheat codes

This will install Nginx and PHP 8.x on Ubuntu 22.04 or later and perform basic configurations needed to get it up and running. This is used to create servers to serve static content as well as minor PHP scripts that need to run on it.

Start by updating the server and installing Nginx:

sudo apt update
sudo apt upgrade
sudo apt install nginx nano

Then install PHP with either basic or with MariaDB (MySQL) support:

sudo apt install php-sqlite3 php-curl php-xml php-mbstring php-fpm
sudo apt install php-mysql php-gd php-imagick php-xmlrpc php-sqlite3 php-curl php-xml php-mbstring php-fpm

Edit PHP.ini file:

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

And add these at the bottom:

short_open_tag = On
display_errors = on
error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED & ~E_WARNING
error_log = error_log
output_buffering = Off
date.timezone = "Asia/Singapore"
upload_max_filesize = 50M
post_max_size = 50M
memory_limit = 128M

Edit the nginx configuration file:

sudo nano /etc/nginx/nginx.conf 

And add this line to increase file upload size:

http {
        client_max_body_size 50M;

And enable PHP in Nginx site config files for default domain

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

And edit it to make it look like this (change php8.2 with the version of PHP that got installed):

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;
        # 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.1-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass;

Finally save and exit.

Now enable Nginx and PHP fpm to start on boot and run now (change php8.2 with the version of PHP that got installed).

sudo systemctl enable nginx php8.2-fpm
sudo systemctl restart php8.2-fpm
sudo systemctl restart nginx

Now lets get a certificate for the domain from certbot:

sudo certbot --nginx --redirect -d -d

Follow the steps and you’ll get a certificate.

© Beli. All Rights Reserved.