Now Reading
How To Install PHP For Nginx On Ubuntu 20.04 LTS
Dark Light

How To Install PHP For Nginx On Ubuntu 20.04 LTS

Avatar
turned-on MacBook Pro

This tutorial explains all the steps required to install the default distribution of PHP 7 on Ubuntu 20.04 LTS. The steps should be similar to other Linux based systems. It also explains how to configure PHP for NGINX Web Server with FastCGI. Prerequisites This tutorial expects that the Nginx is already installed and you have followed the steps to configure the server blocks using separate files for each block as explained at How To Install And Configure Nginx on Ubuntu 20.04 LTS. Install PHP 7 In this step, we will install PHP 7.4, the default PHP package available for Ubuntu 20.04 LTS release. It can be installed using the command as shown below. Notes: Nginx needs PHP FPM to process the PHP requests. Use the below-mentioned command to install PHP 7 only if required. It also install the Apache Web Server which is not required by Nginx. # Refresh indexessudo apt update# Install PHP 7.4 on Ubuntu 20.04 LTSsudo apt install php7.4# Verify PHPphp –version# OutputPHP 7.4.3 (cli) (built: May 26 2020 12:24:22) ( NTS )Copyright (c) The PHP GroupZend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.3, Copyright (c), by Zend Technologies It will ask to confirm the installation. Press Y and hit Enter to confirm the installation. The important paths and files of the PHP installation are /usr/lib/php/7.4, /usr/bin/php7.4, and /etc/php/7.4/cli/php.ini. Install PHP FPM Use the below-mentioned commands to install the PHP FPM required by Nginx to process the PHP requests. # Install PHP FPM – Required by Nginxsudo apt install php7.4-fpm# Check whether enabled on system bootsudo systemctl is-enabled php7.4-fpm# Outputenable Also, make sure that PHP-FPM is active and running. PHP FPM Statussudo systemctl status php7.4-fpm# Output● php7.4-fpm.service – The PHP 7.4 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Thu 2020-06-06 05:36:17 UTC; 24s ago Docs: man:php-fpm7.4(8) Process: 33932 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/7.4/fpm/pool.d/www.conf 74 (code=exited,> Main PID: 33919 (php-fpm7.4) Status: “Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec” Tasks: 3 (limit: 1119) Memory: 11.3M CGroup: /system.slice/php7.4-fpm.service ├─33919 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf) ├─33930 php-fpm: pool www └─33931 php-fpm: pool www—– Install MySQL Extension Use the below-mentioned commands to install the MySQL extension for PHP 7. Also, refer to How To Install MySQL 8 on Ubuntu 20.04 LTS. # Install MySQL Extensionsudo apt install php7.4-mysql# Install MySQL Extension – Required for WordPress Installationsudo apt install php-mysql Additional Extensions You can also install the additional extensions in order to fully support a web application. Some of these packages might not be required in your scenario, hence it’s totally optional to install these extensions. # Install CGI and CLI if not installed by defaultsudo apt install php7.4-cgi php7.4-cli # Install CURL and JSON extensionssudo apt install php7.4-curl php7.4-json# Install PHP GD and Imagicksudo apt install php7.4-gd php-imagick# Multibyte String, Internationalization and Spell Checksudo apt install php7.4-mbstring php7.4-intl php7.4-pspell# Multibyte String, Internationalization and Spell Check – Required for WordPress Installationsudo apt install php-mbstring php-intl php-pspell# Emailssudo apt install php7.4-imap# SQLite Databasesudo apt install php7.4-sqlite3 # Tidy and XML RPCsudo apt install php7.4-tidy php7.4-xmlrpc# Excelsudo apt install php7.4-xsl Install OPcache to enable caching at the bytecode level. # Install OPcache extensionsudo apt install php7.4-opcache Install the extensions to handle compressed files. # Install Zipsudo apt install php7.4-zip These are the steps required to install PHP 7 on Ubuntu 20.04 LTS and the additional packages that are mostly required for the website development. Configure PHP Processor We can secure PHP by updating the configuration as shown below. # Update PHP Configurationsudo nano /etc/php/7.4/fpm/php.ini# Updatecgi.fix_pathinfo=0# Reload PHP FPMsudo systemctl reload php7.4-fpm# OR # Restart PHP FPMsudo systemctl restart php7.4-fpm# Test Statussudo systemctl status php7.4-fpm# Output ● php7.4-fpm.service – The PHP 7.4 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Sat 2020-06-06 16:47:58 IST; 2min 45s ago Docs: man:php-fpm7.4(8) Process: 38840 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/7.4/fpm/pool.d/www.> Process: 39283 ExecReload=/bin/kill -USR2 $MAINPID (code=exited, status=0/SUCCESS) Main PID: 38814 (php-fpm7.4) Status: “Ready to handle connections” Tasks: 3 (limit: 4624) Memory: 14.7M CGroup: /system.slice/php7.4-fpm.service ├─38814 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf) ├─39284 php-fpm: pool www └─39285 php-fpm: pool www——– Important Commands Below listed are the commands used to start, stop, enable, or disable the PHP FPM process. # Start PHP FPMsudo systemctl start php7.4-fpm# Stop PHP FPMsudo systemctl stop php7.4-fpm# Restart PHP FPMsudo systemctl restart php7.4-fpm# Enable PHP FPMsudo systemctl enable php7.4-fpm# Disable PHP FPMsudo systemctl disable php7.4-fpm We can also confirm whether PHP is running using the netstat command as shown below. # Test PHPnetstat -pl | grep php# Output(Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) unix 2 [ ACC ] STREAM LISTENING 619439 – /run/php/php7.4-fpm.sock Nginx User Permissions We have to configure the Nginx system user in order to access the PHP via FPM. Check the system user allowed to access PHP FPM as shown below. # Check FPM Usersudo nano /etc/php/7.4/fpm/pool.d/www.conf# Confirm the configurationlisten.owner = www-datalisten.group = www-datalisten.mode = 0660# Restart FPMsudo systemctl restart php7.4-fpm We also need to update the Nginx user by updating the configuration as shown below. # Update Nginx usersudo nano /etc/nginx/nginx.conf# Update# user nginx;user www-data;……# Test Nginxsudo nginx -t# Restart Nginxsudo systemctl restart nginx If the Nginx user is not configured, it will log the error as shown below. # Nginx Permission Error2020/06/06 14:37:20 [crit] 27012#27012: *8 connect() to unix:/run/php/php7.4-fpm.sock failed (13: Permission denied) while connecting to upstream, Configure Nginx – Domain Server Block Now we will configure Nginx to access PHP via FPM since Nginx does not support PHP by default. We will update the domain-specific virtual host or server block as part of this step. In this way, we can enable PHP only for the selected sites instead of enabling it by default for the sites. The default configuration of a site without PHP configuration looks like the one as shown below. You can follow Configure Virtual Host Or Server Block On Nginx to configure domain-specific virtual host or server block. # Server Block without PHP FPMsudo nano /etc/nginx/sites-available/example.comserver {    server_name  example.com www.example.com;    #charset koi8-r;    #access_log  /var/log/nginx/host.access.log  main;    location / {        root   /var/www/example.com/html;        index  index.html index.htm;    }    …    …}# Create Symbolic Linksudo ln -s /etc/nginx/sites-available/mydomain.com /etc/nginx/sites-enabled/mydomain.com# Disable Site (only if required)sudo unlink /etc/nginx/sites-enabled/mydomain.com Now configure the server block to process all the PHP requests. # Server Block without PHP FPMsudo nano /etc/nginx/sites-available/example.comserver {        server_name  example.com www.example.com;    #charset koi8-r;    #access_log  /var/log/nginx/host.access.log  main;    location / {        root   /var/www/example.com/html;        index index.html index.htm;    }    …    … # pass the PHP scripts to FastCGI    location ~ .php$ {    root /var/www/example.com/html;    fastcgi_intercept_errors on;     fastcgi_pass unix:/run/php/php7.4-fpm.sock;     fastcgi_index index.php;     fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;     include fastcgi_params;    } # deny access to .htaccess files    location ~ /.ht {     deny all;    }    …    …}# Reload NGINXsudo systemctl reload nginx Now we will create the PHP file to print the output of the phpinfo() function as shown below. # Add PHP Filesudo nano /var/www/example.com/html/info.php# Content How To Install PHP For Nginx On Ubuntu 20.04 LTS

Touch The Stars

0 / 5

Your page rank:

What's Your Reaction?
Excited
0
Funny
0
In Love
0
Like It
0
Not Sure
0
View Comments (0)

Leave a Reply

Your email address will not be published.


Copyrights © 2021 Lucid.Hive.Systems. All Rights Reserved.