Setting up Nginx + Php8

1. Connect Nginx and PHP 8

Installing the nginx php8 package does not mean that the php file will be applied to the web server. Some files need to be modified so that nginx can serve php8. If your settings are incorrect, php8 will not work properly with nginx. In particular, there is a problem where nothing appears when the php file is downloaded or when phpinfo or php code is run.

Original Korean article: Setting up Nginx + Php8

The part we are checking now will help PHP operate properly on the web server through nginx and PHP8 settings.

1 file exists and 3 files need to be modified.

  1. The /etc/nginx/nginx.conf file exists.
  2. Check and add fastcig_param in /etc/nginx/fastcgi_params
  3. Add and modify php script in /etc/nginx/sites-available/default setting
  4. default_type in /etc/nginx/nginx.conf

Fourth, if you do not fix it, there will be a problem where the php file will not run and be downloaded.

1) Check /etc/nginx/nginx.conf

If you followed the previous installation and steps, the nginx.conf file exists. If you don't have it, just copy the following content and create a file. If you copy the contents below to create a file, you can skip step 4.

The user set below may differ depending on the PHP version. user must match the user, group, listen.owner, and listen.group information in sudo nano /etc/php/8.1/fpm/pool.d/www.conf.

Create a file.

sudo nano /etc/nginx/nginx.conf

Paste the following code:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {
        ##
        # Basic Settings
        ##
        sendfile on;
        tcp_nopush on;
        types_hash_max_size 2048;
        # server_tokens off;
        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;
        include /etc/nginx/mime.types;
        #default_type application/octet-stream;
        default_type text/html;
        ##
        # SSL Settings
        ##
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##
        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##
        gzip on;
        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

2) Check and add fastcig_param in /etc/nginx/fastcgi_params

Open /etc/nginx/fastcgi_param with an editor and add the following if it does not already exist:

sudo nano /etc/nginx/fastcgi_params

Add the following code to the first line:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
Article image 1
Article image 1

3) Add and modify php script in /etc/nginx/sites-available/default setting.

Run the default file with an editor and modify the location part as follows. Here, we modify it based on version 8.1. Simply find and change the following part of the existing file:

# 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;
}
Article image 2
Article image 2

Restart both Nginx and PHP.

sudo systemctl restart nginx
sudo systemctl restart php8.1-fpm

4. Run PHP 8 and check

Create a file to check PHP information. The file is created in the web root.

sudo nano /var/www/html/info.php

Copy and paste the following content according to the PHP code rules.

<?php
phpinfo();
?>

Check the php information on the website with localhost/info.php or address/info.php.

Article image 3
Article image 3

This article was written by selecting only the problem-solving part of the PHP8 settings after installing nginx, and is duplicated with the previous article. If you want to know more about installing nginx, click the following link:

Install Nginx web server (ubuntu) – Thinknote

If you want to install php8 with Ubuntu's nginx installed, click the following link. PHP installation is a preparatory step for installing WordPress.

Install PHP 8 (ubuntu) – Thinknote

Good article to read together

  • Install PHP 8 (ubuntu)
  • Install memory caching APCu, Redis, Memcached
  • Install Nginx web server (ubuntu)
  • Free HTTPS setup (Let’s Encrypt, Cloudflare)
  • Install Nginx web server (Centos 8)

Related Reading

FAQ

What is this article about?

This article is an English translation and global-reader adaptation of the Korean post “Setting up Nginx + Php8.” It preserves the original article’s main explanation, examples, and practical context.

Why is it translated into English?

The English version helps global readers access Thinknote articles through English search keywords while keeping the Korean source available as the original reference.

Where can I read the original Korean version?

You can read the original Korean article here: https://www.thinknote.co.kr/nginx-php8-configuration/