Installing PHP on Ubuntu

Standard

How to install PHP 7 on Ubuntu 16.04

Currently, as of January 2018, the default PHP release in the Ubuntu 16.04 repositories is PHP 7.0. We’ll show you how to install it using Ubuntu’s repository.

Update Ubuntu

First, before you do anything else, you should update your Ubuntu server:

apt-get update && apt-get upgrade

Install PHP

Next, to install PHP, just run the following command:

apt-get install php

This command will install PHP 7.0, as well as some other dependencies:

  • php-common
  • php7.0
  • php7.0-cli
  • php7.0-common
  • php7.0-fpm
  • php7.0-json
  • php7.0-opcache
  • php7.0-readline

To verify if PHP is installed, run the following command:

php -v

You should get a response similar to this:

php -v

And that’s it. PHP is installed on your Ubuntu server.

Install PHP 7.0 modules

You may need some additional packages and PHP modules in order for PHP to work with your applications. You can install the most commonly needed modules with:

apt-get install php-pear php7.0-dev php7.0-zip php7.0-curl php7.0-gd php7.0-mysql php7.0-mcrypt php7.0-xml libapache2-mod-php7.0

Depending on how and what you’re going to use, you may need additional PHP modules and packages. To check all the PHP modules available in Ubuntu, run:

apt-cache search --names-only ^php

You can tweak the command to only show ^php7.0- packages etc.

If you want to use the latest PHP version, follow the next instructions instead.

How to Install PHP 7.2 on Ubuntu 16.04

PHP 7.2 is the latest stable version of PHP and has many new features, improvements, and bug fixes. You should definitely use it if you want a better, faster website/application.

Update Ubuntu

Of course, as always, first update Ubuntu:

apt-get update && apt-get upgrade

Add the PHP repository

You can use a third-party repository to install the latest version of PHP. We’ll use the repository by Ondřej Surý.

First, make sure you have the following package installed so you can add repositories:

apt-get install software-properties-common

Next, add the PHP repository from Ondřej:

add-apt-repository ppa:ondrej/php

And finally, update your package list:

apt-get update

Install PHP 7.2

After you’ve added the repository, you can install PHP 7.2 with the following command:

apt-get install php7.2

This command will install additional packages:

  • libapache2-mod-php7.2
  • libargon2-0
  • libsodium23
  • libssl1.1
  • php7.2-cli
  • php7.2-common
  • php7.2-json
  • php7.2-opcache
  • php7.2-readline

And that’s it. To check if PHP 7.2 is installed on your server, run the following command:

php -v

Install PHP 7.2 modules

You may need additional packages and modules depending on your applications. The most commonly used modules can be installed with the following command:

apt-get install php-pear php7.2-curl php7.2-dev php7.2-gd php7.2-mbstring php7.2-zip php7.2-mysql php7.2-xml

And that’s all. You can now start using PHP on your Ubuntu server.

If you want to further tweak and configure your PHP, read our instructions below.

How to Install PHP 7.2 on Ubuntu 18.04

PHP 7.2 is included by default in Ubuntu’s repositories since version 18.04. So the instructions are pretty similar to PHP 7 for 16.04.

Update Ubuntu

Again, before doing anything, you should update your server:

apt-get update && apt-get upgrade

Install PHP 7.2

Next, to install PHP 7.2 on Ubuntu 18.04, just run the following command:

apt-get install php

This command will install PHP 7.2, as well as some other dependencies.

To verify if PHP is installed, run the following command:

php -v

You should get a response similar to this:

PHP 7.2.3-1ubuntu1 (cli) (built: Mar 14 2018 22:03:58) ( NTS )

And that’s it. PHP 7.2 is installed on your Ubuntu 18.04 server.

Install PHP 7.2 modules

These are the most common PHP 7.2 modules often used by php applications. You may need more or less, so check the requirements of the software you’re planning to use:

