LAMP Basic Installation on Ubuntu 16.04 Server

By | May 16, 2017
LAMP

LAMP (Basic) Installation on Ubuntu 16.04 Server

LAMP is a very popular server configuration already covered by countless tutorials and HowTo’s readily found with a basic web search. The following tutorial was constructed on request from several colleagues and fellow students who want to setup a basic LAMP server for lab, or home use.

YES, I am well aware that there are several, possibly better in some ways, procedures for installation and configuration of LAMP. This is just one variant I have used that I know works.

First, giving credit where credit is due. The information used for this article was taken from several sources  too numerous to list all of them. The primary sources come from the most excellent “Perfect Server” series of articles, Digital Oceans LAMP stack on Ubuntu 16.04, and Linodes LAMP on Ubuntu 16.04

For those comfortable with installation and configuration, and just want the steps and commands used, you can find the short essential version of this configuration here…

Disclaimer: Any mistakes or misinformation that the article may contain are my sole responsibility, and not the fault of any of the authors of source material. Additionally, all information in the article pertains to a known and verified working configuration. Every effort has been made to ensure that the information provided is accurate and up to date, but there may be instances where commands or parts of the procedure do not works as intended. Any mistakes or misinformation is unintentional. For this reason any person or entity that uses the information provided in this article does so at their own risk.
For those that are comfortable with installing and configuring, and just want a quick reference of steps and commands, I have created a short essential version of this LAMP configuration that can be accessed here

Configuration Information

The following is sample user, network, and domain, information used in the article. Wherever applicable, substitute you own information:

Domain = mydomain.home

Hostname = web

Server IP = 192.168.1.10

Netmask = 255.255.255.0

Gateway = 192.168.1.1

DNS (Google) = 8.8.8.8 and 8.8.4.4

Initial sudo user account = maint

Password = chosenpassword

 

Assumptions

A working machine with Ubuntu 16.04 Server installed and a working Internet connection.

Suggested

Another computer that has the Putty terminal application so you can configure while sitting in an office chair with your feet up instead of standing in front of a terminal.

Helpful

Access to another computer with Internet access that can be used to research any possible glitches that might pop-up

 

Some Basic Server Configuration

It is a good idea to update the OS and make sure the root password is set.

[simterm]maint@web:~$ sudo apt-get update[/simterm]
[simterm]maint@web:~$ sudo apt-get upgrade[/simterm]

 

Set the root password
[simterm]maint@web:~$ sudo passwd root[/simterm]

At the prompt, enter the maint sudo password if requested, and then enter the desired root password the first time, and then enter it a second time at the request prompt

 

At this point, if you had chosen to configure the network interface manually during installation, you can fire up Putty on a remote machine such as your favorite desktop machine, and log into your server remotely via SSH.

If you had chosen to configure networking via auto-configuration, no worries. After initial login simply run ifconfig in the terminal and get the servers current IP address. You can use this address to connect via Putty, or continue using the local terminal.
[simterm]maint@web:~$ ifconfig[/simterm]

To clarify, the, IP address is the inet addr of the Ethernet link (Link encap:Ethernet).  Make sure and use your inetaddr in any applicable steps that follow

Now it is time to tweak network  settings.

If you chose to auto-configure the network during installation, this is where you will configure the network for a static IP address and other network configuration information. This is also where you can add additional DNS options if desired:

 

Example

Use the text editor Nano to open the network configuration file.
[simterm]maint@web:~$ sudo nano /etc/network/interfaces[/simterm]

Edit the configuration with the required configuration information

iface eth0 inet static
        address 192.168.1.10
        netmask 255.255.255.0
        network 192.168.1.1
        broadcast 192.168.1.255
        gateway 192.168.1.1
        # dns-* options are implemented by the resolvconf package, if installed
        dns-nameservers 8.8.8.8 8.8.4.4
        dns-search search google-public-dns-a.google.com search

 

When finished, save the file and close nano.

Save the file = Ctrl o

Confirm = y

Exit = Ctrl x

 

