A performant Nginx with PHP 7.4 or 8.1

Installing Nginx

In case that you have installed Apache2 already, then remove it first with these commands & then install nginx:

be shure to be root

sudo -i

Stop and remove Apache
service apache2 stop && update-rc.d -f apache2 remove && apt-get remove apache2

Install Nginx and start it

be shure to get updates on a fresh server first start

apt-get update

no you can install the latest version of nginx
apt-get -y install nginx && service nginx start

TEST Type in your web browser (the server’s IP address or hostname) e.g. http://example.com

and you should see the page: welcome to nginx!

Remove old PHP Versions

apt remove php*

Installing PHP 7.4

with the LTS version of Ubuntu you need to add the php repo

sudo apt install software-properties-common

sudo add-apt-repository ppa:ondrej/php

Smothe and performant is the socket installation:

!wordpress does not work under v8.1 yet

apt -y install php8.1 php8.1-fpm php8.1-xml php8.1-mysqli

7.4 works well for wordpress

apt -y install php7.4 php7.4-fpm php7.4-xml php7.4-mysqli

that runs a FastCGI server on the socket /run/php/php7.4-fpm.sock

nano /etc/nginx/sites-available/default

[…]
server {
listen 80 default_server;
listen [::]:80 default_server;

# SSL configuration
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;

root /var/www/html;

# 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 the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;

# With php7.4-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# With php8.1-fpm:
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}

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

##optional: save an error page under /var/www/error/404.html
#error_page 404 /404.html;
#location = /404.html {
#root /var/www/error/;
#internal;
#}

}

sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /etc/php/7.4/fpm/php.ini

service nginx reload && service php7.4-fpm reload

(Test your PHP setup)

nano /var/www/html/info.php

Find and Install optional PHP modules

apt-cache search php8.1

apt install php8.1-mbstring php8.1-mcrypt php8.1-curl php8.1-gd php8.1-intl php-pear php-imagick php8.1-imap php-memcache  php8.1-pspell php8.1-recode php8.1-sqlite3 php8.1-tidy php8.1-xmlrpc php8.1-xsl  php-gettext php-apcu

service php8.1-fpm reload

or use

sudo systemctl restart php7.4-fpm nginx

Leave a Reply