apt-get install php-pear php-fpm php-dev php-zip php-curl php-xmlrpc php-gd php-mysql php-mbstring php-xml libapache2-mod-php

To check all the PHP modules available in Ubuntu, run:

apt-cache search --names-only ^php

How to install PHP 7.3 on Ubuntu 18.04 or 16.04

PHP 7.3 is still under development, but a stable release should be available at the end of 2018. You can install it now if you want to test things out.

Update Ubuntu

First, update your Ubuntu server:

apt-get update && apt-get upgrade

Add the PHP repository

To install PHP 7.3 you’ll need to use a third-party repository. We’ll use the repository by Ondřej Surý that we previously used.

First, make sure you have the following package installed so you can add repositories:

apt-get install software-properties-common

Next, add the PHP repository from Ondřej:

add-apt-repository ppa:ondrej/php

And finally, update your package list:

apt-get update

Install PHP 7.3

After you’ve added the repository, you can install PHP 7.3 with the following command:

apt-get install php7.3

This command will install additional packages:

  • libapache2-mod-php7.3
  • libaprutil1-dbd-sqlite3
  • php7.3-cli
  • php7.3-common
  • php7.3-json
  • php7.3-opcache
  • php7.3-readline
  • …and others.

And that’s it. To check if PHP 7.3 is installed on your server, run the following command:

php -v

Install PHP 7.3 modules

You may need additional packages and modules depending on your applications. The most commonly used modules can be installed with the following command:

apt-get install php-pear php7.3-curl php7.3-dev php7.3-gd php7.3-mbstring php7.3-zip php7.3-mysql php7.3-xml

And that’s all. You can now start using PHP on your Ubuntu server.

If you want to further tweak and configure your PHP, read our instructions below.

How to change the PHP version you’re using

If you have multiple PHP versions installed on your Ubuntu server, you can change what version is the default one.

To set PHP 7.0 as the default, run:

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

To set PHP 7.2 as the default, run:

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

To set PHP 7.3 as the default, run:

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

If you’re following our LAMP tutorials and you’re using Apache, you can configure Apache to use PHP 7.2 with the following command:

a2enmod php7.2

And then restart Apache for the changes to take effect:

systemctl restart apache2

How to upgrade to PHP 7.2 or 7.3 on Ubuntu

If you’re already using an older version of PHP with some of your applications, you can upgrade by:

  1. Backup everything.
  2. Install the newest PHP and required modules.
  3. Change the default version you’re using.
  4. (Optionally) Remove the older PHP
  5. (Required) Configure your software to use the new PHP version. You’ll most likely need to configure Nginx/Apache, and many other services/applications. If you’re not sure what you need to do, contact professionals and let them do it for you.

Speed up PHP by using an opcode cache

You can improve the performance of your PHP by using a caching method. We’ll use APCu, but there are other alternatives available.

If you have the ‘php-pear’ module installed (we included it in our instructions above), you can install APCu with the following command:

pecl install apcu

There are also other ways you can install APCu, including using a package.

To start using APCu, you should run the following command for PHP 7.2:

echo "extension=apcu.so" | tee -a /etc/php/7.2/mods-available/cache.ini

Or this command for PHP 7.3:

echo "extension=apcu.so" | tee -a /etc/php/7.3/mods-available/cache.ini

And the following command for PHP 7.0:

echo "extension=apcu.so" | tee -a /etc/php/7.0/mods-available/cache.ini

If you’re following our LAMP tutorials and you’re using Apache, create a symlink for the file you’ve just created.

For PHP 7.2:

ln -s /etc/php/7.2/mods-available/cache.ini /etc/php/7.2/apache2/conf.d/30-cache.ini

For PHP 7.3:

ln -s /etc/php/7.3/mods-available/cache.ini /etc/php/7.3/apache2/conf.d/30-cache.ini

For PHP 7.0:

ln -s /etc/php/7.0/mods-available/cache.ini /etc/php/7.0/apache2/conf.d/30-cache.ini

And finally, reload Apache for the changes to take effect:

