This tutorial will walk you through the process of how to setup LAMP stack, which includes the set up of the components such as Apache 2.4, PHP 7, MySql 5.7 with PHP-FPH on Ubuntu 14.04 LTS Trusty Tahr

This comprehensive linux guide expects that you run the following commands as root user but if you decide to run the commands as a different user then ensure that the user has sudo access and that you precede each of the privileged commands with sudo

Install Apache

In this particular guide, we will show how Apache2.4 is configured. First Apache server will be setup and installed.

apt-get install -y apache2
service apache2 start 

/etc/apache2/apache2.conf 

this is the location of the apache file.

first, quickly test that apache web server if it is setup and running fine by accessing http://localhost

Note: The local ip address of the ubuntu machine can replace localhost

Install MySQL or MariaDB

There is an option in between installing MySQL or MariaDB, which can be seen as a drop in replacement for MySQL. in MariaDB there all the enterprise features present but without all the corporate structuring.It is managed by the MariaDB Foundation as opposed to being managed by Oracle which manages MySQL. That said, they are both good choices.

Don’t install both, only one needs to be installed.

Install MySQL

After this, it is time to install MySQL 5.7. For this, both the server and client are needed. The database is run by the server while the interaction with the server is done by the client.

apt-get install -y mysql-server mysql-client

While installing, you will be asked to setup a root password for the MySQL server as well. After this step, the next one is to setup the server. This can be secured by unselecting remote root login and deleting any anonymous users .

mysql_secure_installation

Install MariaDB

Since MariaDB is also an option, as mentioned earlier, and this means it can be installed just like MySql without any further steps. We will be installing MariaDB

apt-get install -y mariadb-server mariadb-client

Similar to MySQL server, a server needs to be setup as well.

mysql_secure_installation

Install PHP

Installation and setup PHP 7.0 with PHP-FPM will be done in this section. PHP FPM is the abbreviation for PHP Fast Process Manager.the mod_fastcgi module will be used to get support for PHP-FPM For Apache2 web server .

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

Remember to disable the default mod_php module and and enable the mod_fastcgi module for PHP-FPM to ensure Apache is operational

a2dismod php7.0
a2enmod actions fastcgi alias

Configure Apache With PHP-FPM

After Apache2.4 and PHP7.0 with PHP-FPM have been installed correctly, apache configuation is needed to begin using it.
A file has to be created to globally setup PHP-FPH

/etc/apache2/conf.d/php7.0-fpm.conf

/etc/apache2/conf.d/php7.0-fpm.conf

/etc/apache2/conf.d/php7.0-fpm.conf
    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 -pass-header Authorization      

Another way to do this is to allow it for selective virtual host, and in this case the following edits have to be made in the virtual host file:

/etc/apache2/sites-available/codesposts.com.conf
    Require all granted

    SetHandler php7-fcgi .php
    Action php7-fcgi /php7-fcgi virtual
    Alias /php7-fcgi /usr/lib/cgi-bin/php7-fcgi
    FastCgiExternalServer /usr/lib/cgi-bin/php7-fcgi -socket /var/run/php/php7.0-fpm.sock -pass-header Authorization      

For PHP7, support for either MariaDB or MySQL needs to be enables since a database server is going to be used for this setup. So the php modules needed to work with the databases must be installed.

apt-get install -y php7.0-mysql php7.0-curl php7.0-gd php7.0-intl php-pear php7.0-imap php7.0-mcrypt php7.0-ps php7.0-pspell php7.0-recode php7.0-snmp php7.0-sqlite php7.0-tidy php7.0-xmlrpc php7.0-xsl

Restart apache and reload PHP FPH.

service php7.0-fpm reload 
service apache2 restart 

To make sure PHP-FPM is enabled along with all of the other PHP modules are setup correctly, create an info.php file with the following information:

/var/www/html/codesposts.com/info.php
<?php phpinfo(); ?>

Next, navigate to http://codesposts.com/info.php . If everything has been correctly installed and enables you should be able to see that FPM/FastCGI is enabled as the server API on the page while all the other PHP modules are installed.