Install Apache with php-fpm on Ubuntu 16

Date Posted:03-05-2016

Php-fpm is a FastCGI Process Manager which is faster to serve php when compared to mod_php. In this post, we will explain on how to install Apache with php-fpm on Ubuntu 16 host.

Implementation:

Let’s Start with apache2 installation.

apt-get install apache2

By default, ServerName might not be added. We may get warning when we try to restart the apache. To fix the error, we will add ServerName to avoid warning. Open the file /etc/apache2/apache2.conf and add the following line

vi /etc/apache2/apache2.conf

ServerName 127.0.0.1

If the server has hostname, we can add the same.

Check for syntax error.

apachectl -t

Enable the service on reboot safe and start the apache service.

systemctl enable apache2

systemctl restart apache2

Install php-fpm on Ubuntu:

Install php and libapache2-mode-fastcgi.

apt-get -y install libapache2-mod-fastcgi php7.0-fpm php7.0

Enable the modules.

a2enmod actions fastcgi alias

Update the file /etc/apache2/conf-enabled/php7.0-fpm.conf

vi /etc/apache2/conf-enabled/php7.0-fpm.conf

<IfModule mod_fastcgi.c>
                AddHandler php7-fcgi .php
                Action php7-fcgi /php7-fcgi
                Alias /php7-fcgi /usr/lib/cgi-bin/php7-fcgi
                FastCgiExternalServer /usr/lib/cgi-bin/php7-fcgi -socket /var/run/php/php7.0-fpm.sock -idle-timeout 900 -pass-header Authorization
                <Directory /usr/lib/cgi-bin>
                        Require all granted
                </Directory>
</IfModule>

We can add the same configuration on each virtual host if we dont want to specify globally. The above lines should be placed between <virtualhost> </virtualhost>. Default configuration will be /etc/apache2/sites-enabled/00-default.conf.

Verify the syntax error on apache configurations

apachectl -t

Restart the apache service.

systemctl restart apache2

Restart php-fpm service for the changes into effect.

systemctl restart php7.0-fpm

Create a phpinfo page on the document root /var/www/html/phpinfo and check the page on info page.

vi /var/www/html/info.php

<?php
phpinfo();
?>

Change the permission of the file

chown www-data.www-data /var/www/html/info.php

Access the page on phpinfo page on browser. It can be http://IPaddress/info.php. Look for the parameter Server API  We will see FPM/FastCGI.

Leave a Reply