Restart network services
[simterm]maint@web:~$ sudo ifdown eth0 && ifup eth0[/simterm]

 

LAMP – Download, Install, and Setup Apache 2 Webserver

Since we are going to be downloading and installing several applications, it is a bit simpler to run them from a root shell to eliminate the sudo part of commands:

[simterm]maint@web:~$ sudo su -[/simterm]
Enter the sudo password.

root shell
[simterm]root@web:~# [/simterm]
Install Apache
Install Apache with the following command:

[simterm]root@web# apt-get install apache2 apache2-utils[/simterm]
After installation is complete, check and see if Apache is running. Open a web browser and direct the address to your server

http://192.168.1.10

You should get the Apache2 default sample page

Configure your web site

The first step is to create a directory for your site. In this article we will create our directory within the default web file path, but not in the default html directory. This way your web site and the default site are kept separate

[simterm]root@web:~# mkdir -p /var/www/mydomain.home/public_html[/simterm]
Set group and folder permission

Add user maint to the www-data group and set permissions on your web site folder. (Note, the following usually work correctly, but if FTP will be used to upload web pages, permissions may need to be adjusted):

Add maint to www-data group and grant permissions in the group
[simterm]root@web:~#root@web:~# usermod -a -G www-data maint[/simterm]

Set ownership of the web folder to group www-data
[simterm]root@web:~# chown www-data:www-data /var/www/mydomain.home/public_html[/simterm]

Set permissions on the web folder
[simterm]root@web:~# chmod -R 755 /var/www/mydomain.home/public_html[/simterm]
Create an Apache virtual host configuration file from the default installed during the apache installation. Use copy/paste renaming the destination file to your domain config name. NOTE: the name can pretty much be what you want as long as it ends with .conf
[simterm]root@web:~# cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/mydomain.home.conf[/simterm]

Open the new config file with nano. Below is an example of the virtual host configuration inside the mydomain.home.conf file. The configuration file will contain a lot of configuration documentation which can make it look a bit intimidating. The important thing is to ensure that all configuration information is between the virtual host tags. You may not have to edit the last few lines. Edit the configuration file between the virtual host tags to match the following example, and ignore the rest for now. Use your email address for the ServerAdmin email:

[simterm]root@web:~# nano/etc/apache2/sites-available/mydomain.home.conf[/simterm]

<VirtualHost *:80>
    ServerAdmin user@legitemail.com
    ServerName web.mydomain.home
    ServerAlias www.mydomain.home
    DocumentRoot /var/www/mydomain.home/public_html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

When finished, save the file and close nano.

Save the file = Ctrl o

Confirm = y

Exit = Ctrl x

 

Enable the site in Apache
[simterm]root@web:~# a2ensite mydomain.home.conf[/simterm]
Disable the default Apache site
[simterm]root@web:~# a2dissite 000-default.conf[/simterm]
Restart Apache 2 service to apply the configuration change
[simterm]root@web:~# systemctl restart apache2[/simterm]
If everything restarts with no errors it is time to test your new virtual host. To do this we will use nano to create a basic html index file:
[simterm]root@web:~# nano /var/www/mydomain.home/public_html/index.html[/simterm]
Type or copy / paste the following into the new file:

<html>
  <head>
    <title>Welcome to my domain home page !</title>
  </head>
  <body>
    <h1>Success!  The mydomain.home virtual host is working!</h1>
  </body>
</html>

Save and close Nano

Save the file = Ctrl o

Confirm = y

Exit = Ctrl x

After the index.html file has been created and saved, check and ensure that the site is active and displays the web page we just created. Open a web browser and direct the address to your server

http://192.168.1.10

You should get the new index page.

Note: local access to your site is based primarily on the default site being disabled. Multiple virtual hosts on a local network are dependent on more configuration including DNS adjustments. If this site is the only site you are going to run on your local network, you can use the site name or the machine IP to access it as long as the default site is disabled.

web_page_success
LAMP – Install and Configure MariaDB

