You are here: Surferz World // Tools // Installing LAMP ( Linux – Apache – MySQL – PHP ) Server in Ubuntu.

Installing LAMP ( Linux – Apache – MySQL – PHP ) Server in Ubuntu.


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,

mkdir ~/public_html

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,

1) HTML

<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

2) PHP

 
<?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
http://localhost/~charles/

I hope you can see our Success message, which is inside the index.html file.

Let’s check our php file by visiting

http://localhost/~user/phpinfo.php

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.

Share

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

35 Responses to " Installing LAMP ( Linux – Apache – MySQL – PHP ) Server in Ubuntu. "

  1. Mubashir says:

    Thanks for the tutorial. It really helped me.

  2. Steve says:

    This tutorial was absolutely amazing. You took a complex process (at least for a newbie), and made it perfectly do-able. You helped me out tremendously, and I owe you my gratitude.

    • Kees says:

      What Steve said!

      You rock, helped me a bunch (as in with everything)

      Tweeted you in the hope others can benefit as well.

  3. Hello,guy!I like this article,your blog is great!

  4. The need for Linux experts is rising. The good thing is that you can now find Linux consultants online easily. With the right evaluation, you will find one who will provide the answers to your requirements.

  5. To integrate synamic functionalities in websites, PHP is one application that needs to be learned. It is good that there are sites that offer PHP programming consultancies or programming services. With these services, you can easily upgrade your site to a more dynamic one.

  6. Hi,
    I’m using Ubuntu 10.10
    & when I go to: http://localhost/~myUsername/
    or: http://localhost/~myUsername/index.html

    Depending on the URL I try to use, I get a 403 error, which says:
    Forbidden
    You don’t have permission to access /~myUsername/ on this server.
    Apache/2.2.16 (Ubuntu) Server at localhost Port 80
    or it says:
    Forbidden
    You don’t have permission to access /~myUsername/index.html on this server.
    Apache/2.2.16 (Ubuntu) Server at localhost Port 80

    I went to Properties > Permissions on my public_html on my home folder & set them all to create, read & write, but it still didn’t work. Then I applied the same permission settings to my home folder, and still got the same thing.

    Any advice for me?

    • Charles Richard says:

      Could you please post the output of

      ls -l ~/public_html

      It’s not advisable to grant all permissions to the home folder. However, this problem relates to the permission issues only. Create index.html as described in this post & try to access that file. For your own folders, apply permissions (Create and delete files) to the enclosed files too. Let me the know the results. If you could n’t achieve, I’ll assist you through Skype.

  7. JWizzy101 says:

    Thanks for the advice. I reset my homefolder permissions to how they were. The output of “ls -l ~/public_html” was:
    total 12
    -rw-rw-rw- 1 myUsername myUsername 0 2010-11-28 10:32 index.html

    I tried making a new index.html file & changing the permissions, but still no joy. If you could help, it’d be appreciated so much.

    My Skype username is: *edited*

    • Charles Richard says:

      gotcha… I’ve sent you an Skype message, will catch u there. Our Timezone matters.. :)

  8. Jay says:

    Thanks for everything man. You’ve been a big help. =]

  9. CLearness says:

    Thanks, i like it.
    In the first time, sorry for my english ^^.

    In a second time, I say my problem.

    I have do what you said, but i have always the same problems :
    When i want to go in the PhpMyAdmin Panel with [http://localhost/phpmyadmin], I have an error message :

    Forbidden
    You don’t have permission to access /phpmyadmin on this server.
    Apache/2.2.16 (Ubuntu) Server at localhost Port 80

    I don’t know how i can great permissions to access it, it’s a big problem, for example if I want to install DRUPAL, DRUPAL can be connect to the Database :/

    Please help me :D
    Bye

    • Charles Richard says:

      Shall I assist you by Skype? Please share your skype id. I’ll catch you there.

  10. Alex says:

    hi friends,
    i follow the instruction step by step but i got this error when tried to point to the public_html folder:
    Forbidden

    You don’t have permission to access /~alex/phpinfo.php on this server.

    Apache/2.2.16 (Ubuntu) Server at localhost Port 80

    whats wrong??

    • Charles Richard says:

      I request you to check the public_html folder permission by using the following command ls -l ~/public_html . & all also make sure to comment out the proper lines inside php5.conf file. If you could not able to do, share your skype id. I’ll assist you there.

    • Anonymous says:

      here:

      alex@alex-HP-Mini-210-1000:~$ ls -l ~/public_html
      total 24
      -rw-rw-r– 1 alex alex 190 2010-12-06 16:37 index.html
      -rw-rw-r– 1 alex alex 20 2010-12-06 16:35 phpinfo.php
      alex@alex-HP-Mini-210-1000:~$

    • Charles Richard says:

      It’s better, if you come online on skype. It’ll make my job easier to find, when you share your screen.

    • Alex says:

      by the way please add me on skype: *edited*

  11. Jim says:

    Champion effort thank you soo much!

  12. nagesh says:

    I dont think I ever came across a command as powerful as: sudo apt-get install lamp-server^. This qualifies that *nix is easier than windows.

  13. Huberto says:

    First of all, great tutorial.
    Sometimes it may be needed also to add following line:
    “Include /etc/phpmyadmin/apache.conf” to apache2.conf file.

    Cheers,
    H

  14. EFP says:

    Thank you so much, worked perfect first time:)

  15. dane says:

    Thanks so much! great tutorial!

  16. Regarding the security issue of using PHP in the users home fodler.

    I fou you feel you want to stick to the ubuntu/debian concept of security, wouldn’t it be advisable to explain how to set permissions on the /var/www/ folder right?

    to give all permissions to it type the following line in your terminal:

    sudo chmod -R 777 /var/www/

    I’m not sure it is better in anyway, since i’m a newbie… i just speak my mind…

    • Charles Richard says:

      Thanks. but, try this one too

      sudo chown -R root:www-data /var/www

      Personally, I’m used to formatting my File System(/), every 6 months. So, keeping it on public_html folder doesn’t make me to backup again. And also, I don’t like to give 777 (read/write/execute to everyone) permission to anything on a server. :)

  17. Vishwasjois says:

    Hello Charls
    Thanks a lot for this wonderful step by step tutorial.
    This is one of the best tutorial i have ever seen.
    i am able to run my server perfectly
    Thanks again

  18. Ahmed Kamal says:

    Thank you very much indeed, it was so easy and simple to be followed.

  19. You are a godsend. You have managed to get me to setup my PC in the shortest time possible. May God bless you.

  20. Tiffany says:

    You rock! Thank you so much for this. I am up and running thanks to your help.

  21. ice says:

    Hey, thanks a million!! Now my html & php files are in the good place!

  22. labqt says:

    An hitec knowledge been provided for people like me

    Thank you for the wonderful job

  23. バーバリー says:

    Nice post. I was checking constantly this weblog and I am inspired! Extremely helpful information particularly this ultimate section :) I maintain such information a lot. I was seeking this certain information for a long time. Thank you and good luck.

  24. Mincir En Courant says:

    It’s actually a great and helpful piece of info. I am happy that you just shared this helpful information with
    us. Please keep us informed like this. Thank you for
    sharing.
    Mincir En Courant recently posted..Mincir En Courant

Trackbacks/Pingbacks

  1. [...] Installing LAMP ( Linux – Apache – MySQL – PHP ) Server in Ubuntu. | Surferz World (tags: Ubuntu lamp tutorial) [...]

Leave a Reply

CommentLuv badge

Copyright © 2014 Surferz World. All rights reserved.
Powered by WordPress