systemctl restart apache2

To further configure APCu and how it works, you can add some additional lines to the cache.ini file you previously created. The best configuration depends on what kind of server you’re using, what applications you are using etc. Either google it and find a configuration that works for you, or contact professionals and let them do it for you.

That’s it for our basic setup. Of course, there are much more options and configurations you can do, but we’ll leave them for another tutorial.

Advertisements

Build custom systemctl service command

Standard

Let’s create a service command in linux to execute the Laravel queue listen command.

php artisan queue:listen

Lets start 🙂

STEP 1: Create bash script for Laravel php artisan queue:listen

Create a bash script queue.sh to any preferred location in server. I preferred to create in /root as a root user.

$ sudo su –

$ vim /root/queue.sh

 

In queue.sh file

#!/bin/bash

cd /path/to/laravel/project

php artisan queue:listen

 

Save the file and change the file mode executable.

$ chmod +x /root/queue.sh

STEP 2: Create .service

Create a service .service file named ays_queue.service. 

$ sudo vim  /etc/systemd/system/ays_queue.service

In ays_queue.service file

[Unit]
Description=AYS Laravel queue listen service

[Service]
ExecStart=/bin/sh /root/queue.sh

[Install]
WantedBy=multi-user.target

Here, ‘WantedBy=multi-user.target’ refers the service create can can be executed by any user in the system.

Now, once .service file created the system should reload the daemon.

$ systemctl daemon-reload

To verify, the system service working following commands can be executed

$ systemctl status ays_queue
$ systemctl start ays_queue
$ systemctl stop ays_queue
$ systemctl restart ays_queue

STEP 4: Check and re-start command

In advance, one more step to check the command and start the service in case of inactive.

Create a new bash file again in /root named check_ays_queue.sh

$ sudo vim /root/check_ays_queue.sh

In check_ays_queue.sh file

#!/bin/bash

CHECKQUEUE=$(systemctl is-active ays_queue)

if [ $CHECKQUEUE = ‘active’ ]; then
echo ‘active’
else
systemctl restart ays_queue
fi

Also, make check_queue.sh file executable.

$ sudo chmod +x /root/check_ays_queue.sh

You can add check_ays_queue.sh in crontabs to check the status and restart the service in case of inactive. In this way, your service will never stops.

 

Enjoy!!

 

Magento 2 – System Requirments (REF)

Standard

System Requirements

For the most up-to-date information, see System Requirements in the developer documentation.

SYSTEM REQUIREMENTS

ENVIRONMENT

Operating System

Linux x86-64

Composer

Composer is required for developers who want to contribute to code base, or develop extensions.

Web Server

Apache 2.2 or 2.4

The apache mod_rewrite module must be enabled. To learn more, see: Apache.

Nginx 1.8.x

(or latest mainline version)

PHP

Supported

Not Supported

7.0.2

7.0.0

7.0.4

7.0.1

7.0.6 – 7.0.x

7.0.3

7.1.x

7.0.5

Required PHP Extensions:

bc-math (Magento Commerce only)

curl

gdImageMagick 6.3.7 (or later) or both

intl

mbstring

mcrypt

mhash

openssl

PDO_MySQL

SimpleXML

soap

xml

xsl

zip

Optional, but recommended

opcache

This extension is bundled in many PHP distributions. To verify, see: CentOS or Ubuntu.

php_xdebug2.2.0or later

Recommended for development environments only.

Additional configuration:

safe_mode off

memory_limit minimum 512 MB

Database

MySQL 5.7or 5.6.x

Compatible with MariaDB and Percona

Magento Enterprise Edition 2.x can use three master databases to provide scalability for the different functional areas of checkout, orders, and product data.

SSL

A valid security certificate is required for HTTPS.

Self-signed certificates are not supported.

Transport Layer Security (TLS) Requirements:

TSL 1.1 or later

PayPal and repo.magento.com require TSL 1.1 or later.

Mail Server

