Ubuntu 16.04 LAMP server tutorial with Apache 2.4, PHP 7 and MariaDB

  • Installing MariaDB as MySQL replacement
  • Install Apache 2.4
  • Install PHP 7
  • Get MySQL / MariaDB support in PHP
  • Install the APCu PHP cache to speed up PHP
  • Enable the SSL website in apache
  • Install phpMyAdmin

to be root…
[code]
sudo su
[/code]

update and upgrade
[code]
apt-get update
apt-get upgrade
[/code]

Installing MariaDB as MySQL replacement

[code]
apt-get -y install mariadb-server mariadb-client
[/code]

Now we set a root password for MariaDB.
[code]
mysql_secure_installation
[/code]

You will be asked these questions:

[code]
# mysql_secure_installation

Enter current password for root (enter for none): <– press enter
Set root password? [Y/n] <– y
New password: <– enter the new root password
Re-enter new password: <– retype
Remove anonymous users? [Y/n] <– y
Disallow root login remotely? [Y/n] <– y
Remove test database and access to it? [Y/n] <– y
Reload privilege tables now? [Y/n] <– y

#
[/code]

Test the login to MariaDB with the “mysql command”
[code]
# mysql -u root -p
[/code]

Install Apache 2.4

Apache 2 is available as an Ubuntu package, therefore we can install it like this:
[code]
# apt-get -y install apache2
[/code]
The document root of the apache default vhost is /var/www/html on Ubuntu and the main configuration file is /etc/apache2/apache2.conf. The configuration system is fully documented in /usr/share/doc/apache2/README.Debian.gz.

apache version

[code]
apache2 -v
[/code]

Install PHP 7

We can install PHP 7 and the Apache PHP module as follows:
[code]
apt-get -y install php7.0 libapache2-mod-php7.0
[/code]

Then restart Apache:
[code]
systemctl restart apache2
[/code]

php version
[code]
php -v
[/code]

Test PHP

create info.php file

[code]
nano /var/www/html/info.php
[/code]

info.php

[code]
<?php
phpinfo();
?>
[/code]

Then change the owner of the info.php file to the www-data user and group.
[code]
chown www-data:www-data /var/www/html/info.php
[/code]

Now we call that file in a browser (e.g. localhost/info.php):

As you see, PHP 7.0 is working, and it’s working through the Apache 2.0 Handler, as shown in the Server API line. If you scroll further down, you will see all modules that are already enabled in PHP5. MySQL is not listed there which means we don’t have MySQL / MariaDB support in PHP yet.

Get MySQL / MariaDB support in PHP

To get MySQL support in PHP, we can install the php7.0-mysql package. It’s a good idea to install some other PHP modules as well as you might need them for your applications. You can search for available PHP modules like this:

[code]
apt-cache search php7.0
[/code]

Pick the ones you need and install them like this:

[code]
apt-get -y install php7.0-mysql php7.0-curl php7.0-gd php7.0-intl php-pear php-imagick php7.0-imap php7.0-mcrypt php-memcache php7.0-pspell php7.0-recode php7.0-sqlite3 php7.0-tidy php7.0-xmlrpc php7.0-xsl php7.0-mbstring php-gettext
[/code]

Now restart Apache2:

[code]
systemctl restart apache2
[/code]

PHP with MySQL / MariaDB support.
PHP 7 has now MySQL / MariaDB support.

Install the APCu PHP cache to speed up PHP

APCu is a free PHP opcode cacher for caching and optimizing PHP intermediate code. It is strongly recommended to have an Opcache installed to speed up your PHP page.
APCu can be installed as follows:

[code]
apt-get -y install php-apcu
[/code]

Now restart Apache:
[code]
systemctl restart apache2
[/code]

Please don’t forget to delete the info.php file when you don’t need it anymore as it provides sensitive details of your server. Run the following command to delete the file.

[code]
rm -f /var/www/html/info.php
[/code]

Enable the SSL website in apache

SSL/ TLS is a security layer to encrypt the connection between the web browser and your server. Execute the following commands on your server to enable https:// support. Run:
[code]
a2enmod ssl
a2ensite default-ssl
[/code]

which enables the ssl module and adds a symlink in the /etc/apache2/sites-enabled folder to the file /etc/apache2/sites-available/default-ssl.conf to include it into the active apache configuration. Then restart apache to enable the new configuration:
[code]
systemctl restart apache2
[/code]

Now test the SSL connection by opening https://localhost in a web browser.
Accept SSL Warning in Firefox
You will receive an SSL warning as the SSL certificate of the server is a “self-signed” SSL certificate, this means that the browser does not trust this certificate by default and you have to accept the security warning first. After accepting the warning, you will see the apache default page.
Apache default page in https mode.
The closed “Green Lock” in front of the URL in the browser shows that the connection is encrypted. To get rid of the SSL warning, replace the self-signed SSL certificate /etc/ssl/certs/ssl-cert-snakeoil.pem with an officially signed SSL certificate from an SSL Authority.

Install phpMyAdmin

phpMyAdmin is a web interface through which you can manage your MySQL databases. It’s a good idea to install it:
[code]
apt-get -y install phpmyadmin
[/code]

MariaDB enables a plugin called “unix_socket” for the root user by default, this plugin prevents that the root user can log in to PHPMyAdmin and that TCP connections to MySQL are working for the root user. Therefore, I’ll deactivate that plugin with the following command:
[code]
echo "update user set plugin=” where User=’root’; flush privileges;" | mysql -u root -p mysql
[/code]

Enter the MariaDB root password, when requested by the mysql command.
Afterward, you can access phpMyAdmin under http://192.168.1.100/phpmyadmin/:

link: howtoforge.com