How to Install PHP on Any Server (Linux, MacOS or Windows)

Setting up a server can be a daunting task, especially when installing languages ​​like PHP. If you want to get a WordPress test server up and running, it’s a necessary skill to learn.

No idea where to start to get PHP on your server? Depending on your unique setup, there are many ways to do this. Where do you download PHP, what version, what does it need to make it work, and how do you install PHP on various operating systems?

We’ll answer them all in this comprehensive PHP installation guide.

Let’s get started!

What is PHP?

PHP is an open source scripting language primarily used for web development and server-side (backend) scripting. In plain language:

PHP can be installed on a server to run scripts (for example, code snippets to add forms to your site).
It runs server-side rather than client-side, so it works on the backend rather than in the browser.
It integrates well with HTML, making it very suitable for web development.
It’s also one of the easiest web scripting languages ​​to learn, so which is a popular choice for beginners. But it is also quite powerful and suitable for advanced website features.

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

Does your server need PHP?
So, you know what PHP is, but why should you install it on your server?

The most notable reason is that it is a requirement for WordPress to be up and running, as it is built almost exclusively on PHP. PHP also powers most WordPress functions, hooks, themes, and plugins.

You can see all other WordPress system requirements here:

So if you want to run a WordPress staging server to play around or set up a staging site, you’ll need to install PHP.

On top of that, PHP is a great starting point for beginning developers. It’s easy to use compared to other web scripting languages, but that doesn’t come with any expense in functionality. You can do all sorts of things with it if you’re skilled enough.

PHP is also quite popular. Therefore, you can find many extensions and resources online.

On top of that, if you want to learn how to code WordPress themes and plugins, you’ll need to learn PHP. Setting up a test server is a great way to experiment.

Note that many web hosts (such as Phluit) come pre-installed with PHP and WordPress. Phluit even includes the ability to create a WordPress test site, so that’s easy to access as well.

The only reason you’ll need to do this manually is if you’re setting up a server from scratch, either on your local computer or in an unmanaged hosting environment.

With that said, let’s jump into the server setup instructions.

PHP Prerequisites
Before you install PHP, you’ll need to make sure your server can handle it. Fortunately, the requirements are fairly basic, and much of the software is likely already installed on your computer.

If you’ve never created a server before, you should refer to this guide to setting up a local server on various operating systems. The exact details vary depending on your operating system, but you should know how to work with the command line and be ready to install new software.

Regardless of the operating system you are installing PHP on, you will need a web server to run it. You may also need to install a database like MySQL, so keep that in mind.

These are the PHP prerequisites for each operating system, starting with Linux:

-An ANSI C compiler.
-Module-specific components, such as GD graph libraries or PDF libraries.
-Optional: Autoconf 2.59+ (for PHP versions <7.0), Autoconf 2.64+ (for PHP versions > 7.2), Automake 1.4+, Libtool 1.4+, re2c 0.13.4+, and Bison.

You should also be familiar with navigating Unix-like operating systems.

Now the PHP requirements for Windows:

  • Multiple Windows operating systems are supported in PHP 5.5+, but users of 7.2.0+ cannot use Windows 2008 or Windows Vista.
  • Visual C Runtime (CRT) .
  • Visual Studio 2012, 2015, 2017, or Microsoft Visual C++ Redistributable for Visual Studio 2019, depending on your version of PHP.

Finally, there are no prerequisites for macOS because PHP is included with the system. We’ll explain how to enable it below.

Where to download PHP
If you need to download the PHP files manually, you should get them from the PHP downloads page from the official site.

Avoid installing from third-party sites unless you know they are safe, as downloading files from third-party sites can lead to accidental installation of malware.

If you are downloading for a Windows machine, look for the “Windows Downloads” link under each version of PHP and make sure you are installing the correct files.

You can also install older versions of PHP, but this is not recommended unless you know what you’re doing, as they expose your server to bugs and major security flaws.

If you’re running a Linux distribution, you generally don’t need to get the files through the website and must use the command line. We’ll go over it in detail below.

Which version of PHP should I use?
If you’ve clicked the download link above, you’re probably looking at all those files and feeling overwhelmed. Which version of PHP is correct?

Generally speaking, if you’re starting a new project where compatibility issues aren’t an issue, you should get the latest stable version of PHP.

It is sometimes possible to download beta versions of PHP, which are even newer, but often have bugs and are only released to help developers gather feedback. Most of the time, you should stick with the stable versions.

What about previous versions? Each version of PHP comes with new features, but that means new incompatibilities with older features. If you need to do something specific that isn’t supported by the latest version of PHP, you can use an earlier version, but you should stick to currently supported versions only.

