How to install LAMP on Ubuntu 16.04

How to install LAMP on Ubuntu 16.04

LAMP stands for Linux, Apache, MySQL and PHP. This is a group of open source applications commonly used to run a web server for hosting and development. This tutorial will include the steps needed to set up a simple web server to get your website up and running. 

Linux is the operating system used to run your server. Since it is already installed we will skip this step. 
Connect to your server though an SSH Session to run the following steps.

Step 1: Installing Apache

Apache is an open source, commercial-grade web server. We will install Apache with the apt command provided by Ubuntu's package manager. 
In your terminal window type the following commands:
  1. sudo apt-get update
  2. sudo apt-get install apache2
When using the sudo command, it will prompt you for your password to execute root privileges. 
You will see something like this 
user@ubuntu1604:~$ sudo apt-get install apache2
[sudo] password for user:
Reading package lists... Done
Building dependency tree      
Reading state information... Done
The following additional packages will be installed:
  apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.1-0 ssl-cert
Suggested packages:
  www-browser apache2-doc apache2-suexec-pristine | apache2-suexec-custom openssl-blacklist
The following NEW packages will be installed:
  apache2 apache2-bin apache2-data apache2-utils libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap liblua5.1-0 ssl-cert
0 upgraded, 10 newly installed, 0 to remove and 0 not upgraded.
Need to get 1,559 kB of archives.
After this operation, 6,448 kB of additional disk space will be used.
Do you want to continue? [Y/n]
The default is Y or Yes so you can just click enter and it will start the installation. 
Once this command has been executed the Apache web server will be installed. You can check this by visiting your web server IP address.
  1. http://ip-address-of-your-server/
Once you visit this page you will see the default Apache2 Ubuntu Page. It will look something like this: 


If you see this page then you have installed Apache correctly and we are ready to move on. 

Step 2: Install MySQL

Our next step now that Apache is installed, is to install a database management system called MySQL. This is needed for sites like Wordpress and many other development applications that need to store information in a database. 
From the command line type the following command: 
  1. sudo apt-get install mysql-server
This time running the sudo command does not prompt you for the password as you are within a set time to continue using root privileges. 
You will see an output similar to this:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libaio1 libcgi-fast-perl libcgi-pm-perl libencode-locale-perl libfcgi-perl libhtml-parser-perl libhtml-tagset-perl libhtml-template-perl libhttp-date-perl libhttp-message-perl libio-html-perl liblwp-mediatypes-perl libtimedate-perl liburi-perl mysql-client-5.7
  mysql-client-core-5.7 mysql-common mysql-server-5.7 mysql-server-core-5.7
Suggested packages:
  libdata-dump-perl libipc-sharedcache-perl libwww-perl mailx tinyca
The following NEW packages will be installed:
  libaio1 libcgi-fast-perl libcgi-pm-perl libencode-locale-perl libfcgi-perl libhtml-parser-perl libhtml-tagset-perl libhtml-template-perl libhttp-date-perl libhttp-message-perl libio-html-perl liblwp-mediatypes-perl libtimedate-perl liburi-perl mysql-client-5.7
  mysql-client-core-5.7 mysql-common mysql-server mysql-server-5.7 mysql-server-core-5.7
0 upgraded, 20 newly installed, 0 to remove and 0 not upgraded.
Need to get 18.0 MB of archives.
After this operation, 158 MB of additional disk space will be used.
Do you want to continue? [Y/n]

During the installation you will be prompted for a password for the root user. Be sure to use a strong password, DO NOT leave this blank. 
This user will have increased privileges. We recommend you adding a very secure password for this user. You will be able to create additional uses later. 



After you enter your password it will ask you to confirm the password.


Next we will run a security command that will set a few initial parameters that are more secure than the default Ubuntu/MySQL installation
Do this by running the following command: 
  1. sudo mysql_secure_installation
After you run this command you will be asked for the 'root' password of your MySQL database. This is the same password you entered above. Once you enter that password it will ask you if you want to change that password. If you are ok with the current password enter 'n' or 'no' and press enter.
It will continue asking you questions: 

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin? 
Press
There are three levels of password validation policy: 
LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:  2

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

