There was a problem loading the comments.

How to install PHP on Ubuntu 20.04 or 22.04 or 23

Support Portal  »  Knowledgebase  »  Viewing Article

  Print

How to Install PHP on Ubuntu Server 20.04 or 22.04 or 23

PHP is a script-based server-side programming language, which can help to automate various server tasks. It handles tasks such as dynamic content, database requests, and data preprocessing/display.
PHP is used by many CMS such as Wordpress, Prestashop and others.

In order to install PHP you will need the following:

  • A Linux system Linux VPS running Ubuntu 20.04 or 22.04;
  • Access to an Ubuntu user account with sudo privileges;
  • Access to a command line/terminal window (Ctrl+Alt+T); and
  • A running web server (Apache or Nginx).

Note: PHP has several different release versions and is updated on a regular basis. Currently, PHP 7.4 is EOL and 8.1 are the recommended and supported by most used versions of the software available.

 

Installing PHP on Ubuntu ( 20.04 or 22.04 or 23 )

Installing PHP on Ubuntu can be accomplished using the apt package manage, you will need to follow these steps:

  1. Update the package list:
aad84a8e1e6074d52ceef48bab5c3ef5e42b3f003f1eee229a9eb8864e42831df11d2eacd3776b9d?t=5e98a28119238b563315ce4c9c5f31bb 
  1. Install the PHP package along with any required dependencies:
92875376cf49a5322b04c6d8a3b1f8bc414bb844ae2f1349a5c4142ce3010b09b08cddf78b67c7e2?t=1cf303faf1a3da991bc49ebd9c2c5c94
Note: On Ubuntu 20.04, this command will install PHP 7.4, while on Ubuntu 22.04 it will install PHP version 8.1.
  1. To check if PHP is installed correctly, run the following command:
35d8ad8782072654a4610ba43c1f7cfad807dd85a335364544c8dfa172added2dff5a8a7a25e2985?t=faaacaea48a72196eb5a144556ba1ee9
 

This will display the version of PHP installed on your system.

  1. If you need to install a specific PHP version, you can do so by specifying the version number after the package name. For example, to install PHP 7.4, you can use the following command:
bd68b65c7c84173dbe4eec2266f0b0ac886638893fe90be0b130fcc47a7f63a1658663b21c7fe3e0?t=a001d555a2d716de2b976b8fa489191c
  1. Once you have installed PHP, you can also install additional PHP extensions if needed. You can search for available extensions using the apt-cache command. For example, to search for the mysqli extension, you can use the following command:
c7035eb84060428f7af1e99d8b9c830ad2e0fd12dfba50e3b92012bcef80e6ac4b5024d13febd08b?t=05a726b20e22558502416cf335f0967a
  1. To install the mysqli extension, run the following command:
fbf48dab2aea56673b315c530ad4f7f9ea35997cd576c36afd5e36caf1cff5e27863b1e39edb4bc6?t=b33c391e7bcacc8986bef7e3f70a175b

That's it! PHP is now installed and ready to use on your Ubuntu 20.04 or 22.04 system.

 

Remember that PHP works in tandem with an existing server, so you will need to install it specifically to your server software. Alternatively, you can always install a stable version from the default Ubuntu software repositories.

If you are running an Apache or Nginx web server and need to install PHP, please follow the applicable steps below.

 

Installing PHP with Apache on Ubuntu

  1. Install Apache web server:
98913ee07b1fe7b823cc2e2096a82151df001f285fb3d8363e0f7da847a6ed40da2da73759355a0a?t=b6403d0b1037058b20e2f4880ed7aa18
  1. Install PHP and required modules:
656e61f54a54354a2ecbc0fc6756fca3932fff69b331b2f8118c3e8f43a55df73a93821ea08460bb?t=c7f42d854585620a37d973924cbd41b3
  1. Restart Apache to enable the PHP module:
7521b6b974f566f00d808554f0a6a69653a668ca58dbd83c165ed9c94c486875da6701744899642d?t=62f99cdf7f8b6b5915aba6fb798c5e9c
  1. Create a PHP test file to verify that PHP is working properly. For example, create a file named info.php in the /var/www/html directory:
916df4034f104abf4962ae65517460069d6984de7f8bd921e331fcdd9ef540ee1eab11479526fd18?t=b36e7f7ab48db44335ab6e7ae3fc92e1
  1. Add the following PHP code to the file and save it:
5831622ec43e216421b7dacd59c14a872b07be955972c0174994397d7dc62b51934447c6cdc6c78f?t=618e1158218e0751b2e39419c80b89da
  1. Now, open a web browser and navigate to http://your_server_ip/info.php. You should see a page with information about the PHP version and configuration.


