PHP is a popular programming language used for many purposes, especially widely in web development. In this guide, we show you how to install PHP on your server.
First, connect to your server via SSH. If you don’t know how, check out our First Access (SSH) guide.
Once logged in, we recommend running the update command for your operating system to keep your server secure and up to date.
sudo apt update
sudo yum update
sudo zypper update
sudo dnf update
There are two main ways to install PHP on your Linux server: as a standalone version or as an additional package for Apache. If you use your own web server other than Apache, or need PHP for general purposes, we recommend the standalone method. For use with the Apache web server, you should install PHP as an Apache package.
Use the following command to install the latest PHP version. If you want to install a specific version, you can do so with the -y flag and the version number.
# Latest version
sudo apt install php
# Specific version (e.g., php7.4)
sudo apt -y install php[version]
Verify the successful installation with the command php -v. You have successfully installed PHP on your server. We recommend reading the PHP Extensions section to ensure you install all necessary extensions.
If Apache is not yet installed, you can install it with the following command:
sudo apt install apache2
After installation, make sure the appropriate firewall rules are set so your web server is accessible from the internet. In this example, we use the UFW Firewall since Apache has a registered application profile. If you use a different firewall, ensure port 80 (HTTP) is open.
sudo ufw allow in "Apache"
Your server should now be reachable. Test this by entering http://[your_server_ip] in a browser.
With Apache ready, you can now install the PHP package for Apache:
sudo apt install php libapache2-mod-php
Check the installation with php -v. You have successfully installed PHP alongside your Apache web server. Be sure to check the PHP Extensions section to install all required extensions.
PHP comes with many optional extensions you can install to expand functionality. To see a list of available extensions, use the following command, which conveniently displays search results with less:
apt search php- | less
Use the arrow keys to scroll and press Q to exit. To install an extension, simply use the apt install command. You can specify multiple extensions separated by spaces to save time.
sudo apt install [php_extension] [...]