Remove anonymous users? [Y/n] 
Disallow root login remotely? [Y/n] 
Remove test database and access to it? [Y/n]
Reload privilege tables now? [Y/n]
You can choose the default Y and click enter though all of these. This will make sure your MySQL instance is secure. 

Now that we have our MySQL database installed we can move on to installing PHP.

Step 3 Install PHP

PHP is used to display dynamic content on your web page. It can communicate with the MySQL database to get information to display to your users. 
To install PHP run the following command in the terminal: 
  1. sudo apt install php7.0 libapache2-mod-php7.0 php7.0-mysql
This command will install the latest version of PHP and a few extra modules that will be helpful with your development. 
You will see an output similar to this: 
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  php-common php7.0-cli php7.0-common php7.0-json php7.0-opcache php7.0-readline
Suggested packages:
  php-pear
The following NEW packages will be installed:
  libapache2-mod-php7.0 php-common php7.0 php7.0-cli php7.0-common php7.0-json php7.0-mysql php7.0-opcache php7.0-readline
0 upgraded, 9 newly installed, 0 to remove and 0 not upgraded.
Need to get 3,596 kB of archives.
After this operation, 14.4 MB of additional disk space will be used.
Do you want to continue? [Y/n]
The default is Y or Yes so you can just click enter and it will begin installation. As like before if you are still within the windows to use root privileges you will not be prompted for a password. 
We also suggest installing Line 1, and if you plan on installing wordpress we suggest Line 2. 
  1. php7.0-curl php7.0-json php7.0-cgi
  2. php-curl php-gd php-mbstring php-mcrypt php-xml php-xmlrpc

Now that we have PHP installed we want to edit the configuration files to tell Apache to use our preferred file index.php. By default the web server will use index.html. 
To do this we need to edit the following file:
  1. sudo vim /etc/apache2/mods-enabled/dir.conf
The contents of that file will look something like this:
<IfModule mod_dir.c>
        DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm
</IfModule>
Press i to start typing and edit the file to look like it does below:
<IfModule mod_dir.c>
        DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>
Now press ESC then :wq and that will write and save the file. 
Now that we have edited this file we need to tell the Apache web server to read this file again and execute the changes we made. 
To do this run the following command: 
  1. sudo service apache2 restart

Step 4: Testing PHP

Now that we have everything installed for our basic web server. We need to test to make sure PHP is working and being phrased properly. To do that we can create a basic PHP file in our root direct of our website located at /var/www/html/. 
Type the following in the terminal to create the file:
  1. sudo vim /var/www/html/info.php
This will open a blank file and allow us to edit the contents. In this file enter the following information: <
  1. <?php
  2. phpinfo();
  3. ?>
Now you can save and close the file just like we did before press ESC then :wq
Now you can visit your IP address and add /info.php afterwards like this:
  1. http://ip-of-your-server/info.php
You should get a response that looks something like this. 



This info page will give you information about what modules are installed for your PHP instance. 

Conclusion

Now that we have everything installed and working for our basic web server. It is time for you to get to work to show the world what you can do. This basic setup will allow you to install Wordpress, the most popular content management system. 
We hope this tutorial has helped you. 


    • Related Articles

    • How to install LAMP on Ubuntu 14.04

      LAMP stands for Linux, Apache, MySQL and PHP. This is a group of opensource applications commonly used to run a web server for hosting and development. This tutorial will include the steps needed to set up a simple web server to get your website up ...
    • Managing MySQL

      Plesk Shared Hosting: Managing MySQL XMission's Shared Hosting powered by Plesk has phpmyadmin essentially built right into it! From the Control Panel, click on the "Domains & Websites" tab near the top of the screen. On the left, click the button ...
    • Adding MySQL Databases

      Plesk Shared Hosting: Adding MySQL Databases Create and remove databases used by your websites, and manage them using integrated administrative web application. From the Control Panel, click on the "Domains & Websites" tab near the top of the screen. ...
    • Password Protecting a Directory With ".htaccess"

      Introduction This document assumes you have a basic understanding of the UNIX shell. Web access control on XMission is a function of the web server. It lets you set permissions on who can access your web documents based on username/password, a group ...
    • What is Spam?

      What is Spam? Spam is a word adopted by the Internet community to describe mass-message posting via email or Usenet newsgroups. These messages are usually unsolicited and invasive commercial advertising, typically for get-rich-quick scams or to ...