Upgrade PHP version to PHP 7.4 on Ubuntu. You can upgrade your current PHP version to the latest release PHP 7.4 on your Ubuntu 18.04.
This upgrade is tested on virtual machine instance running Ubuntu 18.04 OS on Google Cloud Compute Engine. So the steps mentioned in this guide works on any cloud servers like AWS, DigitalOcean, Linode, Vultr or any VPS or Dedicated servers running Ubuntu 18.04.
Here is a brief guide to show you how to install and upgrade to PHP 7.4 on Ubuntu 18.04 LTS with Apache and PHP7.4-fpm with Nginx.
You can follow this guide to Upgrade to PHP 8
Add PPA for PHP 7.4
Add the ondrej/php
which has PHP 7.4 package and other required PHP extensions.
sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt update
Once you have added the PPA you can install PHP 7.4.
Install PHP 7.4 for Apache
Execute the following command to install PHP 7.4
sudo apt install php7.4
Install PHP 7.4 Extensions
Installing PHP extensions are simple with the following syntax.
sudo apt install php7.4-extension_name
Now, install some commonly used php-extensions
with the following command.
sudo apt install php7.4-common php7.4-mysql php7.4-xml php7.4-xmlrpc php7.4-curl php7.4-gd php7.4-imagick php7.4-cli php7.4-dev php7.4-imap php7.4-mbstring php7.4-opcache php7.4-soap php7.4-zip php7.4-intl -y
After the installation has completed, you can confirm the installation using the following command
php -v
Enable PHP 7.4 for Apache
Now you need to tell Apache to use the installed version of PHP 7.4 by disabling the old PHP module (below I have mentioned php7.0, you need to use your current php version used by Apache) and enabling the new PHP module using the following command.
sudo a2dismod php7.0 sudo a2enmod php7.4
Restart Apache for the changes to take effect.
sudo service apache2 restart
Install PHP 7.4 FPM for Nginx
For Nginx you need to install FPM, execute the following command to install PHP 7.4 FPM
sudo apt install php7.4-fpm
Follow the same method above mentioned to install the extensions
After the installation has completed, confirm that PHP 7.4 FPM has installed correctly with this command
php-fpm7.4 -v
Modify Nginx configuration to use PHP 7.4
For Nginx you need to update the PHP-FPM socket in your Nginx configration located inside the sites-available
directory. This will be located inside the location
block location ~ \.php$
Edit your configuration…
sudo nano /etc/nginx/sites-available/your.conf
The line you need to modify will look like this…
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
You need to replace the old PHP version with the new version.
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
Test your configration.
sudo nginx -t
Save the file and exit the editor and restart Nginx for the changes to take effect.
sudo service nginx restart
Configure PHP 7.4
Now we configure PHP for Web Applications by changing some values in php.ini
file.
For PHP 7.4 with Apache the php.ini
location will be in following directory.
sudo nano /etc/php/7.4/apache2/php.ini
For PHP 7.4 FPM with Nginx the php.ini
location will be in following directory.
sudo nano /etc/php/7.4/fpm/php.ini
Hit F6
for search inside the editor and update the following values for better performance.
upload_max_filesize = 32M
post_max_size = 48M
memory_limit = 256M
max_execution_time = 600
max_input_vars = 3000
max_input_time = 1000
Once you have modified your PHP settings you need to restart your Apache for the changes to take effect.
Configure PHP 7.4 FPM Pools
PHP 7.4 FPM allows you to configure the user
and group
that the service will run under. You can modify these with these commands
sudo nano /etc/php/7.4/fpm/pool.d/www.conf
Change the following lines by replacing the www-data with your username
.
user = username group = username listen.owner = username listen.group = username
Hit CTRL+X
and Y
to save the configuration and check if the configuration is correct and restart PHP.
Restart PHP 7.4 FPM
Once you have updated your PHP FPM settings you need to restart it to apply the changes.
sudo php-fpm7.4 -t sudo service php7.4-fpm restart
Prepare yourself for a role working as an Information Technology Professional with Linux operating system
Conclusion
Now you have learned how to upgrade PHP to PHP 7.4 on Ubuntu 18.04.
Thanks for your time. If you face any problem or any feedback, please leave a comment below.
28 Comments
thanks for your help, that was quite easy to update php version with this article
Thank you for using Cloudbooklet
Wow, after looking all over the web and at least two dozen different and very confusing page results, I thankfully found this article — so straight-forward and simple I almost couldn’t believe it was going to work! Easy-peasy, thank you!
Very glad to hear, this post helps you Mike. Welcome
Thanks, this helps me a lot!
Very welcome here, Thank you too for using Cloudbooklet
Can’t get around the following error:
sudo a2enmod php7.4
Considering dependency mpm_prefork for php7.4:
Considering conflict mpm_event for mpm_prefork:
ERROR: Module mpm_event is enabled – cannot proceed due to conflicts. It needs to be disabled first!
Considering conflict mpm_worker for mpm_prefork:
ERROR: Could not enable dependency mpm_prefork for php7.4, aborting
-Callahan-
sudo update-alternatives –set php /usr/bin/php7.4
Fixed it!!
sudo a2dismod mpm_event
Module mpm_event disabled.
To activate the new configuration, you need to run:
systemctl restart apache2
[email protected]:/etc# sudo a2enmod php7.4
Considering dependency mpm_prefork for php7.4:
Considering conflict mpm_event for mpm_prefork:
Considering conflict mpm_worker for mpm_prefork:
Enabling module mpm_prefork.
Considering conflict php5 for php7.4:
Enabling module php7.4.
To activate the new configuration, you need to run:
systemctl restart apache2
-Callahan-
Encountering the same symptoms but totally unable to understand what you did to solve this.
It would be great if you could elaborate. Thank you!
A person’s success does not depend on his wisdom, but perseverance
Perfect ! Worked like a charm with your article. Thanks a lot 🙂
Thank you so much. This worked like a charm, this tutorial was to the point. got php7.4 easy 😛 (i was in php7.3 btw)
Great, straight-forward tutorial for updating the PHP version! Thank you!
I had an error:
$ sudo a2enmod php7.4
ERROR: Module php7.4 does not exist!
Then I run
$ sudo apt purge libapache2-mod-php7.3 libapache2-mod-php
$ sudo apt install libapache2-mod-php7.4 libapache2-mod-php
This was enough. When running again
$ sudo a2enmod php7.4
…
Module php7.4 already enabled
Thank you so much, it’s working fine! But is there an idea, what’s to do with installed php7.0 programm-files? apt-get remove php7.0*? I’m anxious that there will be trouble.
thanks, up to 7,4 now. great post!.
BTW, should I remove the 7.2 ? how?
When I want to add ondrej/php it recommends to add ondrej/apache2, too
is this necessary?
Thank you
A big THANK YOU from me, too.
This worked really fine for me even with ubuntu 16.04
Thanks a lot!
It worked on my Ubuntu 18.04 server
I did all of this but
sudo a2enmod php7.4
Considering dependency mpm_prefork for php7.4:
Considering conflict mpm_event for mpm_prefork:
Considering conflict mpm_worker for mpm_prefork:
Module mpm_prefork already enabled
Considering conflict php5 for php7.4:
Module php7.4 already enabled
and still I see php -v as 7.2.
Please let me know what I was missing
Thanks
Please restart apache, if you are seeing PHP version 7.2 in the terminal you need to change the version using the below method
sudo update-alternatives --set php /usr/bin/php7.4
When I input:
sudo a2dismod php7.0 (or variations – same as listed in WP admin)
sudo a2enmod php7.4
I get:
“ERROR: Module php7.0.33 does not exist”
(then)
“… Module php7.4 already enabled”
When I input:
sudo service apache2 restart
I get:
“…could not reliably deternube the server’s fully qualified domain… failed!”
The php version in WP admin still reads as php7.0.33-0+deb9u10
What do I need to do?!
Thanks
Really thank you a gem to follow!
Thank you…
Thank you so much for this! Messing with production servers is always very tense.
Docker is the way to go to avoid all tedious procedures.
When I enter the command “sudo nano /etc/nginx/sites-available/your.conf”, he answers me:
“Directory ‘/etc/nginx/sites-available’ does not exist”
Help solve the problem please!
Thanks very much, worked great!