How to Install PHP 7.3 on NGINX/Apache2 in Ubuntu 18.04/18.10

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, avrg: 5.00)

PHP (Hypertext Preprocessor) is a scripting language on server-side, it’s designed for Web development and making dynamic and interactive Web pages. PHP is the most popular scripting language that supported by the community around the world, many programmers said PHP is a dynamically, flexibility typed language and no hard on structure coding. Learn how to install PHP 7.3 on NGINX or Apache2

The latest version of PHP (PHP7.3) has fixed security-related issues and was fixed a lot of inconsistencies and fatal errors as well. Users are encouraged to install and upgrade to this version.

Prerequisites

Make sure you have already done Installed Ubuntu and running web server NGINX or Apache


Step 1: Add PPA Third-party Repositories

When this tutorial made the PHP 7.3 is not yet available in Ubuntu default repositories, for installing it we have to add manually ondrej/php PPA by running the following command

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php

After adding the PPA we need to update Ubuntu, run the following command

sudo apt-get update

Step 2: Install PHP 7.3 for NGINX or Apache

Once the PPA repository has been added and updated, now we are going to install PHP 7.3

Install PHP 7.3 for NGINX and Related Modules

 sudo apt-get install php7.3 php7.3-cli php7.3-mysql php7.3-gd php7.3-imagick php7.3-recode php7.3-tidy php7.3-xmlrpc php7.3-common php7.3-curl php7.3-mbstring php7.3-xml php7.3-bcmath php7.3-bz2 php7.3-intl php7.3-json php7.3-readline php7.3-zip

Install PHP 7.3 for Apache and Related Modules

 sudo apt-get install php7.3 libapache2-mod-php7.3 php7.3-cli php7.3-mysql php7.3-gd php7.3-imagick php7.3-recode php7.3-tidy php7.3-xmlrpc php7.3-common php7.3-curl php7.3-mbstring php7.3-xml php7.3-bcmath php7.3-bz2 php7.3-intl php7.3-json php7.3-readline php7.3-zip

To verified PHP 7.3 was installed correctly on NGINX or Apache, you may check PHP version

sudo php -v

You will be presented on the terminal screen as below

PHP 7.3.0-2+ubuntu18.10.1+deb.sury.org+1 (cli) (built: Dec 17 2018 09:23:19) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.0-dev, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.0-2+ubuntu18.10.1+deb.sury.org+1, Copyright (c) 1999-2018, by Zend Technologies

Step 3: Configure PHP 7.3 for NGINX or Apache

You may need to configure PHP for the web server, if your web server NGINX please follow configuration for NGINX and if your server Apache please follow configuration for Apache2

Configuration for NGINX (Option)

The default location of the PHP socket that Nginx List the contents for the directory /var/run/php/

ls /var/run/php/

You will see the output like below

php7.3-fpm.pid php7.3-fpm.sock

now edit server block configuration

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

Let’s see below configuration, on the red line you may change name_server with your IP address or your domain

server {
    listen 80;
    listen [::]:80;
    root /var/www/html/;
    index  index.php index.html index.htm;
    server_name YOUR_IP_ADDRESS OR DOMAIN;

    location / {
    try_files $uri $uri/ /index.php?$args;        
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
        fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
    }

}

Press Ctrl/Cmd + X and then press Y and ENTER to save changes

Create file  info.php into the root directory as the above configuration

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

Fill it with code for PHP information

<?php phpinfo(); ?>

Press Ctrl/Cmd + X and then press Y and ENTER to save changes

You can now view this page in your web browser by visiting your server’s domain name or public IP address followed by /info.php: http://YOUR_DOMAIN_or_IP/info.php

Configuration for Apache2 (Option)

The default document root directory is located in /var/www/html/, create a file using the nano editor

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

Fill it with code for PHP information

<?php phpinfo(); ?>

Press Ctrl/Cmd + X and then press Y and ENTER to save changes

You can now view this page in your web browser by visiting your server’s domain name or public IP address followed by /info.php: http://YOUR_DOMAIN_or_IP/info.php you will be presented PHP info on the screen like above


Step 4: Configure PHP file (Optional)

The php.ini file is a default configuration file that read on PHP starts up. If you want to change the PHP settings on Ubuntu, php.ini file is an easy and most effective way to do it. Follow the below steps to modify the PHP configuration file (php.ini) using the command line on Ubuntu.

For the NGINX web server use the following command

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

For the Apache web server use the following command

sudo nano /etc/php/7.3/apache2/php.ini

To edit the configuration file your may find or you can use Cmd/Ctrl W to search and replace it as the following

error_reporting = E_COMPILE_ERROR | E_RECOVERABLE_ERROR | E_ERROR | E_CORE_ERROR
max_input_time = 30
error_log = /var/log/php/error.log
file_uploads = On
allow_url_fopen = On
memory_limit = 256M
upload_max_filesize = 100M
max_execution_time = 360
date.timezone = America/Los_Angeles

Press Ctrl/Cmd + X and then press Y and ENTER to save changes

Changes made in the NGINX or Apache configuration file will not be applied until the command to Restart

Restart for NGINX web server

sudo service nginx restart

Restart for Apache2 web server

sudo systemctl reload apache2

You may also need to restart PHP service

sudo systemctl restart php7.3-fpm.service

Step 5: Troubleshooting (Optional)

In the case upgrade from your current version to PHP 7.3 and if you wish to have multiple installed PHP, you need to set one for default, you may change the version of PHP with the red marked below

update-alternatives --set php /usr/bin/php7.3

If the above would not work or error you may remove PHP, change the version of PHP with the red marked that you want to remove

sudo apt purge php7.2

If this tutorial could help you, please rate above Star button rating and share to help others find it! Feel free to leave a comment below.

Recommended For You

Axfon

About the Author: Axfon

Team committed to share our IT skills and experience through our website which may assist to formulate a task easy