How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu

The

LAMP stack is a set of open source software that is typically installed together to allow a server to host dynamic websites and web applications. This term is an acronym that represents the Linux operating system, with the web server Apache. Site data is stored in an Mand SQL database and PHP processes the dynamic content.

In this guide, we will get a LAMP stack installed on an Ubuntu 16.04 server. Ubuntu will meet our first requirement: a Linux operating system.

Prerequisites

Before you begin this guide, you must have a separate non-root user account with sudo privileges configured on the server. You can learn how to do this by completing steps 1-4 in our initial server setup tutorial for Ubuntu 16.04.

Step 1 — Installing Apache and Setting the Firewall

The Apache web server is one of the most popular web servers in the world. It’s well-documented and has been widely used for much of the web’s history, making it a great default choice for hosting a website.

We can easily install Apache using Ubuntu’s package manager, apt. A package manager allows us to install most software seamlessly from a repository maintained by Ubuntu. You can learn more about how to use apt here.

For our purposes, we can start by typing these commands:

sudo apt-get update sudo

  1. apt-get
  2. install apache2

Since we are using a sudo command, these operations are executed with root privileges. It will ask you for the password of your usual user to verify your intentions.

Once you’ve entered your password, apt will tell you which packages you plan to install and how much additional disk space they will take up. Press y and press ENTER to continue, and the installation will continue.

Configuring Global ServerName to suppress syntax warnings

Next, we’ll add a single line to the /etc/apache2/apache2.conf file to suppress a warning message. Although harmless, if you do not set ServerName globally, you will receive the following warning when checking your Apache configuration for syntax errors

: sudo apache2ctl configtest OutputAH00558:

  1. apache2

: The fully qualified domain name of the server could not be reliably determined, using 127.0.1.1. Set the ‘ServerName’ directive globally to suppress this message Correct syntax Open the

main configuration file with its text Edit:

sudo nano /etc/apache2

  1. /apache2.conf

Inside, at the bottom of the file, add a ServerName directive, pointing to your primary domain name. If you don’t have a domain name associated with your server, you can use your server’s public IP address:

. . . ServerName server_domain_or_IP

Save and close the file when you are finished

.

Next, check for syntax errors by typing

: sudo apache2ctl configtest

Since we added the ServerName global directive, all you should see is

: OutputSyntax OK

Restart Apache to implement your changes:

  1. sudo systemctl restart apache2

You can now start adjusting

the firewall.

Setting your firewall to

allow web

traffic

Next, assuming you have followed the initial server configuration instructions to enable the UFW firewall, make sure that your firewall allows HTTP and HTTPS traffic. You can ensure

that UFW has an application profile for Apache as follows: sudo

  1. ufw app list

OutputAvailable applications: ApacheApache FullApache Secure OpenSSH

If you look at Apache’s full profile, it should show that it allows traffic to ports 80 and 443

:

  1. Sudo UFW “Apache Full” Application

Information OutputProfile: Apache Full title: Web server (HTTP, HTTPS) Description: Apache v2 is the next generation of the ubiquitous Apache web server. Ports: 80,443/tcp

Allow incoming traffic for this profile:

  1. sudo ufw allow in “Apache Full”

You can do a random check right away to verify that everything went as planned by visiting your server’s public IP address in your web browser (see the note below the heading below to find out what your public IP address is if you don’t already have this information):

http://your_server_IP_address

You will see the default Ubuntu 16.04 Apache webpage, which is there for informational and testing purposes. It should look something like this:

Ubuntu 16.04 Apache default

If you

see this page, then your web server is now properly installed and accessible through your firewall

.

Find the

server’s public IP address If you don’t know what your server’s public IP address is, there are several ways to find it. Usually, this is the address you use to connect to your server via SSH.

From the command line, you can find this in several ways. First, you can use the iproute2 tools to get your address by typing this:

  1. ip addr show eth0 | grep inet | awk ‘{ print $2; }’ | sed ‘s//.*$//’

This will return two or three lines to you. They are all correct addresses, but your computer may only be able to use one of them, so feel free to try each one.

An alternative method is to use the curl utility to contact an external party to tell them how they see your server. You can do this by asking a specific server what

its IP address is:

  1. sudo apt-get install curl curl http://icanhazip.com

Regardless of the

method you use to get your IP address, you can type it into the address bar of your web browser to access your server.

Step 2 — Installing MySQL

Now that we have our web server up and running, it’s time to install MySQL. MySQL is a database management system. Basically, you will organize and provide access to databases where our site can store information.

