Server Side Stories #1 : LAMP

In this post, we shall set up and configure a LAMP ( Linux, Apache, MySQL, PHP ) web server. This is the industry standard for web applications and  although other alternatives such as WAMP and XAMPP exist, the LAMP stack is popular because it has proven to be more flexible, user-friendly and secure than all other options

<NOTE>
O. This tutorial will be quite easy if you have previous experience in working with Linux systems (Like Ubuntu, Fedora .etc) and if you are comfortable with using the command line. If not, don’t worry. Go through this tutorial and everything should work fine. Just have a willingness to learn on the fly.
1. This tutorial is for Ubuntu and other Debian derived OSs (like elementary OS, Linux Mint) which use the aptitude package manager, If you are using some other OS, the instructions would be similar. Instead of apt-get, you would have to change the command to “yum” or “pacman” depending on your package manager
for ex. if you see a command sudo apt-get update change it to sudo yum update or sudo pacman update depending on your case.
2. Normal human instructions look like this. Code to be typed in looks this.
3. If you are stuck anywhere or have any doubts, feel free to drop a comment and I will get back to you soon. However a google search should give you most, if not all answers. Just type in the correct set of key-words 🙂
</NOTE>

Introduction

Since we have already installed Linux, our task now is to install the other three components, Apache server, MySQL and PHP.
The Apache server “serves” your web-pages to people who request it.
The MySQL database is a popular open-source RDBMS used for storing structured user-data.
We will be installing phpMyAdmin which gives a user-friendly graphical interface to help work with MySQL even if you don’t have much idea about SQL query syntax.
PHP is a programming language which is used to create dynamic web pages which change based on user-input.

Installation

Shortcut

The LAMP stack is such a popular setup that debian and debian-derived systems have a single command to install all of them together.
However I strongly suggest installing the components one by one because you can learn a lot in the process.
However, if you are in a hurry, this should help you.
Just type in

sudo tasksel install lamp-server

in your terminal and it should take care of the rest, prompting whenever a user input is required
If, for reasons, known and unknown, the above command doesn’t work, just follow along installing one by one.

Apache Web server

Open up a terminal and type in sudo apt-get install apache2 . Say yes to all installation questions.

To check if the installation is successful, open up a web browser and type in “localhost” and press enter.

You should be able to see this if everything went right.
snapshot1

MySQL

Again type in sudo apt-get install mysql-server-5.5  into the terminal.

During installation, it will ask you to set a password for the “root” user of MySQL, provide a secure,easy-to-remember password

Once you are done, to log into MySQL as root, type in mysql -u root -p

Enter your password when prompted, once inside run the following query to remove anonymous users.
delete from mysql.user where user='';
Once done, type in exit to exit MySQL

PHP

Open up a terminal and type in sudo apt-get install php5 php5-cgi libapache2-mod-php5 php5-common php-pear

Once the installation is complete, type in

sudo vi /etc/apache2/mods-enabled/mime.conf

 near line 220 type in
AddHandler php5-script .php
Finally,restart apache server.Instructions on how to restart the server is given later on in this post.

To check if you have installed PHP correctly and to check your PHP configuration,

go to /var/www and create a file with a .php extension (say foo.php),type in

<?php

phpinfo();

?>

Save and close

Open up a browser and type in localhost/foo.php.

 

If you have installed PHP correctly, you should be able to see something like this

phpinfo

phpMyAdmin

Fire up a terminal and type in sudo apt-get install phpmyadmin

During the installation, you will be asked to select the web server, select “apache2” since we installed the apache web server.

You will also be asked whether you want to configure a database, select “no” as we have already installed and configured a MySQL database.

Once installation is complete, type in the following commands.

sudo cp /etc/phpmyadmin/apache.conf /etc/apache2/conf.d/phpmyadmin.conf

sudo vi /etc/apache2/conf.d/phpmyadmin.conf

Go to line 8 and enter the following

Order Deny,Allow
Deny from all
Allow from 127.0.0.1 10.0.0.0/24

These are to make sure that the phpMyAdmin portal can be accessed only at the server (127.0.0.1) and nowhere else.

Save and close the file and restart the apache server.

To check if phpMyAdmin is installed correctly, open up a browser and type in “localhost/phpmyadmin”.
You will be greeted by this page, type in the username and password you provided during the MySQL installation and log in.
snapshot2

Ok, now that you have setup the stack and made sure that everything is working fine.
We’ll now talk about the locations of important files that you may need to edit or look up for configuring and debugging purposes.

Important files

Logs

The server logs are stored in /var/log/apache2 folder

