How to setup LAMP - Apache (with directory structure and virtual hosts), mysql, and php


In this article you will learn how to setup Apache webserver with its directory structure. You’ll also learn how to setup virtual hosts with Apache, configure MySQL along with PHP so that you can use your web applications that require PHP along with MySQL for its database storage.

 

Before we move on, let’s understand few terminologies that we will be using to setup Apache, mysql and php.

 

Note: We are going to install LAMP which means L=Linux A=Apache M=MySQL and P=PHP

Webservers, MySQL, virtual hosts defined

Webserver are servers that deliver or serves web pages. You have a webpage that you want public to see you need a webserver for that. Now PHP is a scripting language which gets configured with the webserver- in our case it is an Apache webserver and serves pages writing in PHP. MySQL is the database where the application data or information or records are stored and the PHP scripting languages queries for those to display them in their application.

Virtual Hosts in Apache lets you host multiple websites on a Single IP address, If you have domain1.com you can host it easily using Apache but if you have domain2.com, domain3.com or domain4.com and you want all of them to be served by Apache but all of them have different pages, then you will need Virtual Hosting for this purpose.

 

Now, we will install LAMP (Apache with MySQL and PHP) on Debian based operating system Ubuntu. It will work with Debian 6,7 Ubuntu 10.04 LTS, 12.04 LTS and even Ubuntu 13.

 

To start with you will learn how to install Apache webserver and then understand its directory structure. Then you’ll learn how to create a virtual host later after the installation.

 

You either need to run commands as root and ignore sudo in below commands or if you are logged in with your user it should be a sudo user.

 

sudo apt-get update

sudo apt-get upgrade
sudo apt-get install apache2

 

The first command is updating software update repositories. The second is upgrading software packages installed against the updated repositories. The third command is installing Apache webserver.


You can use below command to see what is the IP address.

# ifconfig

 

For example, if it is 192.168.10.20 then I can see a default page of Apache webserver “It works”

 

You can view in your web browser

http://192.168.10.20

 

Now, let’s install PHP 5 and make it with Apache

 

sudo apt-get install php5 libapache2-mod-php5

 

Above command installs php5 and module for Apache so that webserver understand php module and files.

In order to get these changes in effect, restart Apache server web service.

sudo /etc/init.d/apache2 restart

 

Let’s test if PHP5 is working with Apache2

Use your favorite text editor. We’re using “vim”. You can either use “nano” or “gedit” if you like having a graphical interface.

Default directory of Apache which serves file is /var/www and we will create a test php file which will give information about the PHP and we will be able to verify that PHP is not working

sudo vim /var/www/phpinfo.php

 

Following is the content of the file you need to fill in:

Phpinfo();

?>

Save the file and close the file.

Open you rweb browser and type the following into the web address bar:

http://192.168.10.20/phpinfo.php

 

There will be a purple page with PHP version that means congratulations you have installed php5 and setup with Apache.

 

Now the database part: you will learn to install MySQL database and make it work with PHP5. You will install its module and verify if its integrated with PHP.

 

sudo apt-get install mysql-server mysql-client

 

The above command will install MySQL server and its client- this will be needed later.

During the installation, MySQL will ask you to set a root password. If you miss for some reason to set the root password while it was installing, don’t worry it is very easy to set the password later from within the MySQL shell / console.

 

Once you have installed MySQL, we should activate it with this command:

sudo mysql_install_db

 

Run the MySQL set up script

Now, run the MySQL set up script:

sudo /usr/bin/mysql_secure_installation

 

This script will ask you for your current root password which you set earlier while installation prompt.

If you want your server to be accessible from other servers or machines, you must edit the "Bind Address" directive in the MySQL configuration file by editing the file "my.cnf" in /etc/mysql directory

 

sudo vim /etc/mysql/my.cnf

 

Change bind-address = 127.0.0.1 to your IP address which you found earlier.

 

After changing it will be something like below:

 

bind-address = 192.168.10.20

 

Now install Apache and PHP configuration modules to be able to utilize MySQL with them. You should also install phpmyadmin which is a web application used for the administration of databases.

