Nginx 1.4.x on Unix systems
This documentation will cover installing and configuring PHP with
PHP-FPM for a Nginx 1.4.x HTTP server.
This guide will assume that you have built Nginx from source and therefore
all binaries and configuration files are located at
/usr/local/nginx. If this is not the case and you have
obtained Nginx through other means then please refer to the
» Nginx Wiki in order to translate
this manual to your setup.
This guide will cover the basics of configuring an Nginx server to
process PHP applications and serve them on port 80, it is recommended
that you study the Nginx and PHP-FPM documentation if you wish to
optimise your setup past the scope of this documentation.
Please note that throughout this documentation version numbers have been
replaced with an 'x' to ensure this documentation stays correct in the future,
please replace these as necessary with the corresponding version numbers.
-
It is recomended that you visit the Nginx Wiki
» install page
in order to obtain and install Nginx on your system.
-
Obtain and unpack the PHP source:
-
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 PHP-FPM and MySQL support.
cd ../php-x.x.x
./configure --enable-fpm --with-mysql
make
sudo make install
-
Obtain and move configuration files to their correct locations
-
It is important that we prevent Nginx from passing requests to the
PHP-FPM backend if the file does not exists, allowing us to prevent
arbitrarily script injection.
We can fix this by setting the
cgi.fix_pathinfo
directive to 0 within our php.ini file.
Load up php.ini:
Locate cgi.fix_pathinfo= and modify it as follows:
-
php-fpm.conf must be modified to specify that php-fpm must run as the user
www-data and the group www-data before we can start the service:
vim /usr/local/etc/php-fpm.conf
Find and modify the following:
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
user = www-data
group = www-data
The php-fpm service can now be started:
This guide will not configure php-fpm any further, if you are interested
in further configuring php-fpm then please consult the documentation.
-
Nginx must now be configured to support the processing of PHP applications:
Modify the default location block to be aware it must attempt
to serve .php files:
The next step is to ensure that .php files are passed to the
PHP-FPM backend. Below the commented default PHP location block,
enter the following:
Restart Nginx.
sudo /usr/local/nginx/sbin/nginx -s stop
sudo /usr/local/nginx/sbin/nginx
-
Create a test file
rm /usr/local/nginx/html/index.html
echo "<?php phpinfo(); ?>" >> /usr/local/nginx/html/index.php
Now navigate to http://localhost. The phpinfo() should now be shown.
Following the steps above you will have a running Nginx web server with
support for PHP as a SAPI module. Of course there are
many more configuration options available for Nginx and PHP. For more
information type ./configure --help in the corresponding
source tree.
User Contributed Notes
junlong666 at gmail dot com4 months ago
When I tried to install PHP-7.0.1, i got the following warning at step 3:
configure: WARNING: unrecognized options: --with-mysql
and other installation guide may have similar issues —— doing a configure with MySQL support (ext/mysql) in PHP 7. The extension was deprecated in the recent version
https://wiki.php.net/rfc/remove_deprecated_functionality_in_php7
then you could use ext/mysqli or ext/pdo_mysql instead, for example, configure PHP 7.x with:
./configure --enable-fpm --with-mysqli
Lenny2 months ago
Building from source is not easy if something is a bit different, and I had a hard time with some directory and configuration options. I was floundering around the web until I found this site that translated from Chinese. No one else had the solution. I couldn't get php fpm to start until I changed the directory (Item 2.ERROR: Unable to globalize). I had other issues listed but I was able to solve them. Please don't delete this, it is very useful info.
The original site (it is in Chinese, not my site, but I want to give credit):
(there is some more there, you can goto the site)
blog.dream1987.top/?paged=2
Installation problems:
1. configure: error:. Xml2-config not found Please check your libxml2 installation.
solution:
apt-get install libxml2-dev
2.Warning: Declaration of PEAR_Installer :: download () should be compatible with & PEAR_Downloader :: download ($ params) in phar: ///root/php-7.0.0alpha1/pear/install-pear-nozlib.phar/PEAR /Installer.php on line 43
Warning: Declaration of PEAR_PackageFile_Parser_v2 :: parse () should be compatible with PEAR_XMLParser :: parse ($ data) in phar: ///root/php-7.0.0alpha1/pear/install-pear-nozlib.phar/PEAR/PackageFile/ Parser / v2.php on line 113
[PEAR] Archive_Tar - already installed: 1.3.13
[PEAR] Console_Getopt - already installed: 1.3.1
[PEAR] Structures_Graph- already installed: 1.0.4
Warning: Declaration of PEAR_Task_Replace :: init () should be compatible with PEAR_Task_Common :: init ($ xml, $ fileAttributes, $ lastVersion) in phar: ///root/php-7.0.0alpha1/pear/install-pear-nozlib. phar / PEAR / Task / Replace.php on line 31
[PEAR] XML_Util - already installed: 1.2.3
Warning: Declaration of PEAR_Task_Windowseol :: init () should be compatible with PEAR_Task_Common :: init ($ xml, $ fileAttributes, $ lastVersion) in phar: ///root/php-7.0.0alpha1/pear/install-pear-nozlib. phar / PEAR / Task / Windowseol.php on line 76
Warning: Declaration of PEAR_Task_Unixeol :: init () should be compatible with PEAR_Task_Common :: init ($ xml, $ fileAttributes, $ lastVersion) in phar: ///root/php-7.0.0alpha1/pear/install-pear-nozlib. phar / PEAR / Task / Unixeol.php on line 76
[PEAR] PEAR - already installed: 1.9.5
solution:
Workaround not found (http://pear.php.net/bugs/bug.php?id=20554)
3. Start php-fpm
1.ERROR: failed to open configuration file '/usr/local/etc/php-fpm.conf': No such file or directory (2)
ERROR: failed to load configuration file '/usr/local/etc/php-fpm.conf'
ERROR: FPM initialization failed
solution:
Php-fpm.conf copy files from the source file to that location.
cp /root/php-7.0.0alpha1/sapi/fpm/php-fpm.conf /usr/local/etc/php-fpm.conf
2.ERROR: Unable to globalize '/usr/local/NONE/etc/php-fpm.d/*.conf' (ret = 2) from /usr/local/etc/php-fpm.conf at line 125.
ERROR: failed to load configuration file '/usr/local/etc/php-fpm.conf'
ERROR: FPM initialization failed
solution:
Edit /usr/local/etc/php-fpm.conf document introduced * .conf part, change to the correct path include = / usr / local / etc / php-fpm.d / *. Conf
If there is no /usr/local/etc/php-fpm.d directory, create the directory.
3.WARNING: Nothing matches the include pattern '/usr/local/etc/php-fpm.d/*.conf' from /usr/local/etc/php-fpm.conf at line 125.
ERROR:. No pool defined at least one pool section must be specified in config file
ERROR: failed to post process the configuration
ERROR: FPM initialization failed
solution:
cp www.conf.default www.conf
4.ERROR: [pool www] can not get gid for group 'nobody'
ERROR: FPM initialization failed
solution:
Www.conf open files, user and group users into nginx default settings, usually the default is www-data.
Akash Kumar Sharma4 months ago
If you get "File not found" error then add "root ROOT_DIR_LOCATION" directive to PHP location block i.e. "location ~* \.php$ { }" , where ROOT_DIR_LOCATION is root directory like "/usr/share/nginx/html" .
nguyentienlong88 at gmail dot com5 months ago
at step 3, if there is no configure script yet, you need to rebuiding configure script using this command:
./buildconf --force