Here you can see two type of files, access.log and error.log, what these contain is explained later.
If you have been running the apache server long enough, you can see access.log.<<some_number>>.gz files too.
These are archived logs and can be viewed using zcat. The most recent entries will be always in the access.log file,
the next recent are in access.log.1 and the older ones are archived in access.log.2.gz, access.log.3.gz and so on.

To see the latest “N” requests,
run tail -n N access.log

Logs record the IP, time and page requested by various users. Depending upon your configurations, it may have more or less information.
If you are running a website which has a large amount of traffic, the log files can be extremely large.
For example, websites like google, facebook, wikipedia have logs which are potentially TBs in size.

This is the big in big data.

The error.log contains error messages as well as information on when the server was shutdown, restarted .etc. Hopefully, this log should be small

Similarly, the MySQL logs are stored in /var/log/mysql folder. The data they store is similar to what the apache logs store.

The PHP error logs too are stored in /var/log/apache2 folder.

Configuration files

Configuration files are what you use to tweak the behaviour of applications to your needs. This mostly involves opening up the config file and editing the values of  various options, what the option does and how you can change the behaviour are well documented in the respective configuration files and I will not be going into the details.

Apache Server

The main config file is /etc/apache2/apache2.conf

There are several other config files in the sub-directories for configuring the active modules, setting up multiple websites to be hosted on the same server, which ports to listen to .etc. These details are given in the beginning of the apache2.conf file so you can read it whenever you want.

MySQL

The config file is at /etc/mysql/my.cnf

This file holds the global configurations which applies for all users.You can copy this file to your home directory and rename it to .my.cnf to set user-specific configurations

PHP

The PHP config file for apache is at /etc/php5/apache2/php.ini

This config file defines the behaviour of php in the apache server. In simpler terms, how php scripts will be executed in the browser.
If you have installed php command line interface (which is useful if you are running php scripts in a cron job, for example), its config file would be in /etc/php5/cli/php.ini

phpMyAdmin

No prizes for guessing, the config file’s at /etc/phpmyadmin/apache.conf

If you are using the lighttpd server(which is not our case!), the config file would be /etc/phpmyadmin/lighttpd.conf
Apart from these two config files, there are several php scripts which are used to access the MySQL database and to modify the look of phpMyAdmin.
In most cases, you would never have to edit any of these files,not even the conf files of phpMyAdmin

<NOTE>
O. You need to  be root whenever you are editing any of the config files (edit with the sudo prefix. for ex, sudo vi /etc/phpmyadmin/apache.conf).
1. Once you have done editing the files, you need to restart the service for the changes to be effected.
for example, if you have edited the /etc/apache2/apache2.conf file, you need to run sudo service apache2 restart to restart apache2.
Similarly, run sudo service mysql restart to restart MySQL.
2. The above case is an example where there would be a new entry in error.log file of apache,go take a look.
</NOTE>

Web pages

These are what your users will see when they connect to your server. By default, the index.html file is the first page presented to the user. You can change this file or even create your index.html file to replace it.

The files which can be viewed as webpages are to be stored in /var/www/ directory.

You can create subdirectories here to organize your pages.
This is the default behaviour and you can change it by editing the config file 😉

The 403 issue

In some cases, you can get a “403: Forbidden” error when you try to open a web page in browser.

403error
Like this

This is usually caused due to a file permissions issue. Since you are editing as root, the other users do not have permission to read it.
The solution is to change the file permissions by running the chmod command. Commonly used file permission is 755 (rwx-r_x-r_x), which means that the owner of the file can read, write and execute whereas others, whether the same group or not, can only read or execute the file. This is exactly what we need. (If you are interested to learn more about file permissions in Unix, let me know in the comments, I might write a post on it)
Suppose a file, foo.html is giving you the 403 error.

Just run sudo chmod 755 foo.html and then refresh the page.This will fix the issue.

That’s it!
Congratulations, you have now successfully setup and configured a LAMP stack !!

You can now play around with the config files to see what combination suits you best.

P.S. This is my first lengthy blog post. If there are any typos, please let me know.

Advertisements

Published by

gokul

Hi!, My name is Gokula Krishnan, you can (and I prefer) call me gokul. I'm a third year Computer Science major from BITS Pilani. I'm interested in Technology, Theoretical Computer Science and Discrete Mathematics. A FOSS enthusiast, I'm one of the founders of the BITS Firefox community. I'm currently working on Big Data Analytics, Machine learning and UNIX shell programming. My not-so-geeky hobbies include playing volleyball and football and origami

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s