How to install PHP 7.2.latest on Ubuntu 16.04/ Ubuntu 18.04/Debian etc/
Advertisement:
I have a number of guides on moving away from CPanel, Setting up VM’s on UpCloud, AWS, Vultr or Digital Ocean along with installing and managing WordPress from the command line. PHP is my programming language of choice.
PHP has a support page that declares the support date ranges and support types: http://php.net/supported-versions.php
A version of PHP is either actively supported, security fix supported or end of life. Read this post to check WordPress for PHP compatibility.
From time to time vulnerabilities come up that require PHP updates to be applied.
Multiple flaw found in #PHP, most severe of which could allow arbitrary code execution
Affected Versions:
PHP 7.2 —prior to 7.2.5
PHP 7.1 —prior to 7.1.17
PHP 7.0 —prior to 7.0.30
PHP 5.0 —prior to 5.6.36https://t.co/TtiqXePoHuUpgrade to the latest version of PHP immediately
— The Hacker News (@TheHackersNews) May 1, 2018
#PHP 7.2.12 has been released https://t.co/iNXGYTs0PX
— Neustradamus (@neustradamus) November 9, 2018
Source Link here
Advertisement:
I have guides on setting up PHP 7 here on Digital Ocean, here on AWS and here on Vultr. I have tried upgrading to PHP 7.1 in the past with no luck (I forgot to change something and rolled back to 7.0).
FYI: I have a guide on setting up PHP child workers so the output from some commands below may be different than yours. Here are the steps I performed to install PHP 7.2 alongside 7.0 then switch. to 7.2.
Backup your system
Do perform a Snapshot or Backup before proceeding. Nothing beats a quick restore if things fail.
Note: Use this information at your own risk.
Updating php 7.2.12 to 7.2.12
Update your Ubuntu systems
1 | apt-get update && apt-get upgrade |
Updating from an older php (e.g 5.x, 7.1, 7.1 to say 7.2.12)
Backup PHP
1 2 | cd /etc/php zip -r php7.0backup.zip 7.0/ |
Install Helper
This software provides an abstraction of the used apt repositories. It allows you to easily manage your distribution and independent software vendor software sources. More Info
1 | apt-get install python-software-properties |
Add the main PHP repo (more information)
1 | add-apt-repository ppa:ondrej/php |
Update the package lists
“In a nutshell, apt-get update
doesn’t actually install new versions of the software. Instead, it updates the package lists for upgrades for packages that need upgrading, as well as new packages that have just come to the repositories.” from here
1 | apt-get update |
List Installed Packages (optional)
1 | dpkg -l |
Install PHP 7.2
1 | apt-get install php7.2 |
Install common PHP modules
1 | apt-get install php-pear php7.2-curl php7.2-dev php7.2-mbstring php7.2-zip php7.2-mysql php7.2-xml |
Install PHP FPM
1 | apt-get install php7.2-fpm |
Update all packages (may be needed to update from php 7.2.4 to 7.2.5)
1 | sudo apt-get upgrade |
Edit your NGINX sites-available config
1 2 | sudo nano /etc/nginx/sites-available/default # I set: fastcgi_pass /run/php/php7.2-fpm.sock; |
Edit your NGINX sites-enabled config
1 2 | sudo nano /etc/nginx/sites-enabled/default # I set: fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; |
I edited these lines
1 2 3 4 5 | location ~ \.php$ { ... fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; ... } |
Edit your PHP config (and make desired changes)
1 | sudo nano /etc/php/7.2/fpm/php.ini |
Edit your PHP pool config file (as required). See this guide here.
e.g.
> cgi.fix_pathinfo=0
> max_input_vars = 1000
> memory_limit = 1024M
> max_file_uploads = 8M
> post_max_size = 8M
1 | sudo nano /etc/php/7.2/fpm/pool.d/www.conf |
Make sure you set: listen = /run/php/php7.2-fpm.sock
Advertisement:
Set PHP 7.2 as the default PHP
1 | update-alternatives --set php /usr/bin/php7.2 |
Check your PHP version
1 | php -v |
Reload PHP
1 | sudo service php7.2-fpm reload |
Reload NGINX
1 2 3 | nginx -t nginx -s reload /etc/init.d/nginx restart |
Check the status of your PHP (and child workers)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | sudo service php7.2-fpm status ● php7.2-fpm.service - The PHP 7.2 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php7.2-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Fri 2018-05-04 19:02:27 AEST; Docs: man:php-fpm7.2(8) Process: 123456 ExecReload=/bin/kill -USR2 $MAINPID (code=exited, status=0/SUCCESS) Main PID: 123456 (php-fpm7.2) Status: "Processes active: 0, idle: 10, Requests: 0, slow: 0, Traffic: 0req/sec" Tasks: 11 Memory: 30.5M CPU: 10.678s CGroup: /system.slice/php7.2-fpm.service ├─16494 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf) ├─16497 php-fpm: pool www ├─16498 php-fpm: pool www ├─16499 php-fpm: pool www ├─16500 php-fpm: pool www ├─16501 php-fpm: pool www ├─16502 php-fpm: pool www ├─16503 php-fpm: pool www ├─16504 php-fpm: pool www ├─16505 php-fpm: pool www └─16506 php-fpm: pool www |
Check your website.
Advertisement:
Troubleshooting
Guides that helped me.
https://thishosting.rocks/install-php-on-ubuntu/
Check your log files
1 | tail /var/log/nginx/error.log |
Debug FPM Service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | systemctl status php7.2-fpm.service ● php7.2-fpm.service - The PHP 7.2 FastCGI Process Manager Loaded: loaded (/lib/systemd/system/php7.2-fpm.service; enabled; vendor preset: enabled) Active: active (running) since Sun 2018-05-06 00:18:55 AEST; 7min ago Docs: man:php-fpm7.2(8) Process: 123456 ExecReload=/bin/kill -USR2 $MAINPID (code=exited, status=0/SUCCESS) Main PID: 123 (php-fpm7.2) Status: "Processes active: 0, idle: 10, Requests: 44, slow: 0, Traffic: 0req/sec" Tasks: 11 Memory: 212.6M CPU: 12.052s CGroup: /system.slice/php7.2-fpm.service ├─438 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf) ├─441 php-fpm: pool www ├─442 php-fpm: pool www ├─443 php-fpm: pool www ├─444 php-fpm: pool www ├─445 php-fpm: pool www ├─446 php-fpm: pool www ├─447 php-fpm: pool www ├─449 php-fpm: pool www ├─450 php-fpm: pool www └─451 php-fpm: pool www May 06 00:18:55 server systemd[1]: Stopped The PHP 7.2 FastCGI Process Manager. May 06 00:18:55 server systemd[1]: Starting The PHP 7.2 FastCGI Process Manager... May 06 00:18:55 server systemd[1]: Started The PHP 7.2 FastCGI Process Manager. |
Remove PHP 7.0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | sudo apt-get purge php7.0-common Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: libaspell15 libauthen-pam-perl libc-client2007e libio-pty-perl libmcrypt4 librecode0 libtidy-0.99-0 libxmlrpc-epi0 linux-headers-4.4.0-109 linux-headers-4.4.0-109-generic linux-headers-4.4.0-112 linux-headers-4.4.0-112-generic linux-headers-4.4.0-87 linux-headers-4.4.0-87-generic linux-headers-4.4.0-96 linux-headers-4.4.0-96-generic linux-image-4.4.0-109-generic linux-image-4.4.0-112-generic linux-image-4.4.0-87-generic linux-image-4.4.0-96-generic linux-image-extra-4.4.0-109-generic linux-image-extra-4.4.0-112-generic linux-image-extra-4.4.0-87-generic linux-image-extra-4.4.0-96-generic mlock Use 'sudo apt autoremove' to remove them. The following packages will be REMOVED: php7.0-cli* php7.0-common* php7.0-curl* php7.0-fpm* php7.0-gd* php7.0-imap* php7.0-intl* php7.0-json* php7.0-mbstring* php7.0-mcrypt* php7.0-mysql* php7.0-opcache* php7.0-pspell* php7.0-readline* php7.0-recode* php7.0-sqlite3* php7.0-tidy* php7.0-xml* php7.0-xmlrpc* php7.0-xsl* |
PHP 7.0 Removed 🙂
Remove other unused packages
1 | sudo apt autoremove |
At the time of writing (November the 18th 2018) PHP 7.2.12 is the latest version of PHP and PHP 7.3 will be out at the end of the year.
Good luck and I hope this guide helps someone
Advertisement:
Ask a question or recommend an article
Revision History
v1.4 Updated the post to mention PHP 7.0 EOL
v1.3 Updated to add PHP 7.2.12 information
v1.2 PHP 7.2.9 and PHP 7.2 updates
v1.1 Remove PHP 7.0 steps
v1.0 Initial post