Ubuntu 16.04 LAMP server tutorial with Apache 2.4, PHP 7 and MariaDB (instead of MySQL)
Configure the Network
Because the Ubuntu insta
ller has configured our system to get its network settings via DHCP, we have to change
that now because a server should have a static IP address. If you want to keep the DHCP based network
configuration, then skip this chapter. Edit /etc/network/interfaces
and adjust it to your needs (in this example
setup I will use the IP address 192.168.1.100 and the DNS servers 8.8.4.4, 8.8.8.8 starting with Ubuntu 12.04, you
cannot edit /etc/resolv.conf directly anymore, but have to specify your nameservers in your net
work configuration
see
for more details):
(man resolvconf)
Open the network configuration file with nano:
nano /etc/network/interfaces
The server is using DHCP right after the install; the interfaces file will look like this:
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary netw
ork interface
auto ens33
iface ens33 inet dhcp
To use a static IP address 192.168.1.100, I will change the file so that it looks like this afterward:
# This file describes the network interfaces available on your system
# and how to activate them. For more
information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto ens33
iface ens33 inet static
address 192.168.1.100
netmask 255.255.255
.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1
dns
nameservers 8.8.8.8 8.8.4.4
Then restart your network to apply the changes:
service networking restart
Then edit /etc/hosts.
nano /etc/hosts
Make it look like this:
127.0.0.1 localhost
192.168.1.100 server1.example.com server1
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6
localhost ip6
loopback
ff02::1 ip6
allnodes
ff02::2 ip6
allrouters
Now, we will change the ho
stname of our machine as follows:
echo server1 > /etc/hostname
hostname server1
The first command sets the hostname “server1” in the /etc/hostname file. This file is read by the system at boot
time. The second command sets the hostname in the current sess
ion so we don’t have to restart the server to
apply the hostname.
As an alternative to the two commands above you can use the hostnamectl command which is part of the systemd
package.
hostnamectl set
hostname server1
Afterward, run:
hostname
hostname
f
T
he first command returns the short hostname while the second command shows
the fully qualified domain name
(fqdn):
root@server1:/home/administrator# hostname
server1
root@server1:/home/administrator# hostname
f
server1.example.com
root@server1:/home/admin
istrator#
Congratulations! Now we have a basic Ubuntu 16.10 server setup that provides a solid basis for all kind of Ubuntu
Server setups.
LAMP is short for Linux, Apache, MySQL, PHP. This tutorial shows how you can install an Apache 2 web server on
an Ubuntu 16.04 (Xenial Xerus) server with PHP 7 (mod_php) and MySQL support. Additionally, I will install
PHPMyAdmin to make MySQL administratio
n easier. A LAMP setup is the perfect basis for CMS systems like
Joomla, WordPress or Drupal.
Preliminary Note
In this tutorial, I will use the hostname server1.example.com with the IP address 192.168.1.100. These settings
might differ for you, so you ha
ve to replace them where appropriate.
I recommend to use a minimal Ubuntu server setup as basis for the tutorial, that can be a virtual
or root server
image with an Ubuntu 16.04 minimal install from a web hosting company or you use our minimal server tuto
rial
to
install a server from scratch.
I’m running all the steps in this tutorial with root privileges, so make sure you’re logged in as root:
sudo su
Installing MariaDB as MySQL replacement
We will install MariaDB instead of MySQL. MariaDB is a MySQL fo
rk maintained by the original MySQL developer
Monty Widenius. MariaDB is compatible with MySQL and provides interesting new features and speed
improvements when compared to MySQL. Run the following command to install MariaDB
server and client:
apt
get
y i
nstall mariadb
server mariadb
client
Now we set a root password for MariaDB.
mysql_secure_installation
You will be asked these questions:
Enter current password for root (enter for none): <
press enter
Set root password? [Y/n] <
y
New password: <
Ent
er the new MariaDB root password here
Re
enter new password: <
Repeat the password
Remove anonymous users? [Y/n] <
y
Disallow root login remotely? [Y/n] <
y
Reload privilege tables now? [Y/n] <
y
Test the login to MariaDB with the “mysql command”
m
ysql
u root
p
and enter the MariaDB root password that you’ve set above. The result should be similar to the screenshot below:
To leave the MariaDB shell, enter the command “quit” and press enter.
Install Apache 2.4
Apache 2 is available as an Ubuntu package, therefore we can install it like this:
apt
get
y install apache2
Now direct your browser to http://192.168.1.100, and you should see the Apache2 default page (It works!):
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.
Install PHP 7
We can install PHP 7 and the Apache PHP module as follow
s:
apt
get
y install php7.0 libapache2
mod
php7.0
Then restart Apache:
systemctl restart apache2
Test PHP and get details about your PHP installation
The document root of the default web site is /var/www/html. We will now create a small PHP file (info.php
) in that
directory and call it in a browser. The file will display lots of useful details about our PHP installation, such as the
installed PHP version.
nano /var/www/html/info.php
<?php
phpinfo();
?>
Then change the owner of the info.php file to the www
data user and group.
chown www
data:www
data
/var/www/html/info.php
Now we call that file in a browser (e.g. http://192.168.1.100/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:
apt
cache search php7.0
Pick the ones you need and install them like this
:
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
Now restart Apache2
:
systemctl restart apache2
PHP 7 has now MySQL / MariaDB support as shown in phpinfo() above.
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:
apt
get
y install
php
apcu
Now restart Apache:
systemct
l restart apache2
Now reload http://192.168.1.100/info.php in your browser and scroll down to the modules section again. You
should now find lots of new modules there:
Please don’t forget to delete the info.php file when you don’t need it anymore as it pr
ovides sensitive details of
your server. Run the following command to delete the file.