The “M” part of LAMP usually refers to “MySQL”. Through a series of events MySQL is now owned by Oracle. It could be argued that this is, or this is not, a bad thing. This argument is beyond the scope of this article. In the spirit of the open source community, the original creators of MySQL created a replacement that is still completely free and open source named MariaDB. MariaDB is full-featured drop in replacement for MySQL. The commands are standard SQL commands, and the folder hierarchy even uses MySQL structure and file names. In other words, MariaDB is a free replacement of MySQL that is for all intents and purposes a mirror replacement.

Install MariaDB
[simterm]root@web:~# apt-get install mariadb-client mariadb-server openssl binutils[/simterm]
When the installation is finished, the next step is to fire off the Secure Installation configuration:
[simterm]root@web:~# mysql_secure_installation[/simterm]

The Secure Installation will ask a series of questions. The following shows the responses:

Answers for Secure Installation Questions

Enter current password for root (enter for none): -- (enter)
Set root password? [Y/n] -- (y)
New password: -- (desiredpassword)
Re-enter new password: -- (desiredpassword)
Remove anonymous users? [Y/n] -- (y)
Disallow root login remotely? [Y/n] -- (y)
Reload privilege tables now? [Y/n] -- (y)

 

A quick configuration edit with Nano
[simterm]root@web:~# nano /etc/mysql/mariadb.conf.d/50-server.cnf[/simterm]

Find the line: bind-address 127.0.0.1

Comment this line out with a # at the beginning. Alternately, I have also seen where instead of commenting this line out, change the 127.0.0.1 value to 0.0.0.0

Comment out this line:

#bind-address      127.0.0.1

Save and close Nano

Save the file = Ctrl o

Confirm = y

Exit = Ctrl x

Restart MariaDB service to apply the configuration change
[simterm]root@web:~# systemctl restart mysql[/simterm]
Ensure the SQL service is running correctly:
[simterm]netstat -tap | grep mysql [/simterm]
You should get a response similar to the below:

sqlgrep

 

LAMP – Install, Enable, and Test PHP 7

The following command will install PHP 7 which is the latest version. NOTE: there are several other PHP modules available not installed in the article instructions. I recommend that you visit Perfect Ubuntu – 16.04 Server if you are wanting to install advanced PHP modules and options.

Install PHP 7:

[simterm]root@web:~# 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[/simterm]

 

When the installation completes, we want to enable it in Apache:

[simterm]root@web:~# a2enmod php7.0[/simterm]

 

The next step is somewhat optional, but is a good way to test and ensure PHP is working on your server.

Use Nano to create a phpinfo.php file:

[simterm]root@web:~# nano /var/www/mydomain.home/public_html/phpinfo.php[/simterm]

Copy the following snippet into the file:

## Copy the snippet of PHP code below

<?php phpinfo(); ?>

 

Save and close Nano

Save the file = Ctrl o

Confirm = y

Exit = Ctrl x

Test

In a web browser, go to http://192.168.1.10/phpinfo.php?

If everything installed correctly, you should get the PHP information and Configuration page.

Finished

 

Next Steps:

For those that are comfortable with installing and configuring, and just want a quick reference of steps and commands, I have created a short essential version of this LAMP configuration that can be accessed here…

 

Take it to the next level..

Now that you have a basic LAMP server setup, your should consider additional services. Below are just a few recommended applications to add ease and functionality:

Install phpMyAdmin. This is a web GUI front end for managing your databases. Very useful.

Install an FTP server. This can be helpful in many ways, but especially useful for moving files between your computer and the web server.

Want to add some basic security to your FTP server? Install and configure fail2ban.

Another useful tool for those that still want to be able to administer their server with a GUI interface, try Webmin.

Want to add a printer to you configuration? Use what was once known as the Common Unix Printing System (CUPS).

Other Options:

Consider an internal caching DNS server with DHCP

Want to learn about installing, configuring, and using Content Management Systems (CMS)’s. Popular CMS’s like WordPress, Drupal, and Joomla all readily install on a LAMP server

 

 

Leave a Reply

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