Mail Transfer Agent (MTA) or SMTP server

 

 

Supported Applications

NAME

DESCRIPTION

Reverse Proxy / Web Accelerator

Varnish 3.5

Varnish 4.x (or latest stable version.)

Cache Storage

Redis 3.x

Memcache 1.4.x

Session Storage

memcached latest stable version for session storage with either memcache or memcached PHP extensions (latest stable version)

Search

Elasticsearch

Versions 1.7 and 2.x (recommended)

2.x supported from Linux repository.

2.0 branch supported from PHP repository.

Apache Solr

(Enterprise Edition only)

Messaging

RabbitMQ (Enterprise Edition only)

 

Supported Browsers

BROWSER

VERSION

OS

STOREFRONT/ADMIN

Firefox

Latest, latest -1*

Any

Chrome

Latest, latest -1

Any

Safari

Latest, latest -1

Mac OS

Microsoft Edge

Version 11 or later

Windows

STOREFRONT ONLY

Internet Explorer

Version 9 or later

Windows

DESKTOP STOREFRONT

Safari Mobile

iPad 2

iPad Mini

iPad with Retina Display

OS 7 or later

MOBILE STOREFRONT

Safari Mobile

iPhone 4 or later

IOS 7 or later

Chrome for Mobile

Latest, latest -1

Android 4 or later

Ref: http://docs.magento.com/m2/ce/user_guide/magento/system-requirements.html

Install multiple PHP versions in Ubuntu

Standard

PHP (recursive acronym for PHP: Hypertext Preprocessor) is

  • an open source, popular general-purpose scripting language that is widely-used
  • best suited for developing websites and web-based applications
  • server-side scripting language that can be embedded in HTML

Currently, there are 3 PHP version ie PHP 5.67.0 and 7.1 are supported versions of PHP.

PHP 5.35.4 and 5.5 are no longer supported with security updates.

Step 1

1.1. Add PPA called Ondřej Surý PPA which will let you install different versions of PHP – PHP 5.6PHP 7.0, PHP 7.1 and PHP 7.2 on Ubuntu.

$ sudo apt install python-software-properties
$ sudo add-apt-repository ppa:ondrej/php

NOTE: If you have already added this PPA, you can jump up to step 2. To check if Ondřej Surý PPA is added or not, here is a command you can try:

$ ls /etc/apt/sources.list.d/ | grep ondrej

1.2. Next, update the system as follows.

$ sudo apt-get update

Step 2

2.1. To install different supported versions of PHP as follows.

------------------- For Apache Web Server -------------------
$ sudo apt install php5.6   [PHP 5.6]
$ sudo apt install php7.0   [PHP 7.0]
$ sudo apt install php7.1   [PHP 7.1]
------------------- For Nginx Web Server -------------------
$ sudo apt install php5.6-fpm   [PHP 5.6]
$ sudo apt install php7.0-fpm   [PHP 7.0]
$ sudo apt install php7.1-fpm   [PHP 7.1]

 

2.2. To install any PHP modules, simply specify the PHP version and use the auto-completion functionality to view all modules as follows.

------------ press [Tab] key for auto-completion ------------ 
$ sudo apt install php5.6 
$ sudo apt install php7.0 
$ sudo apt install php7.1

2.3. You can install most required PHP modules from the list.

------------ Install PHP Modules ------------
$ sudo apt install php5.6-cli php5.6-xml php5.6-mysql 
$ sudo apt install php7.0-cli php7.0-xml php7.0-mysql 
$ sudo apt install php7.1-cli php7.1-xml php7.1-mysql

Step 3: Set Default PHP Version in Ubuntu

3.1. Finally, verify your default PHP version used on your system like this.

$ php -v

yogal@yogalshrestha:~$ php -v
PHP 7.1.13-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Jan 5 2018 13:26:45) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.1.13-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies

3.2. To set the default PHP version to be used on the system with the update-alternatives command.