If you are not sure, use the latest version of PHP.

Please note that WordPress only supports certain versions of PHP. Technically it supports PHP versions from 5.6.20+, but these are quite old and therefore not recommended. Versions 7.2 to 7.4 are currently the best supported versions for WordPress.

PHP version 8 is also supported, but this is currently very new and not all plugins work correctly. If you are concerned about compatibility issues, stick with PHP version 7.4. You can read our PHP benchmarks post to see how various CMS and PHP frameworks perform on different versions of PHP.

How to install PHP on Linux
Before you begin, you should be familiar with the Terminal and how to operate Unix-like operating systems in general. Generally, these command line codes should work on any Linux distribution that uses normal Terminal syntax, but we leave some notes below for specific operating systems.

First, you need to make sure your packages are up to date, so run this command in Terminal.

sudo apt-get update && sudo apt-get upgrade

You are now ready to install PHP. The command to do so is simple and effortless.

sudo apt-get install php

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

php –v

What if you want to install a specific version of PHP, such as PHP 7.4? You will need to use a PPA, or personal package archive, from Ondřej Surý. This is a safe way to install older compatible PHP versions. 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 file and you can install PHP 7.4. Type the following command:

sudo apt install php7.4

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

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

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

An alternative option available to you is to download a LAMP stack. LAMP stands for Linux, Apache, MySQL and PHP. Basically, it’s a package of all the software you need to get a server up and running.

While you can manually download each of these tools separately, you can get Taskel, a package that will install them all at once. Just run these two commands in succession in Terminal:

sudo apt install tasksel

sudo tasksel install lamp-server

Installing PHP on CentOS 7
This operating system is a bit different from other Linux distributions. On CentOS, the apt and apt-get commands are not the ideal ways to install software. Instead, use 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 like php –v should work the same on CentOS 7, so you don’t need to worry about that.

Need incredibly fast, secure, and easy-to-use hosting for developers? Phluit is built with developers in mind. Check out our plans!

Installing PHP on Debian
The ultimate for Unix-like operating systems is Debian. This time there are only a few minor concerns, but most of the commands listed above should work fine.

For Debian, you can use apt-get or aptitude in your Terminal commands. The aptitude command is a bit more complete and provides a menu interface. It depends on the personal preferences you use, and either will do the job.

So instead of using the usual apt-get command, you can run this instead:

sudo aptitude install php

Debian can be finicky. If you find that PHP is not parsing or the extensions are not working, make sure you have updated your server’s web config file and are loading the extension ini files.

Apart from a few small things, the instructions for Debian are virtually 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 via the command line (although it’s an option if you 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.

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

If all this is too complicated, you can install WampServer or XAMPP as they come with everything you need to get started with a web server: Apache, a database and of course PHP.

These instructions will work with most modern Windows operating systems, such as Windows 10, 7, and Vista. If you are using an earlier version of Windows, you should refer to the legacy Windows installation documentation.

Windows may require a little additional configuration for PHP to work properly, so be sure to refer to the recommended Windows configuration documentation. You just have to make a few small ini settings.

How to install PHP on macOS

PHP comes pre-installed on most macOS systems, so you don’t normally 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 in /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 additional configuration if you don’t like the default values ​​of some file settings. Otherwise, find 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 need to download PHP manually, you should install Homebrew and use this simple command:

brew install php

All About PHP Extensions
Once you have PHP up and running, it’s a good idea to think about extensions. These compiled libraries add all sorts of useful and extra features. Think of them as plugins that add to what PHP already offers. Some of these are required to run PHP frameworks, such as Laravel and Symfony.

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

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

The PHP manual also offers extension documentation for many of these, so you can learn how to use them.

Summary
Regardless of the operating system you are using, installing PHP is not such a difficult process. If you want to create a WordPress test server, you will need it. Aspiring PHP programmers will find setting up a web server and installing PHP on it a great exercise.

Make sure to download PHP via the command line or from its official site. You can get the latest stable version of PHP or any other version compatible with WordPress.

If you enjoyed this article, then you are going to love the Phluit WordPress hosting platform. Speed ​​up your website and get 24/7 support from our veteran WordPress team. Our Google Cloud-powered infrastructure focuses on autoscaling, performance, and security. Let us show you the Phluit difference! See our plans

Table of Contents

Stay in the Loop!

Subscribe to Phluit’s Newsletter and stay updated with the latest.

Subscription Form

Ready to buy or sell your
online business?

Explore listings or list your business today on Phluit. Start your journey now to achieve your business goals with our trusted platform.

Share to...