Ubuntu 16.04 Web Server with Apache, PHP, and MySQL

Ubuntu 16.04 LTS has been out for a little while now, and its standard repos offer some nice enhancements over Ubuntu 14: Apache 2.4.18, PHP 7, and MySQL 5.7.12 The process for creating a basic LAMP web server is also streamlined, as a couple of bugs that plagued Ubuntu 14.04 are now eliminated.

To create your LAMP setup:

Install Your Web Server Packages

Ubuntu 16.04 offers current software in its main repos:

root@ubuntu:/# apt-cache madison apache2
       apache2 | 2.4.18-2ubuntu3 | http://us.archive.ubuntu.com/ubuntu xenial/main amd64 Packages

root@ubuntu:/# apt-cache madison php
       php | 1:7.0+35ubuntu6 | http://us.archive.ubuntu.com/ubuntu xenial/main amd64 Packages
       php | 1:7.0+35ubuntu6 | http://us.archive.ubuntu.com/ubuntu xenial/main i386 Packages
root@ubuntu:/# apt-cache madison mysql-server
       mysql-server | 5.7.12-0ubuntu1 | http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages
       mysql-server | 5.7.12-0ubuntu1 | http://us.archive.ubuntu.com/ubuntu xenial-updates/main i386 Packages
       mysql-server | 5.7.12-0ubuntu1 | http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages
       mysql-server | 5.7.12-0ubuntu1 | http://security.ubuntu.com/ubuntu xenial-security/main i386 Packages
       mysql-server | 5.7.11-0ubuntu6 | http://us.archive.ubuntu.com/ubuntu xenial/main amd64 Packages
       mysql-server | 5.7.11-0ubuntu6 | http://us.archive.ubuntu.com/ubuntu xenial/main i386 Packages

Run apt-get update/upgrade, then install everything:

root@ubuntu:/# apt-get install apache2 mysql-server php-mysql php-fpm

As before, if you install Apache and PHP-FPM simultaneously, Apache will be configured to use Event as its multi-processing module:

root@ubuntu:/# apache2ctl -M
Loaded Modules:
 core_module (static)
 so_module (static)
 watchdog_module (static)
 http_module (static)
 log_config_module (static)
 logio_module (static)
 version_module (static)
 unixd_module (static)
 access_compat_module (shared)
 alias_module (shared)
 auth_basic_module (shared)
 authn_core_module (shared)
 authn_file_module (shared)
 authz_core_module (shared)
 authz_host_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 deflate_module (shared)
 dir_module (shared)
 env_module (shared)
 filter_module (shared)
 mime_module (shared)
 mpm_event_module (shared)  <------
 negotiation_module (shared)
 setenvif_module (shared)
 status_module (shared)

Zend OPcache is included with PHP 7:

root@ubuntu:/# php -v
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

Verify everything is running:

root@ubuntu:/# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0    *               LISTEN      884/sshd
tcp        0      0*               LISTEN      2530/mysqld
tcp6       0      0 :::80                   :::*                    LISTEN      2273/apache2
tcp6       0      0 :::22                   :::*                    LISTEN      884/sshd

Make Apache Talk to PHP-FPM

Verify the listen directive in www.conf:

root@ubuntu:/# vi /etc/php/7.0/fpm/pool.d/www.conf
; listen directive should look like this
listen = /run/php/php7.0-fpm.sock

Enable Apache’s proxy modules:

root@ubuntu/# a2enmod proxy_fcgi # this also enables mod_proxy

Next, edit your virtual host file to add the ProxyPassMatch directive. I just used the 000-default.conf 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/

Finally, restart Apache, then create index.php at /var/www/html, add phpinfo() to it, and go to your server’s IP address or domain in your web browser. If everything is working you’ll see the PHP Info page:

PHP Info Page

Database Connections

To test your database connection, edit index.php again and add some PDO statements:

$user = 'mysql_username';
$pass = 'mysql_password';
try {
    $dbh = new PDO('mysql:host=localhost;dbname=mysql', $user, $pass) 
} catch (PDOException $e) {
    return 'Fail: ' . $e->getMessage();

$stmt = $dbh->prepare('SELECT user FROM user');

while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    echo '<p>' . $row['user'] . '</p>';

$dbh = null;

If your connection is working, and you’ve made no other changes to your database, this will print the existing MySQL users to your web browser:


So now you have a LAMP web server running the latest version of Ubuntu, the high-performance Apache2 Event module, MySQL, and PHP 7 with the Zend bytecode cache.


Leave a Reply

Your email address will not be published. Required fields are marked *