sudo apt-get install libapache2-mod-auth-mysql php5-mysql phpmyadmin

 

You can view phpmyadmin by going to below URL and entering username as root and password which you had set earlier.

http://192.168.10.20/phpmyadmin

Now we need to restart Apache to reload MySQL and PHP module in it.

sudo /etc/init.d/apache2 restart

 

PHP also has a variety of useful libraries and modules that you can add onto your server. You can see the all the available libraries that are available by using the below command.

apt-cache search php5-

 

then install by respective module name

 

apt-get install php5-modulenamehere

 

If you want to test if MySQL has been loaded, you can again view the phpinfo.php page and search for MySQL which will show you the MySQL server version installed and integrated with PHP.

Success! You have now successfully installed and configured our LAMP stack and Apache is setup with PHP and MySQL and working perfectly.

 

Now as we discussed earlier about Apache Virtual Host, if you want to use multiple websites using same ip which is also called “Shared hosting” or “Shared IP hosting” there are additional requirements.

Create a virtual host in Apache

The first step to creating a Virtual host in Apache is creating a directory where the files for that particular virtual host will be saved.

In this section of article we will be using domain called “example.com” as our virtual host and you can use anything you like or your actual domain.

 

sudo mkdir -p /var/www/example.com/public_html

 

This path of the new directory created will be your Document Root in the Apache virtual configuration, which we will need to define in the file later. By adding a -p to the command “mkdir” which is used for creating a new empty directory, the command automatically creates all directories that do not exist.

If you are using an actual domain you will need the DNS to point to the IP of the server. If you just want to test we will describe how to check if virtual hosting is working.

 

We need to grant permission and ownership to the newly created directory.

 

sudo chown -R www-data.www-data /var/www/example.com/public_html

 

sudo chmod -R 755 /var/www

 

Within the directory “/var/www/example.com/public_html” we need to create a file called “index.html” and put some text in it to see if things are working.

 

 

 

    www.example.com

 

 

 

Welcome to our first Virtual Host in Apache – www.example.com

 

 

 

 

If you have pointed your domain name to the server in DNS following the steps above, you can skip this step.

 

vim /etc/hosts

 

We edit the file called “/etc/hosts”

Below is the sample file if you want to use it you can use it as well.

# Host Database

#

# localhost is used to configure the loopback interface

# when the system is booting.  Do not change this entry.

##

127.0.0.1       localhost.localdomain

 

#Virtual Hosts

192.168.10.20    example.com

 

You can see we have pointed the domain to our webserver ip.

This file is also called “Local DNS resolver”. Default behavior of Linux/ UNIX is to check this file first then move onwards to the DNS server.

Configuration of Apache

Copy the existing configuration file that comes with Apache so that you can make changes into the file.

 

sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/example.com


The first step is to insert a line for the ServerName under the ServerAdmin line in the file you copied above.

The ServerName specifies the domain name that the virtual host uses in our case it is “example.com”

If you want to make your virtual host site accessible from more than one domain name or sub domains, you can include the alternate names in your virtual host file by adding a ServerAlias Line after ServerName. There is no limit to add ServerAlias as much as you want.

Next step is changing the DocumentRoot as this is the directory where the files for the particular virtual hosts are present.

DocumentRoot /var/www/example.com/public_html

 

DocumentRoot looks something like below

DocumentRoot /var/www/example.com/public_html

 

Our changes to this file are complete. Now we need to save and exit.

 

sudo a2ensite example.com

 

The above command enables the Virtual Host in Apache.

Finally, restart the Apache service and you are done!

 

sudo service apache2 restart

 

Open the browser with http://example.com and you can see the file that you had created called “index.html”. This means your Virtual Host is working.

 

To create additional virtual hosts, just repeat above. You can add as many Virtual Hosts as you want.


This article has been provided by Codero Hosting, the leading provider of reliable dedicated, managed and cloud hosting services. Need more information on this topic or to learn more about Codero’s hosting services please visit www.codero.comchat with us online or give us a call at 866-2-CODERO. 

Posted in
Last update:
2015-12-03 20:39
Average rating:0 (0 Votes)