How to Install PHP on Any Server

by Naishad Vaghani Apr 06, 2024 Total Views: 135

What is PHP?

PHP is an open source scripting language primarily used for web development and server-side (backend) scripting. In simple terms:
  • PHP can be installed on a server to run scripts (e.g. pieces of code to add forms to your site).
  • It is run server-side rather than client-side, so it works in the backend rather than in the browser.
  • It integrates well with HTML, making it highly suitable for web development.

It’s also one of the easiest web scripting languages to learn, making it a popular choice for beginners. But it’s also quite powerful and suitable for advanced website functions.

Many websites and tools use PHP and its many extensions (.NET, Apache, and MySQL may sound familiar). WordPress is built primarily on PHP, and most of its plugins and themes also run on it.


How to Install PHP on Linux

Before starting, you should be familiar with the Terminal and how to operate Unix-like OSes in general. Generally, these command line codes should work on any Linux distribution that uses the normal syntax in the Terminal, but we’ve left some notes below for specific OSes.

First, you should make sure your packages are up to date, so run this Linux command in the Terminal.
sudo apt-get update && sudo apt-get upgrade

Now you’re ready to install PHP. The command to do so is effortless and straightforward.

sudo apt-get install php

This will install the latest version of PHP along with several extensions. You can use this code to see which version you have.

php –v

What if you want to install a specific version of PHP, such as PHP 7.4? You’ll need to use a PPA, or Personal Package Archive, by Ondřej Surý. This is a safe way to install older supported versions of PHP. Run these three commands one at a time:

sudo apt install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
Now your system recognizes the archive, and you can install PHP 7.4. Type in the following command:
sudo apt install php7.4

And of course, you can replace this number with whatever PHP version you want.

That covers the basics, but if you need extra help with configuration, the PHP installation documentation for Unix can help.


Installing PHP on Ubuntu

For Ubuntu users, there aren’t many specific concerns as long as you follow the guide above. Ubuntu is one of the most popular distributions, so most Linux guides are practically made for that system.

One alternative option available to you is downloading a LAMP stack. LAMP stands for Linux, Apache, MySQL, and PHP. It’s essentially a bundle of all the software you need to get a server up and running.

While you can manually download each of these tools separately, you could get Taskel, a bundle that will install all these at once. Just run these two commands in succession in the Terminal:
sudo apt install tasksel
sudo tasksel install lamp-server


Installing PHP on CentOS 7

This operating system is a little different from other Linux distributions. On CentOS, the apt and apt-get commands are not the ideal ways to install the software. Instead, it uses yum, Yellowdog Updater Modified, a better package manager for RHEL-based operating systems.

Otherwise, the commands you’ll need to use are pretty similar. To update your packages, run this command instead:
sudo yum install epel-release && sudo yum update
As for other commands, you can usually replace apt-get with yum. To install PHP, use this command.
sudo yum install php
PHP commands such as php –v should work the same on CentOS 7, so you don’t need to worry about that.


Installing PHP on Debian

Last for Unix-like operating systems is Debian. This time there are just a few minor concerns, but most of the commands listed above should work fine.

For Debian, you can use either apt-get or aptitude in your Terminal commands. The aptitude command is a little more comprehensive and provides a menu interface. It’s up to personal preference which you use, and either will get the job done.

So instead of using the usual apt-get command, you could run this one instead:
sudo aptitude install php

Debian can be finicky. If you find PHP isn’t parsing or extensions aren’t working, make sure you’ve updated your server’s web configuration file and that it’s loading the extension ini files.

Besides a few small things, instructions for Debian are practically identical to those for other Linux distributions.


How to Install PHP on Windows

If you’re using a Windows system, unlike macOS and Linux, you don’t need to install PHP through the command line (though it is an option if you’d prefer).

An easy way to install PHP from here is to enable IIS and then use WebPI to install PHP. After launching WebPI, you can find it under the Products tab. Click Add on the version you want, then click Install.


Enabling IIS on WindowsEnabling IIS on Windows

You can also download PHP for Windows and manually configure it to work with IIS. Make sure to get a non-thread-safe version if you’re using IIS.

If all this is too complicated, you could instead install WampServer or XAMPP, as these come with everything you need to start working with a web server: Apache, a database, and of course PHP.

These instructions will work with most modern Windows OSes such as Windows 10, 7, and Vista. If you’re using an older version of Windows, you should check out the legacy Windows installation documentation.

Windows may need a little extra configuration to get PHP working properly, so make sure to check the recommended Windows configuration documentation. You just have to make a few small ini tweaks.


How to Install PHP on macOS

PHP comes preinstalled on most macOS systems, so you usually don’t need any manual installation.

All you need to do is uncomment a few lines of code in the Apache configuration file httpd.conf, which you can usually find at /private/etc/apache2/httpd.conf. Uncomment these two lines by removing the hashtag symbol:

# LoadModule php5_module libexec/httpd/libphp5.so
# AddModule mod_php5.c

You may need to do extra configuration if you don’t like some file settings’ default values.

Otherwise, find the DocumentRoot, then create and load a PHP file with this code:
<?php phpinfo(); ?>
You can always check the PHP version with the php –v command to make sure PHP was installed correctly.
If you do need to download PHP manually, then you should install Homebrew and use this simple command:
brew install php


All About PHP Extensions

Once you’ve got PHP up and running, it’s a good idea to think about extensions. These compiled libraries add all sorts of extra, helpful functionality. Think of them as plugins that add on to what PHP already offers. Some of these are required to run PHP frameworks, such as Laravel and Symfony.

While it’s completely possible to code everything yourself without ever touching an extension, there’s no reason to do it all on your own when widely-used shortcuts exist to make your life easier.

PHP already comes with dozens of built-in extensions, some of which you must enable manually, and others you can start using right away. You can also install PHP extensions through sites like PECL, which hosts hundreds of third-party packages.

The PHP manual offers extension documentation for many of these as well, so you can get the hang of using them.

Not sure where to start? Here are just a handful of a few popular PHP extensions:

Apache: Apache is a widely used web server software recommended by WordPress due to its great compatibility with it and PHP. Apache comes bundled with PHP, so you don’t need to install it manually.
.NET: .NET is a popular software framework that you can use to build web applications and more. It works with multiple languages, including PHP. You may also be interested in PeachPie, which allows you to run PHP code in .NET.
MySQLi: A better version of the old MySQL extension, MySQLi enables you to work with MySQL database software. This one is a must-have to help you set up your database.
Whenever you install an extension, you need to uncomment it. Just open up php.ini and remove the semicolon (;) in front of extension=extensionname.

You should also check out these PHP frameworks if you’d like to standardize your code and build with premade libraries. They’re a great way to get started with developing web applications with PHP.

Tags: php mysql apache .net

Please login to create comment.

Author
Author Image

for detail. With over five years of experience in the tech industry, Damy has honed his skills in developing cutting-edge software solutions for various clients across different sectors. His expertise lies in full-stack web development, where he excels in languages such as JavaScript, Python, and Java.

- Naishad Vaghani