Again, we may use apt to purchase and install our software. This time, we will also install some other “helper” packages that will help us get our components to communicate with each other:

  1. sudo apt-get install mysql-server

Again, you will be shown a list of the packages that will be installed, along with the amount of disk space they will take up. Type Y to continue.

During installation, your server will ask you to select and confirm a password for the MySQL root user. This is an administrative account in MySQL that has higher privileges. Think of it as being similar to the root account for the server itself (however, the one you’re setting up now is a MySQL-specific account). Make sure this is a strong, unique password, and don’t leave it blank.

When the installation is complete, we want to run a simple security script that will remove some dangerous defaults and block access to our database system a bit. Start the interactive script by running:

  1. mysql_secure_installation

You will be prompted to enter the password you set for the MySQL root account. You will then be asked if you want to configure the VALIDATE PASSWORD PLUGIN.

Answer and for yes, or anything else that continues not to be enabled.

VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security. It checks the strength of the password and allows users to set only those passwords that are strong enough. Do you want to configure the VALIDATE PASSWORD plug-in? Press y| And for Yes, any other key for No:

You will be prompted to select a password validation level. Note that if you enter 2, for the strongest level, you will receive errors when trying to set any password that does not contain numbers, uppercase and lowercase letters and special characters, or that is based on common dictionary words.

There are three levels of password validation policy: LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case and special characters STRONG Length >= 8, numeric, uppercase and lowercase, special characters, and dictionary file Enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 1

If you enabled password validation, you will be shown a password strength for the existing root password, and asked if you want to change that password. If you are satisfied with your current password, enter n for “no” when prompted: Use existing password for root

