How to install both PHP 8.0 and 7.4 on NGINX and Ubuntu

PHP has a new version and it was recently released on the public on November 2020. The update brings several advantage and improvements to squeeze some juices for a speedy NGINX webserver and web app like WordPress. One of this are the JIT (Just in Time) Compiler, where this new features enables PHP to compile your code immediately before it is executed. Basically, it gives your server significant faster code execution compared to the traditional PHP compilation.

But did you know that this leap in performance was based on the original OPCache found on the previous version of PHP? In which it pre-compiled the codes of your PHP so that when it run on the next time, it gives the pre-compiled result. Though the previous OPCache was not natively executive compared to the latest JIT compiler.

Because of this improvement, you must likely just to try PHP 8.0 if it will works on your server. To do that without server down time, your first choice is to install PHP 8.0 alongside with your current PHP 7.4 version. The good news is, this is possible with NGINX and Ubuntu where you can use multiple version of PHP on your system and even nominate which server to use PHP 8.0 or PHP 7.4.

So, without further ado, let’s proceed on installing these two version of PHP 7.4. This step doesn’t include step on installing NGINX, you can check our other post for that:

Or simply installing it with this simple line of code sudo apt-get install nginx.

How to install PHP 7.4

To install PHP, you need a repository that we can use to just download the compiled code for Ubuntu OS. The most popular repository is from Ondrej. Go to your SSH and execute this code.

sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt upgrade

Next is, we need to install the PHP 7.4 and we have to make sure that apache server is not installed when doing this. To ignore apache on installation, we have to add apache2- apache2-bin-.

sudo apt-get install php7.4 apache2- apache2-bin-

After that, we have to install necessary module for our PHP that are useful for our web apps like WordPress.

sudo apt-get install php7.4-cli php7.4-fpm php7.4-pdo php7.4-mysql php7.4-zip  php7.4-mbstring php7.4-curl php7.4-xml php7.4-bcmath php7.4-json php7.4-imagick

Make sure to restart your PHP fpm and let’s see if the version will show up.

sudo service php7.4-fpm restart
php-fpm7.4 -v

For configuration, we will discuss later.

How to install PHP 8.0

Installing PHP 8.0 is pretty much the same, the only difference is that some module are already built-in on the system. Still, we are going to use Ondrej repository. If you didn’t execute the previous step, here is the code to add the repo:

sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt upgrade

The same, we have to install PHP 8.0 on our Ubuntu with omitting installation of apache servers.

sudo apt-get install php8.0 apache2- apache2-bin-

Then the necessary module for our webapp.

ssudo apt-get install php8.0-fpm php8.0-pdo php8.0-mysql php8.0-zip php8.0-mbstring php8.0-curl php8.0-xml php8.0-bcmath php8.0-imagick

Then restart your php8.0-fpm and check the version.

sudo service php8.0-fpm restart
php-fpm8.0 -v

We can now proceed on configuring our NGINX server to work with our PHP.

PHP Configuration for NGINX

To make sure our PHP will work on our NGINX server, we need to add snippets folder for our PHP configuration.

mkdir /etc/nginx/snippets

Then here we can create separate configuration files for our 7.4 and 8.0. For 7.4, create a file called fastcgi-php7.4.conf inside our snippets folder.

vim /etc/nginx/snippets/fastcgi-php7.4.conf

Then add the following codes. (You may uncomment the FASTCGI cache if you’re not using Google Pagespeed module)

#split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;

# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;

# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;

fastcgi_index index.php;
include fastcgi_params;

##
# FastCGI PHP connection
##
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_read_timeout 300;

##
# FastCGI Cache
##
#fastcgi_cache phpcache; 
#fastcgi_cache_valid 200 60m; 
#fastcgi_cache_methods GET HEAD;
#add_header X-Fastcgi-Cache $upstream_cache_status; 

Then create a file for our 8.0, called fastcgi-php8.0.conf.

vim /etc/nginx/snippets/fastcgi-php8.0.conf

Then add these codes with fewer adjustment for our 8.0.

#split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;

# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;

# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;

fastcgi_index index.php;
include fastcgi_params;

##
# FastCGI PHP connection
##
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
fastcgi_read_timeout 300;

##
# FastCGI Cache
##
#fastcgi_cache phpcache; 
#fastcgi_cache_valid 200 60m; 
#fastcgi_cache_methods GET HEAD;
#add_header X-Fastcgi-Cache $upstream_cache_status; 

