Install Apache, MariaDB and PHP7 (LAMP Stack) on Ubuntu 16.04 LTS
Ubuntu 16.04 LTS Xenial Xerus comes with PHP7 by default so you don’t have to rely on third-party PPA to get PHP7 installed. In this tutorial, we are going to look at how to install Apache, MariaDB and PHP7 (LAMP stack) on Ubuntu 16.04 LTS Xenial Xerus.
Update: This tutorial is also successfully tested on Ubuntu 16.10 Yakkety Yak.
Step 1: Update Ubuntu 16.04 LTS
Before we install any software, it’s always a good idea to update repository and software packages. SSH into your Ubuntu 16.04 server and enter the below commands.
sudo apt-get update sudo apt-get upgrade sudo apt-get dist-upgrade
Step 2: Install Apache Web Server
Enter this command to install Apache Web server.
sudo apt-get install apache2 apache2-utils
After it’s installed, Apache should be automatically started. Check out its status with systemctl.
systemctl status apache2
Output:
● apache2.service - LSB: Apache2 web server
Loaded: loaded (/etc/init.d/apache2; bad; vendor preset: enabled)
Drop-In: /lib/systemd/system/apache2.service.d
└─apache2-systemd.conf
Active: active (running) since Wed 2016-04-20 18:32:57 EDT; 32s ag
o
If it’s not running, use systemctl to start it.
sudo systemctl start apache2
It’s also a good idea to enable Apache to automatically start when Ubuntu 16.04 is rebooted.
sudo systemctl enable apache2
Check Apache version:
apache2 -v
output:
Server version: Apache/2.4.18 (Ubuntu) Server built: 2016-04-15T18:00:57
Now in your browser’s address bar, type the public IP address of Ubuntu 16.04 LTS server. You should see the “It works!” Web page which means Apache Web server is running correctly.
You can use the following command to fetch the public IP address of Ubuntu 16.04 server.
sudo apt-get install curl curl http://icanhazip.com
If you are installing LAMP on your local Ubuntu 16.04 box, just type 127.0.0.1
or localhost
in the browser address bar.
Finally, we need to make www-data
(Apache user) as the owner of web root directory.
sudo chown www-data /var/www/html/ -R
Step 3: Install MariaDB
MariaDB is a drop-in replacement for MySQL. It is developed by former members of MySQL team who concerned that Oracle might turn MySQL into a closed-source product. Many Linux distributions and companies have migrated to MariaDB. So we’re going to install MariaDB instead of MySQL.
sudo apt-get install mariadb-server mariadb-client
After it’s installed, MariaDB server should be automatically stared. Use systemctl to check its status.
systemctl status mysql
Output:
● mysql.service - LSB: Start and stop the mysql database server daemon
Loaded: loaded (/etc/init.d/mysql; bad; vendor preset: enabled)
Active: active (running) since Wed 2016-04-20 18:52:01 EDT; 1min 30s ago
Docs: man:systemd-sysv-generator(8)
If it’s not running, start it with this command:
sudo systemctl start mysql
To enable MariaDB to automatically start when Ubuntu 16.04 is rebooted:
sudo systemctl enable mysql
Now run the post installation security script.
sudo mysql_secure_installation
When it asks you to enter MariaDB root password, press enter because you have not set the root password yet. Then enter y to set the root password for MariaDB server.
Next you can just press Enter to answer all the remaining questions. This will remove anonymous user, disable remote root login and remove test database. This step is a basic requirement for MariaDB database security.
Step 4: Install PHP7
Enter the following command to install PHP7 and PHP7 extensions.
sudo apt-get install php7.0-fpm php7.0-mysql php7.0-common php7.0-gd php7.0-json php7.0-cli php7.0-curl libapache2-mod-php7.0
Enable the Apache php7.0 module then restart Apache Web server.
sudo a2enmod php7.0 sudo systemctl restart apache2
Step 5: Test PHP
To test the cli version of PHP7, we just need to enter this command:
[email protected]:~$ php --version PHP 7.0.4-7ubuntu2 (cli) ( NTS ) Copyright (c) 1997-2016 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies
To test PHP with Apache server, first create a test.php
file in the Web root directory.
sudo nano /var/www/html/test.php
Paste the following PHP code into the file.
<?php phpinfo(); ?>
Save and close the file. Now in the browser address bar, enter server-ip-address/test.php
. Replace sever-ip-address
with your actual IP. Of course, if you follow this tutorial on your local computer, then type 127.0.0.1/test.php
or localhost/test.php
.
You should see your server’s PHP information. This means PHP processing is fine. You can find that Zend OPcache is enabled.
Apache PHP7.0 Module vs PHP-FPM
There are now basically two ways to run PHP code with Apache web server:
- Apache PHP module
- PHP-FPM.
The above configuration uses the Apache PHP7.0 module to handle PHP code. It’s totally fine and if you are happy with it, then delete test.php
file now so that no one else can see your server’s information and don’t follow the instructions below.
But if you want to use PHP-FPM to run PHP code, then you need to enable Apache mod_proxy_fcgi
module with the following command:
sudo a2enmod proxy_fcgi
Then edit the virtual host configuration file. This tutorial uses the default virtual host as an example.
sudo nano /etc/apache2/sites-available/000-default.conf
Add the ProxyPassMatch
directive to this file.
....
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ProxyPassMatch ^/(.*\.php(/.*)?)$ unix:/run/php/php7.0-fpm.sock|fcgi://localhost/var/www/html/
.....
Save and close this file. Restart Apache2.
sudo systemctl restart apache2
Start php7.0-fpm
sudo systemctl start php7.0-fpm
Enable php7.0-fpm to start at boot time.
sudo systemctl enable php7.0-fpm
Check status:
systemctl status php7.0-fpm
Output:
● php7.0-fpm.service - The PHP 7.0 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php7.0-fpm.service; enabled; vendor pre
set: enabled)
Active: active (running) since Wed 2016-04-20 19:21:05 EDT; 2s ago
Now if you refresh the test.php
page in your browser, you will find that Server API is FPM/FastCGI
which means Apache web server will pass PHP requests to PHP-FPM.
For your server’s security, you should delete test.php
file now to prevent prying eyes.
Congrats! You have successfully installed Apache, MariaDB and PHP7 on Ubuntu 16.04 LTS Xenial Xerus.
Why It keep getting ‘ERROR 1698 (28000): Access denied for user ‘root’@’localhost’ ?
php-fpm is installed and activated but nowhere you tell apache to use it. You actually use the apache module, as shown by your screenshot.
Thanks for pointing this out. I have updated my tutorial to include the steps to use php-fpm.
You tell to remove test.php too early, this should be done after FPM/FastCGI check is completed.
Oh, thanks. Tutorial was updated.
Nice write up.
Would be great to include setting up FPM for separate users, like in a shared host situation. I believe this would be very common.
Perhaps something like this? https://www.linode.com/docs/websites/apache/running-fastcgi-php-fpm-on-debian-7-with-apache
Thanks, a very comprehensive walkthrough!
Hi
After added ProxyPassMatch geting Apache error
AH00526: Syntax error on line 22 of /etc/apache2/sites-enabled/000-default.conf:
ProxyPass URL must be absolute!
Action ‘configtest’ failed.
The Apache error log may have more information.
I am using Elementary OS 0.3.2 Freya, built on Ubuntu 14.04
What’s your version of Apache web server?
Apache/2.4.7
I have the same error
I’m trying to install mariadb on ubuntu mate 16.04… I’m getting this
The following packages have unmet dependencies:
mariadb-client : Depends: mariadb-client-10.1 (= 10.1.17+maria-1~xenial) but it is not installable
mariadb-server : Depends: mariadb-server-10.1 (= 10.1.17+maria-1~xenial) but it is not installable
E: Unable to correct problems, you have held broken packages.
Any ideas?
The default Ubuntu 16.04 repository has mariadb-client-10.0 and mariadb-server-10.0. Did you add another repository?
If you want to install MariaDB from MariaDB’s own repository, check out Install MariaDB 10.1 on Ubuntu 16.04. Or, you can remove MariaDB’s own repository and install it from Ubuntu repository.
Hi, Just completed above. Nice and easy to follow. Worked fine till very last check for ServerAPI (FPM/FastCGI). I didn’t see this. It remained as “Apache 2.0 Handler”. I did move the test.php file into web site beneath /var/www/html where it was accessed (ie. the test.php was used successfully). Should it have found it? J
Checking out the apache log /var/log/apache2/access.log or /var/log/apache2/error.log may help.
Hi Thanks for suggestion, Logs did identify problem- “AH01071: Got error ‘Primary script unknown”.
The line
ProxyPassMatch ^/(.*.php(/.*)?)$ unix:/run/php/php7.0-fpm.sock|fcgi://localhost/var/www/html/
kills auto indexing (Options Indexes). Is there a way around this?
PS. I know that auto indexing is generally a bad idea. But this is a laptop, and I’m not serving to anyone but myself.
Hello, how come no other tutorial online mentions this statement;
ProxyPassMatch ^/(.*.php(/.*)?)$ unix:/run/php/php7.0-fpm.sock|fcgi://localhost/var/www/html/
Also I’ve read I using this instead which doesn’t work for me;
SetHandler “/var/run/php/php7.0-fpm.sock|fcgi://localhost
#SetHandler “proxy:fcgi://127.0.0.1:9000/”
This was very informative, thank you!
Great tutorial. Your list of packages to get for the php7 install helped me solve a problem I was having with LAMP on Mint19 – Tara. I think I had missed 3 or 4 when installing each of A M P individually using Synaptic.
Many thanks.
I a running Ubuntu 16.04 on a vm server. Upon installing mariadb, I get the following message
Any ideas? Thanks.
Correction. I am running Ubuntu 18.04.
Thank you for sharing the knowledge I needed.