One of my friends, who is a web designer asked me how to install LAMP server in Ubuntu. Because he is recently switched to Ubuntu Desktop Edition and wanted to run a local server. So, I helped him to install the LAMP server with some extra customization. Now, I’m going to share the same steps here through this tutorial. Please Keep in mind that this tutorial is targeted for newbies. Don’t think it’s a long page, so it’s going to be a difficult one. Trust me, it’s not. You are going to execute just a few Terminal commands to make it possible. This page looks longer than other pages only because of it is well explained.
First open the Terminal to execute the commands (Applications>Accessories>Terminal)
First and foremost, ensure that you’re up to date with latest packages on your system by using the following commands,
sudo apt-get update
This command will update the package index with the latest changes made in the repositories.
[Quick Tweak: Just select the command & click the middle mouse button(scroll wheel) on the Terminal to paste it.]
Enter your root password whenever it’s prompted & press Enter to execute the command. You won’t see anything when you type the password ( not even dots). I’m damn sure that you are not going to break your system by executing the commands I provided here.
sudo apt-get upgrade
This command will upgrade all installed packages. This is the equivalent of “Mark all upgrades” in the Synaptic Package Manager.
Let’s start with LAMP Server,
sudo apt-get install lamp-server^
It’ll download & install Apache, MySQL & PHP packages on your System. During the installation process, MySQL will ask you to enter a password for MySQL “root” user. ( Although it’s optional, I recommend you to set a password. It’s going to help you when you install phpMyAdmin. I included a Bonus Tutorial here for installing phpMyAdmin at the bottom of this post.)
By default, the directory for Apache server pages is /var/www . It needs root access in order to put files in. So, let’s create a public_html directory to keep your HTML & PHP files,
This will create public_html directory in your /home. User needs to save his PHP / HTML files in /home/user/public_html
For example, my directory path is /home/charles/public_html
[Quick Tweak: The tilde (~) symbol stands for your home directory. If you are user, then the tilde (~) stands for /home/user]
Now, you’ve to enable userdir mod by using the following command,
sudo a2enmod userdir
Then, restart Apache2
sudo /etc/init.d/apache2 restart
We are almost done our setup. Let’s check it. To test our setup, I request you to create two sample files in HTML & PHP. I provide you the sample codes below,
<html> <!-- Installing LAMP (Linux-Apache-MySQL-PHP) Server in Ubuntu. | http://surferzworld.com/2010/10/installing-lamp-linux-apache-mysql-php-server-ubuntu --> <head> <title>LAMP server setup | http://surferzworld.com </title> </head> <body> <h1>Success! It works!</h1> </body> </html>
Save it as index.html
<?php phpinfo(); /* This will show your installed php version informations | Installing LAMP (Linux-Apache-MySQL-PHP) Server in Ubuntu. | http://surferzworld.com/2010/10/installing-lamp-linux-apache-mysql-php-server-ubuntu */ ?>
Save it as phpinfo.php
Keep these files in your public_html directory.
Now, point your web browser to http://localhost/~user/
Modify this according to your username. For example, mine will be
I hope you can see our Success message, which is inside the index.html file.
Let’s check our php file by visiting
For example, mine will be http://localhost/~charles/phpinfo.php
Does your browser ask if you want to download the php file instead of displaying it? That’s because you may use Ubuntu 10.04 or later versions. I hope you know Ubuntu is based on Debian Linux distribution, they both disabled running PHP scripts in users’ home directories for security reasons. For that we need to re-enable interpretation in users’ home directories . Let’s see how to re-enable it.
Although my favorite editor is Vim, I consider you as a newbie & going to use gedit Text Editor here.
Execute the following command in the terminal,
sudo gedit /etc/apache2/mods-available/php5.conf
It’ll open php5.conf file with root access which is located in /etc/apache2/ mods-available
As indicated in that file itself, you’ve comment out the following lines,
<IfModule mod_userdir.c> <Directory /home/*/public_html> php_admin_value engine Off </Directory> </IfModule>
Then, save your changes.
Your modified file will like this,
<IfModule mod_php5.c> <FilesMatch "\.ph(p3?|tml)$"> SetHandler application/x-httpd-php </FilesMatch> <FilesMatch "\.phps$"> SetHandler application/x-httpd-php-source </FilesMatch> # To re-enable php in user directories comment the following lines # (from <IfModule ...> to </IfModule>.) Do NOT set it to On as it # prevents .htaccess files from disabling it. #<IfModule mod_userdir.c> # <Directory /home/*/public_html> # php_admin_value engine Off #</Directory> #</IfModule> </IfModule>
Now, again you have to restart the Apache,
sudo /etc/init.d/apache2 restart
Clear your browser cache & access this URL http://localhost/~user/phpinfo.php to check your phpinfo.php file in your public_html directory. Obviously, you’ve to replace your username instead of user.
Now, you’ll get your PHP information, that means your PHP is working fine. That’s it. Your LAMP server setup is completed. Hope it shines bright
[Quick Tweak: Anyhow, you are going to visit localhost often. eg: http://localhost/~user/ So, Why don’t you bookmark it? Keyboard shortcut : Ctrl+D]
Bonus tutorial: Installing phpMyAdmin in Ubuntu.
phpMyAdmin helps you to manage MySQL Database via web browser.
Execute the command below to install phpMyAdmin.
sudo apt-get install phpmyadmin
While installing, you will be asked to select the web server to reconfigure automatically. The selected web server will be used to run phpMyAdmin. So we’ve to select “apache2″ at this screen by using Arrow Keys to highlight & Press Space bar to mark it. Then, Press Enter to continue.
Once phpMyAdmin is installed, point your browser to http://localhost/phpmyadmin to start using it. I hope you get a Login Page, enter root as your username and enter your MySQL “root” password you have given while you were setting up MySQL server.
Let’s troubleshoot if you are getting 404 “Not Found” error page. That’s because of not selecting apache2 during the installation process. So, we have to redo the installation by executing the following command,
sudo dpkg-reconfigure -plow phpmyadmin
I’ve just now explained how to select apache2 using the keyboard. So I’m not going to repeat that again.
Now, try again http://localhost/phpmyadmin
This time you are not going to get disappointment. It’ll reach phpMyAdmin login page.
That’s it. I hope this tutorial helps you much. Let me know your results by commenting here. Happy web designing!
Don’t forget to visit Ubuntu ApacheMySQLPHP Documentation for further knowledge.