. Estimated password strength: 100 Change password for root? ((Press y| And for Yes, any other key for No) : n For the

rest of the questions, you need to press Y and press the Enter key on each request. This will remove some anonymous users and the test database, disable remote root logins, and load these new rules so that MySQL immediately honors the changes we’ve made.

At this point, your database system is already set up and we can move on.

Step 3 – PHP Installation PHP

is the

component of our configuration that will process the code to display dynamic content. You can run scripts, connect to our MySQL databases for information, and deliver the processed content to our web server for display.

Once again, we can take advantage of the suitable system to install our components. We will include some helper packages as well, so that the PHP code can run under the Apache server and talk to our MySQL database:

  1. sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql

This should install PHP without any problems. We’ll test this in a moment.

In most cases, we’ll want to modify the way Apache serves files when a directory is requested. Currently, if a user requests a directory from the server, Apache will first look for a file called index.html. We want to tell our web server to prefer PHP files, so we’ll have Apache search for a file .php index first.

To do this, type this command to open the dir.conf file

in a text editor with root privileges

:

  1. sudo nano /etc/apache2/mods-enabled/dir.conf

It will look like this:

<IfModule mod_dir.c> DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm </IfModule>

We want to move the PHP index file highlighted above to the first position after the DirectoryIndex specification, Like this:

<IfModule mod_dir.c> DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm </IfModule>

When finished, save and close the file by pressing Ctrl-X. You will need to confirm the save by typing Y and then press Enter to confirm the save location of the file.

After this, we need to restart the Apache web server for our changes to be recognized. You can do this by typing this

: sudo systemctl restart apache2

We can also check the status of the

apache2 service using systemctl:

  1. sudo systemctl status

apache2 Sample Output● apache2.service – LSB: Apache2 web server Loaded: loaded (/etc/init.d/apache2; bad; provider preset: enabled) Drop-In: /lib/systemd/system/apache2.service.d └─

  1. apache2-systemd.conf

Active: active (running) since Wed 2016-04-13 14:28:43 EDT; 45s ago Documents: man:systemd-sysv-generator(8) Process: 13581 ExecStop=/etc/init.init.d/apache2 stop (code=exited, status=0/SUCCESS) Process: 13605 ExecStart=/etc/init.init.d/apache2 start (code=exited, status=0/SUCCESS) Tasks: 6 (limit: 512) CGroup: /system.slice/apache2.service ├─13623 /usr/sbin/apache2 -k start ├─13626 /usr/sbin/apache2 -k start ├─13627 /usr/sbin/apache2 -k start ├─13628 /usr/sbin/apache2 -k start ├─13629 /usr/sbin/apache2 -k start └─13630 /usr/sbin/apache2 -k start Apr 13 14:28:42 ubuntu-16-lamp systemd[1]: Stopped LSB: Apache2 web server. Apr 13 14:28:42 ubuntu-16-lamp systemd[1]: Starting LSB: Apache2 web server… Apr 13 14:28:42 ubuntu-16-lamp apache2[13605]: * Starting Apache httpd web server apache2 Apr 13 14:28:42 ubuntu-16-lamp apache2[13605]: AH00558: apache2: The fully qualified domain name of the server could not be reliably determined, using 127.0.1.1. Set the ‘ServerNam Apr 13 14:28:43 ubuntu-16-lamp apache2[13605]: * Apr 13 14:28:43 ubuntu-16-lamp systemd[1]: Started LSB: Apache2 web server.

Install PHP modules

To improve the functionality of PHP,

we can optionally install some additional modules. To

see the options available for PHP modules and libraries, you can pipe apt-cache search results into less, a pager that allows you to scroll through the output of other commands:

  1. apt-cache search php- | less

Use the arrow keys to scroll up and down, and Q to quit smoking.

The results are all the optional components that you can install. It will give you a brief description for each:

libnet-libidn-perl – Perl links for GNU Libidn php-all-dev – package that depends on all supported PHP development packages php-cgi – server side, scripting language embedded in HTML (CGI binary) (default) php-cli – command line interpreter for PHP scripting language (default) php-common – Common files for PHP packages php-curl – CURL module for PHP [default] php-dev – Files for the PHP module development (default) php-gd – GD module for PHP [default] php-gmp – GMP module for PHP [default] … :

To learn more about what each module does, you can search the internet or you can view the long description of the package by typing:

  1. apt-cache show package_name

There will be a lot of output, with a field called Description-en that will have a longer explanation of the functionality that the module provides.

For example, to find out what the php-cli module does

, we could write this:

  1. apt-cache show php-cli

Along with a lot of other information, you’ll find something that looks like this:

Output… Description: Command line interpreter for PHP scripting language (default) This package provides the /usr/bin/php shell, useful for testing PHP scripts from a shell or performing general shell scripting tasks. . PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely used open source general-purpose scripting language that is especially suitable for web development and can be embedded in HTML. . This package is a dependency package, which depends on the default PHP version of Debian (currently 7.0). …

If, after research, you decide you want to install a package, you can do so using the apt-get install command as we have been doing for our other software.

If we decide that php-cli is

something we need, we could write

: sudo apt-get install php-cli

If you want to install more than one module, you can do so by listing each one, separated by a space, by following the apt-get

install command like this:

  1. sudo apt-get install
  1. package1package2…

At this point, your LAMP stack is installed and configured. However, we should still test our PHP.

Step 4 — Testing PHP processing on your web server

To test that our system is properly configured for PHP, we can create a very basic PHP script

.

We’ll call this script info.php. For Apache to find the file and serve it properly, it must be saved in a very specific directory, which is called the web root.

In Ubuntu 16.04, this directory is located in /var/www/html/. We can create the file in that location by typing:

  1. sudo nano /var/www/html/info.php

This will open a blank file. We want to put the following text, which is valid PHP code, inside

the file: <?php phpinfo(); ?>

When you’re done, save and close the file

.

Now we can test if our web server can correctly display the content generated by a PHP script. To test this, we just have to visit this page in our web browser. You will need your server’s public IP address again.

The

address you want to visit will be

: http://your_server_IP_address/info.php The page you

arrive at should look like this:

Ubuntu 16.04 default PHP info

This page basically gives you information about your server from a PHP perspective. It is useful for debugging and to ensure that settings are applied correctly.

If this was successful, then your PHP is working as expected.

You probably want to delete this file after this test, as it might actually provide information about the server to unauthorized users. To do this,

you can type this:

  1. sudo rm /var/www/html/info.php

You can always re-create this page if you need to access the information again later

.

Conclusion

Now that you have a LAMP stack installed, you have plenty of options on what to do next. Basically, you have installed a platform that will allow you to install most types of websites and web software on your server.

As an immediate next step, you need to make sure that connections to your web server are secure, serving them over HTTPS. The easiest option here is to use Let’s Encrypt to secure your site with a free TLS/SSL certificate.

Some other popular options are:

  • Install WordPress, the most popular content management system on the internet
  • .

  • Configure PHPMyAdmin to help
  • manage your MySQL databases from the web browser.

  • Learn more about MySQL to manage your databases
  • .

  • Learn how to use SFTP to transfer files to and from your server.

Contact US