------------ Set Default PHP Version 5.6 ------------
$ sudo update-alternatives --set php /usr/bin/php5.6

yogal@yogalshrestha:~$ sudo update-alternatives –set php /usr/bin/php5.6
update-alternatives: using /usr/bin/php5.6 to provide /usr/bin/php (php) in manual mode
yogal@yogalshrestha:~$ php -v
PHP 5.6.32-1+ubuntu16.04.1+deb.sury.org+2 (cli)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

------------ Set Default PHP Version 7.1 ------------
$ sudo update-alternatives --set php /usr/bin/php7.1

yogal@yogalshrestha:~$ sudo update-alternatives –set php /usr/bin/php7.1
update-alternatives: using /usr/bin/php7.1 to provide /usr/bin/php (php) in manual mode
yogal@yogalshrestha:~$ php -v
PHP 7.1.13-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Jan 5 2018 13:26:45) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.1.13-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies

3.3. To set the PHP version in Apache web server.

  • Firstly, disable the current version with the a2dismod command
  • Then enable the one you want with the a2enmod command.
$ sudo a2dismod php7.1
$ sudo a2enmod php4.6
$ sudo systemctl restart apache2

3.4. After switching from one version to another, you can find your PHP configuration file, by running the command below.

------------ For PHP 5.6 ------------
$ sudo update-alternatives --set php /usr/bin/php5.6
$ php -i | grep "Loaded Configuration File"
------------ For PHP 7.0 ------------
$ sudo update-alternatives --set php /usr/bin/php7.0
$ php -i | grep "Loaded Configuration File"
------------ For PHP 7,1 ------------
$ sudo update-alternatives --set php /usr/bin/php7.1
$ php -i | grep "Loaded Configuration File"

yogal@yogalshrestha:~$ php -i | grep “Loaded Configuration File”
Loaded Configuration File => /etc/php/7.1/cli/php.ini

 

 

How To Install Linux, nginx, MySQL, PHP 5.6 (LEMP) stack on Ubuntu 16.04

Standard

Step 1: Install the Nginx Web Server

$ sudo apt-get update

$sudo apt-get install nginx

Enable UFW (Ubuntu Firewall)

$ sudo ufw enable

$ sudo ufw status

$ sudo ufw allow ‘Nginx HTTP’

Step 2: Install MySQL

$ sudo apt-get install mysql-server

Step 3: Install PHP 5.6

Use the following set of command to add PPA for PHP 5.6 in your Ubuntu system and install PHP 5.6.

$ sudo apt-get install python-software-properties
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt-get update
$ sudo apt-get install -y php5.6

Check Installed PHP Version:

$ php -v 

PHP 5.6.29-1+deb.sury.org~xenial+1 (cli)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

You can install php5.6 modules too for example

sudo apt-get install php5.6-mbstring php5.6-mcrypt php5.6-mysql php5.6-xml

Step 4: Install PHP5.6-FPM

To install PHP-FPM, open terminal and type in these commands. We will configure the details of nginx and php details in the next step:

$ sudo apt-get install php5.6-fpm

We need to make another small change in the php5-fpm configuration.Open up http://www.conf:

 sudo nano /etc/php/5.6/fpm/pool.d/www.conf

Find the line, listen = 127.0.0.1:9000, and change the 127.0.0.1:9000 to /run/php/php5.6-fpm.sock
.

listen = /run/php/php5.6-fpm.sock

Save and Exit.

Restart php-fpm:

/etc/init.d/php5.6-fpm restart

Step 5: Configure Nginx to Use the PHP Processor

sudo nano /etc/nginx/conf.d/default.conf

Currently, with the comments removed, the Nginx default server block file looks like this:

/etc/nginx/conf.d/default.conf
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
        try_files $uri $uri/ =404;
    }
    location ~ \.php$ {
           try_files $uri =404;
           fastcgi_pass unix:/run/php/php5.6-fpm.sock;
           fastcgi_index index.php;
           fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
           include fastcgi_params;
                
     }
}