Install LAMP on Ubuntu 16.04

Fast and Easy
red square swatch
black square swatch
dimgray square swatch

NOTE: The following set-up is intended for developers who want to have a LAMP (Linux-Apache-MySQL-PHP) development environment on their own computer. It is not intended as a set-up for web servers serving the general public, which would require much more attention to security, scalability, etc.

Install Entire LAMP Stack in Five Minutes

Install Apache2, MySQL, and PHP in about five minutes with a minimum of muss and fuss. This is a fantastic Ubuntu project that installs all three programs in one fell swoop. It is a miracle of simplicity! In your terminal, type this comand:

sudo apt-get install lamp-server^

Don't forget the caret!

The terminal will at some point ask you to confirm the download. Answer yes.

When it asks you to enter a password for root user for MySQL, enter a strong password. Some programs interacting with a MySQL database these days (PHPMyAdmin, for example) will insist upon setting a strong password, so if you've set a weak one for MySQL, you won't be able to access MySQL from PHPMyAdmin. Save yourself some hassle and choose a strong one now. Don't leave it blank. Enter a strong password with at least 10 characters and throw in a number, symbol and uppercase letter.

Test That Apache Has Installed Correctly

Open a web browser window and enter the address

http://localhost/

You should see a default Apache welcome page filled with infromation about using and configuring Apache. This welcome page is located at

/var/www/html/index.html

and you can replace it with another index.html page if you wish.

Create a User Folder to Hold Your Web Pages and Scripts

This solves the problem that Web pages in Apache2's var/www/html folder are owned by root, which makes it difficult for you as a mere user to edit those pages. There are various solutions to this problem, but here is the one that I prefer: Set up your own user directory that you own, yet that Apache2 can use to display Web pages and run scripts just as it does in its own default directory (var/www/html).

1. Open a terminal and type:

"a2enmod userdir" (without the quotes)

This command will enable Apache2's userdir module.

2. Create a folder within your home directory called "public_html." You can do that using your regular file browser by opening your Home directory and right-clicking inside it and selecting "New Folder." Or you can do it in the terminal by typing: "mkdir ~/public_html" (without the quotes) and then hit enter.

3. Place within the "public_html" folder an index.html file with "Hello world" or other content.

4. Create another folder within the public_html folder called, say, "Testing." Place within it a .php file, called, say, "test.php," with php content such as

<?php phpinfo(); ?>

5. Now you should make sure the permissions for the public_html folder are properly set, using either your file browser or the terminal. In the file browser, right-click on the folder and then click on the "permissions" tab. The owner should say "me" and have permission to create and delete files. The group and others should have "access" to the folder. In the terminal, you can set the permissions for your userdir by typing:

"chmod +x $HOME" (without the quotes)

and hit enter Then type:

"chmod 755 $HOME/public_html" (without the quotes)

and hit enter.

Enable PHP Scripting Within Your "public_html" folder

You must do the following steps to allow php scripts to run within your userdir. Otherwise, when you browse to a .php file, your browser will download it to your hard drive instead of executing it.

You must edit a file that is owned by root. Normally, you could do this with a "sudo gedit" command, namely:

sudo gedit /etc/apache2/mods-enabled/php7.0.conf

However, there currently is a bug (or a feature, depending on your perspective) that does not allow gedit, a graphical editor, to edit files owned by root. The above command currently throws up an error. So, lacking the ability to use gedit to edit a file owned by root, I chose to reset the permissions for the file to make myself the owner rather than root. I was then able to edit the file, php7.0.conf, normally. After making and saving the changes to it, I reset the permissions on the file to return ownership to root.

1. To perform these steps, first open the terminal and type: [NOTE: the correct folder name in the commands below is "mods-enabled" WITH the hyphen]


sudo chown your_user_name:root /etc/apache2/mods-enabled/php7.0.conf

2. Now you are able to use your text editor to edit the php7.0.conf file. Use your file browser to locate the file in the /etc/apache2/mods-enabled/ folder. Find these five lines within the file:

<IfModule mod_userdir.c>
<Directory /home/*/public_html>
php_admin_value engine Off
</Directory>
</IfModule>


and disable these five lines by placing a # sign before each line. Save the file and quit gedit.

3. Change the ownership of the file back to root by opening the terminal and typing:

sudo chown root:root /etc/apache2/mods-enabled/php7.0.conf

Restart Apache

Restart the Apache2 server in order to load changes you have made to Apache2 files. To do this, open a terminal and type:

sudo /etc/init.d/apache2 restart

Hit enter.

If you get an error "Could not reliably determine the server’s fully qualified domain name, using 127.0.1.1 for ServerName" when you try to restart Apache2 on Ubuntu, do the following: Open the "apache2.conf" file at /etc/apache2/apache2.conf It is owned by root, so you will have to take the same steps to edit it as described above for the php7.0.conf file, namely, change ownership to yourself, then change back to root after editing the file. The edit consists of adding

"ServerName localhost" (without the quotes)

as the last line in /etc/apache2/apache2.conf file.

Test Your Setup!

Everything should work now. To test whether it is working, do the following:

1. Open your browser and type into the address bar: "http://localhost/~username/" (without the quotes) and hit enter. You should see the contents of the "index.html" file that you placed in your "public_html" folder.

2. Next, type into your browser's address bar: "http://localhost/~username/Testing/test.php" (without the quotes) and hit enter. Your browser should display the content created by your php script in the Testing folder within public_html.

You should substitute your user name on the computer for "username" in the above Web address. For example,

http://localhost/~johndoe/Testing/test.php

You do not include the "public_html" in the address. Apache2's userdir module is by default configured to navigate automatically to that folder to look for Web content.