We are now ready, what you only need to do is to edit your server block. By default, it is located at /etc/nginx/conf.d/default.conf.

If you want to use 7.4 on your website, just add this code inside on your server block.

    location ~ \.php$ {
        include snippets/fastcgi-php7.4.conf;
    }

For 8.0, add this code.

    location ~ \.php$ {
        include snippets/fastcgi-php8.0.conf;
    }

Lastly, make sure this line fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; were added on the first line of your fastcgi_params configuration. It should look like.

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

Don’t forget to restart your NGINX.

sudo service nginx restart

That’s it! You pretty have working PHP 8.0 and 7.4 on your NGINX server now.

Enabling PHP 8.0 JIT Compiler

But we still have to enable our JIT compiler for our PHP 8.0, by default it is turned on, but wasn’t as the buffer size configuration was set to zero.

You need to edit the opcache configuration.

vim /etc/php/8.0/cli/conf.d/10-opcache.ini

Then add these two lines of codes at the bottom. You can adjust the JIT buffer size depending on your available RAM Memory. You can also use the following file size notation.

  • M – Megabyte (ex. 512M)
  • G – Gigabyte (ex. 2G)
; configuration for php opcache module
; priority=10
zend_extension=opcache.so

opcache.enable_cli=1
opcache.jit_buffer_size=512M

After that, restart your PHP 8.0 fpm to reflect the changes.

sudo service php8.0-fpm restart

Then to check if the configuration is properly set, check the PHP info.

php -i | grep 'opcache\.'

The values should be the following (Make sure, opcache.jit is equal to tracing => tracing):

opcache.blacklist_filename => no value => no value
opcache.consistency_checks => 0 => 0
opcache.dups_fix => Off => Off
opcache.enable => On => On
opcache.enable_cli => On => On
opcache.enable_file_override => Off => Off
opcache.error_log => no value => no value
opcache.file_cache => no value => no value
opcache.file_cache_consistency_checks => On => On
opcache.file_cache_only => Off => Off
opcache.file_update_protection => 2 => 2
opcache.force_restart_timeout => 180 => 180
opcache.huge_code_pages => Off => Off
opcache.interned_strings_buffer => 8 => 8
opcache.jit => tracing => tracing
opcache.jit_bisect_limit => 0 => 0
opcache.jit_blacklist_root_trace => 16 => 16
opcache.jit_blacklist_side_trace => 8 => 8
opcache.jit_buffer_size => 512M => 512M
opcache.jit_debug => 0 => 0
opcache.jit_hot_func => 127 => 127
opcache.jit_hot_loop => 64 => 64
opcache.jit_hot_return => 8 => 8
opcache.jit_hot_side_exit => 8 => 8
opcache.jit_max_exit_counters => 8192 => 8192
opcache.jit_max_loop_unrolls => 8 => 8
opcache.jit_max_polymorphic_calls => 2 => 2
opcache.jit_max_recursive_calls => 2 => 2
opcache.jit_max_recursive_returns => 2 => 2
opcache.jit_max_root_traces => 1024 => 1024
opcache.jit_max_side_traces => 128 => 128
opcache.jit_prof_threshold => 0.005 => 0.005
opcache.lockfile_path => /tmp => /tmp
opcache.log_verbosity_level => 1 => 1
opcache.max_accelerated_files => 10000 => 10000
opcache.max_file_size => 0 => 0
opcache.max_wasted_percentage => 5 => 5
opcache.memory_consumption => 128 => 128
opcache.opt_debug_level => 0 => 0
opcache.optimization_level => 0x7FFEBFFF => 0x7FFEBFFF
opcache.preferred_memory_model => no value => no value
opcache.preload => no value => no value
opcache.preload_user => no value => no value
opcache.protect_memory => Off => Off
opcache.record_warnings => Off => Off
opcache.restrict_api => no value => no value
opcache.revalidate_freq => 2 => 2
opcache.revalidate_path => Off => Off
opcache.save_comments => On => On
opcache.use_cwd => On => On
opcache.validate_permission => Off => Off
opcache.validate_root => Off => Off
opcache.validate_timestamps => On => On

That’s pretty is it guys! Hope this small tutorial will helps you install both PHP 7.4 and PHP 8.0 on your NGINX web server.

Leave a Comment

trabzon escort yalova escort Samsun escort izmit escort nazilli escort