General Installation Considerations ¶
Before starting the installation, first you need to know what do you want to use PHP for. There are three main fields you can use PHP
- Websites and web applications (server-side scripting)
- Command line scripting
- Desktop (GUI) applications
For the first and most common form, you need three things: PHP itself, a web server and a web browser. You probably already have a web browser, and depending on your operating system setup, you may also have a web server (e.g. Apache on Linux and macOS; IIS on Windows). You may also rent webspace at a company. This way, you don’t need to set up anything on your own, only write your PHP scripts, upload it to the server you rent, and see the results in your browser.
In case of setting up the server and PHP on your own, you have two choices for the method of connecting PHP to the server. For many servers PHP has a direct module interface (also called SAPI). These servers include Apache, Microsoft Internet Information Server, Netscape and iPlanet servers. Many other servers have support for ISAPI, the Microsoft module interface (OmniHTTPd for example). If PHP has no module support for your web server, you can always use it as a CGI or FastCGI processor. This means you set up your server to use the CGI executable of PHP to process all PHP file requests on the server.
If you are also interested in using PHP for command line scripting (e.g. write scripts autogenerating some images for you offline, or processing text files depending on some arguments you pass to them), you always need the command line executable. For more information, read the section about writing command line PHP applications. In this case, you need no server and no browser.
With PHP you can also write desktop GUI applications using the PHP-GTK extension. This is a completely different approach than writing web pages, as you do not output any HTML, but manage windows and objects within them. For more information about PHP-GTK.
From now on, this section deals with setting up PHP for web servers on Unix and Windows with server module interfaces and CGI executables. You will also find information on the command line executable in the following sections.
PHP source code and binary distributions for Windows can be found at » https://www.php.net/downloads.php.
Installation on Unix systems
This section will guide you through the general configuration and installation of PHP on Unix systems. Be sure to investigate any sections specific to your platform or web server before you begin the process.
There are several ways to install PHP for the Unix platform, either with a compile and configure process, or through various pre-packaged methods. This documentation is mainly focused around the process of compiling and configuring PHP. Many Unix like systems have some sort of package installation system. This can assist in setting up a standard configuration, but if you need to have a different set of features (such as a secure server, or a different database driver), you may need to build PHP and/or your web server. If you are unfamiliar with building and compiling your own software, it is worth checking to see whether somebody has already built a packaged version of PHP with the features you need.
Prerequisite knowledge and software for compiling:
- Basic Unix skills (being able to operate “make” and a C compiler)
- An ANSI C compiler
- A web server
- Any module specific components (such as GD, PDF libs, etc.)
When building directly from Git sources or after custom modifications you might also need:
- autoconf: 2.13+ (for PHP < 5.4.0), 2.59+ (for PHP >= 5.4.0), 2.64+ (for PHP >= 7.2.0)
- automake: 1.4+
- libtool: 1.4.x+ (except 1.4.2)
- re2c: Version 0.13.4 or newer
- flex: Version 2.5.4 (for PHP <= 5.2)
- bison:
- PHP 5.4: 1.28, 1.35, 1.75, 1.875, 2.0, 2.1, 2.2, 2.3, 2.4, 2.4.1, 2.4.2, 2.4.3, 2.5, 2.5.1, 2.6, 2.6.1, 2.6.2, 2.6.4
- PHP 5.5: 2.4, 2.4.1, 2.4.2, 2.4.3, 2.5, 2.5.1, 2.6, 2.6.1, 2.6.2, 2.6.3, 2.6.4, 2.6.5, 2.7
- PHP 5.6: >= 2.4, < 3.0
- PHP 7.0 – 7.3: 2.4 or later (including Bison 3.x)
- PHP 7.4: > 3.0
The initial PHP setup and configuration process is controlled by the use of the command line options of the configure script. You could get a list of all available options along with short explanations running ./configure –help. Our manual documents the different options separately. You will find the core options in the appendix, while the different extension specific options are described on the reference pages.
When PHP is configured, you are ready to build the module and/or executables. The command make should take care of this. If it fails and you can’t figure out why, see the Problems section.
Apache 2.x on Unix systems
This section contains notes and hints specific to Apache 2.x installs of PHP on Unix systems.
The » Apache Documentation is the most authoritative source of information on the Apache 2.x server. More information about installation options for Apache may be found there.
The most recent version of Apache HTTP Server may be obtained from » Apache download site, and a fitting PHP version from the above mentioned places. This quick guide covers only the basics to get started with Apache 2.x and PHP. For more information read the » Apache Documentation. The version numbers have been omitted here, to ensure the instructions are not incorrect. In the examples below, ‘NN’ should be replaced with the specific version of Apache being used.
Obtain the Apache HTTP server from the location listed above, and unpack it:
tar -xzf httpd-2.x.NN.tar.gz
Likewise, obtain and unpack the PHP source:
tar -xzf php-NN.tar.gz
Build and install Apache. Consult the Apache install documentation for more details on building Apache.cd httpd-2_x_NN
./configure --enable-so
make
make install
Now you have Apache 2.x.NN available under /usr/local/apache2, configured with loadable module support and the standard MPM prefork. To test the installation use your normal procedure for starting the Apache server, e.g.:
/usr/local/apache2/bin/apachectl start
and stop the server to go on with the configuration for PHP:/usr/local/apache2/bin/apachectl stop
Now, configure and build PHP. This is where you customize PHP with various options, like which extensions will be enabled. Run ./configure –help for a list of available options. In our example we’ll do a simple configure with Apache 2 and MySQL support.If you built Apache from source, as described above, the below example will match your path for apxs, but if you installed Apache some other way, you’ll need to adjust the path to apxs accordingly. Note that some distros may rename apxs to apxs2.cd ../php-NN
./configure --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql
make
make install
If you decide to change your configure options after installation, you’ll need to re-run the configure, make, and make install steps. You only need to restart apache for the new module to take effect. A recompile of Apache is not needed.Note that unless told otherwise, ‘make install’ will also install PEAR, various PHP tools such as phpize, install the PHP CLI, and more.
Setup your php.inicp php.ini-development /usr/local/lib/php.ini
You may edit your .ini file to set PHP options. If you prefer having php.ini in another location, use –with-config-file-path=/some/path in step 5.If you instead choose php.ini-production, be certain to read the list of changes within, as they affect how PHP behaves.
Edit your httpd.conf to load the PHP module. The path on the right hand side of the LoadModule statement must point to the path of the PHP module on your system. The make install from above may have already added this for you, but be sure to check.For PHP 7:LoadModule php7_module modules/libphp7.
soFor PHP 5:LoadModule php5_module modules/libphp5.so
Tell Apache to parse certain extensions as PHP. For example, let’s have Apache parse .php files as PHP. Instead of only using the Apache AddType directive, we want to avoid potentially dangerous uploads and created files such as exploit.php.jpg from being executed as PHP. Using this example, you could have any extension(s) parse as PHP by simply adding them. We’ll add .php to demonstrate.
SetHandler application/x-httpd-php
Or, if we wanted to allow .php, .php2, .php3, .php4, .php5, .php6, and .phtml files to be executed as PHP, but nothing else, we’d use this
SetHandler application/x-httpd-php
And to allow .phps files to be handled by the php source filter, and displayed as syntax-highlighted source code, use this:
RewriteEngine On
RewriteRule (.*\.php)s$ $1 [H=application/x-httpd-php-source]
The php source filter should not be enabled on production systems, where it may expose confidential or otherwise sensitive information embedded in source code.
Use your normal procedure for starting the Apache server, e.g.:/usr/local/apache2/bin/apachectl start
ORservice httpd restart
Following the steps above you will have a running Apache2 web server with support for PHP as a SAPI module. Of course there are many more configuration options available Apache and PHP. For more information type ./configure --help
in the corresponding source tree.
Apache may be built multithreaded by selecting the worker MPM, rather than the standard prefork MPM, when Apache is built. This is done by adding the following option to the argument passed to ./configure, in step 3 above:
--with-mpm=worker
This should not be undertaken without being aware of the consequences of this decision, and having at least a fair understanding of the implications. The Apache documentation regarding » MPM-Modules discusses MPMs in a great deal more detail.