That's it! You can now start developing and deploying PHP applications on your server.


Installing PHP with Nginx on Ubuntu

  1. Install Nginx web server:
f6cb4ef3643e2c123c8860cc5d63795afec48171da3603441b8510a102fbdc62315eaef8e692bfc4?t=7aebb5938ee0fe0b524d6c93c1dffd12
  1. Install PHP and required modules:
3f34191b45245db8f572bd0f173d83a9e1cfb6528b5f888cb9d712eca55c1972f520ebc7d3a5b5b0?t=2911f7b96741d902275db3c027e51b9b
  1. Configure Nginx to use PHP-FPM:
00f213c3b9cd3bc06f2acf7f4d42bde5e00e9b79eed829636499eb21280a8724d41859586e6d51c4?t=903747c1af892b20bf606662cd8c85c1
  1. Find the following block of code:
296c40c9412b4f63250b387026b6b3597635a4ac9b600bc49cecb5d1c90c697c3c9ddf275e28bb1c?t=53681f519e1579de787b6f4332915342
  1. Uncomment the block and modify the fastcgi_pass line to point to the PHP-FPM socket. In this example, we are using PHP 7.4:
b3a9a6113c7ac1572f4eaaf1e6da8b3639fd6f3f28f765e99c682fb7b9f3c2433e2932f7c296f55b?t=41d8538995485b0fbf6e7aa28653cb4d
  1. Save and close the file.

  2. Restart Nginx and PHP-FPM:

c054db6fe3e079a5377b9f3a631aae68ebf9f08d84ad723b05cd6df63c676585b80f07435290f6a6?t=6e4f24b0f1e2a55595c37d0740585ebb
  1. Create a PHP test file to verify that PHP is working properly. For example, create a file named info.php in the /var/www/html directory:
2c105a2eb05ef942d6d02a36755d502b226b05906d8aaded73cc4cd8bf7caee0dc5faa961ec00f51?t=c3b856583c2514123ab50ecec1a7fd73
  1. Add the following PHP code to the file and save it:
393e3c34f4558e4a4b581b46392f4e0c0268b96eee52101881704e068a61e711f64b1de3dfbe5f14?t=d574da69a7b88619b4b744b647aa3026 
  1. Now, open a web browser and navigate to http://your_server_ip/info.php. You should see a page with information about the PHP version and configuration.

That's it! You can now start developing and deploying PHP applications on your server.

 

Installing additional PHP modules on Ubuntu

To install additional PHP modules on Ubuntu 20.04 or 22.04, you can use the apt package manager and perform the following steps:

  1. Search for available PHP modules using the apt-cache command. For example, to search for the imagick module, you can use the following command:
d3346601badcea1ed8e6af8bab3705292d455f45e5ce0b046d731570a8d76323acfc473c52f04ef0?t=fc57804e261aa457a27697bd7d2b91b6
  1. Install the desired PHP module(s) using the apt command. For example, to install the imagick module, you can use the following command:
126de97991da17cc075e5cf834f2cb278917caa58b0724ad17288823732e3a42aee57a7c7a28ec3a?t=aee9494ccea0c15664ad51dbc928c003
  1. Restart the web server to enable the new module(s):
63ee69bfb7a729c5f73c6d1ce6f2b70ca11c77b8376f02da0b03e7bf490f9a05036639a67d1f5ad6?t=b14cc9089025a45f397cc9c01e241820

You have now successfully installed additional PHP modules on your Ubuntu system, and can repeat these steps to install any other PHP module(s) that you may need.

 

How to Uninstall PHP on Ubuntu

To uninstall PHP on Ubuntu 20.04 or 22.04, you can use the following command:

45e2d0524dd4a64b631cba466216e7927cd053d9f796a2c5ffc87464b106d24d0de113e58b7e3783?t=7af048612ee37cea3858923482d43d54

This will remove the PHP packages and any dependencies that are no longer needed. If you want to completely remove all configuration files associated with PHP, you can use the following command:

7c2a0193ea7fa245a7bc8dacd87f1d20414d9c599390675f86c65271a1b89d849444110bd35adf6b?t=327501e605c808d83e38a925bd6fca4b

This will remove the PHP packages and all configuration files associated with them. Note that this will also remove any PHP modules and extensions that you may have installed. If you only want to remove specific PHP modules, you can use the apt remove command with the name of the module you want to remove, for example:

f87ba20da41116204b8ee83537ea9e27039c5a1c5983a527f566f2adec9e406f9e64f85b04c021a9?t=55bf474c5d30ac063a8d091385d3210d

This will remove the php-imagick module but leave the rest of the PHP installation intact.



Share via
Did you find this article useful?  

Related Articles

Tags

© Rackzar