Skip to content

Setting Up PHP on a Linux Server – Power for Dynamic Web Apps

Learn how to install PHP on your Linux server for web development and optimize your setup for Apache or standalone use → Discover more now

12 November 20262 min read15 Views0 helpful

Introduction

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.

Preparation

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.

Code
  sudo apt update
Code
  sudo yum update
Code
  sudo zypper update
Code
  sudo dnf update

Installation

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.

Standalone

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.

Code
# 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.

With Apache

If Apache is not yet installed, you can install it with the following command:

Code
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.

Code
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:

Code
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 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:

Code
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.

Code
sudo apt install [php_extension] [...]

Conclusion

You have successfully installed PHP on your Linux server. If you have any questions or need help, our support team is available daily! 🙂

FAQ

Frequently asked questions

Connect to your server via SSH, update your system, then install the latest PHP version using sudo apt install php. For a specific version, use sudo apt -y install php[version].
For use with the Apache web server, install PHP as an Apache package instead of the standalone method.
Use the command php -v to verify that the installation was successful.

Feedback

Was this article helpful?

Your feedback helps us prioritize and improve content.

0 of 0 Users found this article helpful.

Personal support

Need more help than the guide provides?

Our ZAP support team knows your product — briefly describe your issue and we'll help you personally.

Zappy knows this guide and answers you right in the chat.