Your Ad Here

Ubuntu LAMP Server Guide - Configure Apache, mySQL, and cgi-bin

Ubuntu LogoIn a previous post, I shared step-by-step instructions for setting up a Ubuntu LAMP (Linux, Apache, mySQL, PHP) server on your local area network (LAN). I also showed how you could customize the server installation to include all of the niceties of Ubuntu’s Desktop GUI. If this installation was successful, you should be able to view the default site served by Apache (actually an index of the default site) using any machine on your LAN. Type the server’s IP address (or alias if you added the server to your /etc/hosts file) in your browser’s address bar or, if you are browsing on the server itself, type 127.0.0.1 or localhost. If an error occurs, then you will have to edit the apache2.conf file to ensure that Apache can fully resolve the server’s name. Out of good measure you should do this anyway, even if everything seems to be working. Use your favorite text editing program (I use vi here) and open the file.

sudo vi /etc/apache2/apache2.conf

Add the following line somewhere:

ServerName localhost

and restart Apache.

sudo /etc/init.d/apache2 restart

If the default site loads then you are ready to configure Apache to serve your own sites. This configuration process may not be familiar to you if someone else hosts your websites. Here I walk you through the necessary steps. Additionally, I show you how to install phpMyAdmin to administer mySQL databases and how to configure a cgi-bin directory to run CGI programs.



Step 1: Create a new Apache site

To create a new site, first create a directory where you want to keep files associated with this site. I like to keep sites in my home directory so (using my user name of sam) I create a /home/sam/public_html directory. The configuration files for all sites available to Apache are stored in the /etc/apache2/sites-available/ directory. Create a configuration file for your new site (here called zaphu) by copying the configuration file for the default site.

sudo cp /etc/apache2/sites-available/default /etc/apache2/sites-available/zaphu

Open the new configuration file.

sudo vi /etc/apache2/sites-available/zaphu

You will notice that there is a lot of stuff between <VirtualHost> tags. You have to edit some of this. Change the DocumentRoot path to point to the newly created site directory.

DocumentRoot /home/sam/public_html

In a similar fashion, change one of the Directory directives (it looks something like this)

<Directory /var/www/>

to be consistent with your site path.

<Directory /home/sam/public_html/>

After making these changes, disable the default site using the Apache disable site script.

sudo a2dissite default

Next, use the Apache enable site script to activate the new site.

sudo a2ensite zaphu

Finally, restart Apache.

sudo /etc/init.d/apache2 restart

Create a simple index.html file and save it in /home/sam/public_html/ to test your new site. Something simple like

<html>
<strong>Ubuntu Hardy Heron is Here</strong>

Browse (preferable on another machine) to your new site to test the configuration.

Step 2: Install phpmyadmin and create root mySQL user

In order to make mySQL database administration easy, many hosts provide phpMyAdmin. You can easily install this tool on your Ubuntu LAMP server too using the built in package management software. At the command prompt type

sudo aptitude update
sudo aptitude install phpmyadmin

You will have to insert the Ubuntu install CD to complete this installation. During the installation the script will ask which version of Apache to automatically configure. If you installed apache as per this previous post then select apache2 (with space bar) and continue. After the install completes, the phpMyAdmin script should be in the /usr/share/directory and (after restarting Apache; see above) it should resolve in your browser when you type localhost/phpmyadmin (or xxx.xxx.xxx.xxx/phpmyadmin). If it doesn’t, you must add the following alias and include to the bottom of the apache configuration file apache2.conf in /etc/apache2/.

Include /etc/phpmyadmin/apache.conf
Alias /phpmyadmin /usr/share/phpmyadmin

You can log in by using root as your username and the root password configured during the mySQL installation (if you decided not to enter a mySQL password or were not prompted for a password then the password field is blank by default). Once logged on to phpMyAdmin, you can set a new root password by navigating to the privileges page, clicking the icon that looks like a pencil next to each root account (there may be more than one), and entering a password in the appropriate field of the page that loads.

Step 3: Configure a cgi-bin directory

Configuring Apache to allow CGI program execution is pretty easy. Create a directory to be used for CGI programs (here I create /home/sam/public_html/cgi-bin) and add the following to the site configuration file (again between the <VirtualHost> tags).

ScriptAlias /cgi-bin/ /home/sam/public_html/cgi-bin/
<Directory /home/sam/public_html/cgi-bin/>
          Options ExecCGI
          AddHandler cgi-script cgi pl
</Directory>

The first line creates an alias that points to the directory in which CGI scripts are stored. The final line tells Apache that only files that end with the *.cgi and *.pl extensions should be considered CGI programs and executed.

Congratulations! Your new site is configured. It is worth mentioning that if you are going to be granting computers on the wide area network access to your new Ubuntu LAMP server, you should take some time to learn how to properly secure Apache. Check out these other sites for more information on configuring Apache under Ubuntu. Ubuntu Community, Apache2.2 Documentation

If you liked this guide, show support by leaving comments and visiting our sponsors.



Update, Aug. 30, 2007: The next version (8.04) of Ubuntu (Hardy Heron) has been announced for release in Q2 2008.

Update, Jan. 14, 2008: I’ve updated the phpmyadmin installation notes above to work with Ubuntu 7.10.


52 Comment(s) rss

  1. Hey, thanks for the awesome guide. Im a total newbie to ubuntu and my boss really wanted to set up an ubuntu web server for one of our upcoming projects. I know I still have a lot to learn as far as securing our server, etc, but you really got me off to a great start. Thanks again!

    Respectfully,
    Sonny

    Sonny | Aug 30, 2007 | Reply

  2. @Sonny - Glad I could help you out. In the future I want to post guides on securing Apache. I have one in the works regarding directory level Apache configuration using .htaccess files. Subscribe to our RSS feed to stay abreast of our latest posts.

    Sam | Aug 31, 2007 | Reply

  3. Thanks, this helped out so much. I planned on creating a test server within my internal network before I tossed it onto a production server hosted elsewhere. Securing it won’t be worth it then I suppose?

    Chris | Sep 26, 2007 | Reply

  4. This guide is perfect. Did not have a single issue with this guide. Thanks

    Anon | Nov 2, 2007 | Reply

  5. Great guide. Had no problems following and I had everything set up in less than ten minutes. Looking forward to learning the ins and outs of administering a web server.
    Thanks,
    DWRZ

    David Wen Riccardi-Zhu | Nov 10, 2007 | Reply

  6. For once, a page that not only Does What It Says On The Can, but also did it in a innovative way. Much impressed with the simplicity of the a2ensite etc., which i will use elsewhere now.

    Top job. Thanks a bundle.

    ed | Nov 14, 2007 | Reply

  7. @Chris, Anon, David Wen Riccardi-Zhu, and ed - Thanks for the feedback. I’m glad you found this guide helpful.

    Sam | Nov 16, 2007 | Reply

  8. hi & thanks for this tutorial. I want to ask you something: i want to use public_html dir to put more sites there:

    /public_html/site1
    /public_html/site2
    /public_html/site3

    do i have to repete steps to add my sites to /etc/apache2/sites-available & then to create the config file for each of it & edit the path?

    Thanks in advanced.

    mafsi | Nov 21, 2007 | Reply

  9. @mafsi - You have the right idea. Configuring multiple sites requires that you create a configuration file for each site in the sites-available directory and then enable each of these sites using the a2ensite command. Of course, you also have to create home directories for each site and restart apache. Let me know if this helps.

    Sam | Nov 24, 2007 | Reply

  10. Sam, this was great. I am starting to feel as if I can use my Ubuntu box for something other than just reading email!

    I have a question you may be able to help me with. I want to be able to run an in-house blog behind our router (with 5 computers running in the house, this isn’t as crazy as it seems). I successfully installed WordPress on my new LAMP server, but when I try to view it from another machine, there is an error, even though I can see the default files in the home directory. What seems to be happening is that as the server serves the WordPress page, the URL is being converted from the IP address to ‘localhost’ which is fine when I’m working on the server machine, but not so fine when I try to view it from another box. If this is a WordPress problem, I’ll look elsewhere. THANK YOU for a really well-written article, I’ll be back to read more.

    Jorah | Nov 25, 2007 | Reply

  11. Sam, please disregard my question about WordPress… I found the correct settings this morning. Thanks again for a great tutorial. I’m working through the DNS articles now!

    Jorah | Nov 25, 2007 | Reply

  12. Jorah,

    I am having the same problem (i.e. wp not accessible except on server machine because URL keeps switching localhost… on other machine).

    Would you let me know what the settings were?

    Thanks in advance,

    Irene | Nov 27, 2007 | Reply

  13. @Irene - When you log into the Wordpress as admin and navigate to options-general what do you see for Wordpress URL and Blog address? These should contain something like http://192.168.1.110/wordpress not localhost/wordpress.

    Franklin | Nov 27, 2007 | Reply

  14. Frank,

    thanks a lot. I found that to be the problem. Blog address had localhost. Stupid error when setting it up a while ago…

    Irene

    Irene | Nov 27, 2007 | Reply

  15. Hey, I love your article, but i have a problem when installing phpmyadmin…

    I’ve followed your tutorial to the letter (apart from installing gui)

    and when I do “sudo aptitude install phpmyadmin”
    I’m told that the dependency php5mcryp is uninstallable…

    please help!

    Zeekstah | Dec 12, 2007 | Reply

  16. @Zeekstah - Are you spelling this dependency php5mcryp correctly? I can’t find anything on it on google. You’ve got me stumped.

    Sam | Dec 17, 2007 | Reply

  17. The setup was very well presented. I appreciate you taking the time to do it. I am having a problem getting phpmyadmin to work. When I type my server IP/phpmyadmin I get “NOT FOUND the requested URL /phpmyadmin was not found on this server Apache/2.2.4 (Ubuntu) Server at 192.168.1.xxx port 80″ Is there something not listed that I need to edit?

    Steve | Dec 27, 2007 | Reply

  18. @Steve - Are you able to view the default apache site when you type the server IP address into a browser? If so then log on to the server and open a browser and try navigating to localhost/phpmyadmin. If this doesn’t work, go back over the step 1 to make sure you didn’t miss something. Check that the phpmyadmin script is located in /var/www/ and make sure that the site’s configuration file in /etc/apache2/sites-available/ directory has an aliias pointing to the phpmyadmin script as per the directions in step 2.

    Sam | Dec 29, 2007 | Reply

  19. I could see the site I made and set up in /home/xxx/public_html. It seems that in Ubuntu 7.1, phpmyadmin is not installed to /var/www/. Instead, it is installed to /usr/share/phpmyadmin. I have changed my alias to match. This gave me a permission error. I got by that and now it gives me a file roller prompt because it doesn’t know how to handle a *.phtml. I haven’t found an answer to that problem yet. Any suggestions would be great! thanks in advance.

    Steve | Dec 30, 2007 | Reply

  20. I got it. I had to add symbolic links from /etc/apache2/mods-available/php5.conf php5.load in /etc/apache2/mods-enabled. Thanks again for a good setup.

    Steve | Dec 31, 2007 | Reply

  21. thanks for this tutorial - good job :-)

    optivex | Jan 8, 2008 | Reply

  22. @optivex - Thanks for the support.

    Sam | Jan 8, 2008 | Reply

  23. Great site. Have followed the instructions is no issues except the one with 7.10 putting the phpmyadmin in /usr/share/phpadmin. However, now I’ve run up against an issue trying to log onto phpmyadmin when trying to log in as root. I get an error #1045 - Access denied for user ‘root’@'localhost’ (using password: YES). I’ve tried the blank password, and the password I assigned to root before starting the appache/phpmyadmin install. Can’t get passed it. Yes, I’m a newbe.

    Irwin | Jan 13, 2008 | Reply

  24. NameVirtualHost *

    ServerName http://www.trial.com
    ServerAlias trial.com
    ServerAdmin webmaster@trial.com
    # DocumentRoot /home/railsfactory/trial/

    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    Allow from all

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/

    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all

    ProxyPass / balancer://railsapp/
    ProxyPassReverse / balancer://railsapp/

    ErrorLog /var/log/apache2/error.log
    # Log Level
    LogLevel warn
    CustomLog /var/log/apache2/access.log combined
    ServerSignature On

    Alias /doc/ “/usr/share/doc/”

    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
    Allow from 127.0.0.0/255.0.0.0 ::1/128

    the above is what i configured, but while browsing i have to type http://127.0.0.1:3000 but not http://127.0.0.1, why is that? can someone help me..

    Lingam | Jan 14, 2008 | Reply

  25. @Irwin - I am currently running into this problem too. It is a Ubuntu 7.10 issue since I didn’t have this problem with 7.04. If I figure it out I will post an update.

    Sam | Jan 14, 2008 | Reply

  26. I ended up having another issue with my setup that prompted me to start the whole install from the beginning, so I put the Ubuntu server CD in and booted up for a fresh install. This time when I came to the phpMyAdmin install, again thanks for the simple, clear, explanation of the steps, everything worked as it was supposed to.

    The only thing I can think of is that somewhere in the first install I must have “fat-fingered” something.

    Thanks again,

    irwin | Jan 22, 2008 | Reply

  27. Hey Sam,
    By far and the way the easiest tutorial pair I’ve found on the basics of installing and setting up a LAMP. It was extremely straight forward for me (with the exception of the installation detecting my keyboard wrong, leading to all sorts of weird effects.

    I might have made one change: instead of using Vi as the editor of choice, due to its complex way of editing files that’s slightly intimidating for the newly initiated, I would have used Nano which is more straight forward.

    Aside from that, you did a fantastic job!

    Thanks,
    Andrew

    Andrew | Feb 8, 2008 | Reply

  28. @Andrew - Thanks for the support. I agree; I should have indicated that vi is difficult for the newly initiated to use. Nano is much easier to master.

    Sam | Feb 14, 2008 | Reply

  29. I downloaded the server version ISO of Ubuntu 7.1 — 499 megs. The install went normally and everything seemd to configure as noted. I moved on to the next session of setting up Apache. When I looked for an apache2 folder in the /etc/ folder it is not there. Did apache get installed? If not, what do I need to do to install it? Newbe BTW….

    Thanks, Bob

    Bob | Feb 17, 2008 | Reply

  30. @Bob - It sounds to me like Apache is not installed. Try following this post to install Apache. You can also install apache from the command prompt using sudo aptitude install apache2 in the terminal.

    Sam | Feb 19, 2008 | Reply

  31. In my original install I did follow your Ubuntu Lamp setup guide. I decided to do a complete reinstall to see what I had missed. In your section that comments: “, and a plethora of file copying, the install shell will ask you to select optional software to install. Select LAMP (using the arrow keys and spacebar).” This selection step never appeared. I will try the Apache install you recommend but I wonder now what else is missing.

    Bob | Feb 19, 2008 | Reply

  32. hi sam,
    i am trying to follow trying to set an apache server.. and followed all the steps. Apache seems to work fine as i can run html files in my site directory using mozilla. But the problem is with cgi.
    CGI files are not working. i even changed the permissions to 777 for my site directory, cgi-bin directory and even for .cgi file
    Error log says

    [Wed Feb 20 20:41:55 2008] [error] [client 192.168.1.25] (8)Exec format error: exec of ‘/home/biflab/str_db/cgi-bin/abc.cgi’ failed
    [Wed Feb 20 20:41:55 2008] [error] [client 192.168.1.25] Premature end of script headers: abc.cgi

    Please help… thanks for this wonderful guide.

    sarvesh | Feb 20, 2008 | Reply

  33. @sarvesh - It sounds like you may have an error in the code of your cgi program.

    Sam | Feb 21, 2008 | Reply

  34. @Bob - You have me stumped. I have no idea why the install options are not made available to you. Are you performing the install using the graphical shell or the command prompt? All I can suggest is to try to get a fresh copy of Ubuntu and try again. Perhaps the install shell you are using is slightly different then the one I used to write the tutorial.

    Sam | Feb 21, 2008 | Reply

  35. Sam,

    Did another complete install and still did not see the LAMP server selection. When the first batch of files loaded and the disk ejected I ran sudo tasksel install lamp-server and finally got LAMP installed. Then followed your LAMP configuration guide. Appears to works fine using this method. Thanks for your help.

    Bob

    Bob | Feb 22, 2008 | Reply

  36. @Lingham - You have to go to port 3000 because that is the default port for Rails. In order to change that you have to go into the config directory of your application and look in the environment.rb or test.rb or development.rb or production.rb file for something like HTTP_PORT (or look for 3000 and change that to 80)

    0xcc | Feb 26, 2008 | Reply

  37. Man… let me tell you, that this is the first site that i came across the last 10 or something days (more like nights, cause i work in the day :) since i tryed to change me home network (4 boxes + 1 portable + 2 PS2 + 1 Relook400 + 1 Pinnacle Showtime + 1 Asus Wlan Wireless router with disk, all with need of internet access) to pure Linux using Ubunto. I traveled along many many forums, websites, blogs and others, but this is the first i found that i can really understand.
    Linux seems to be great, the opensource concept seems to be great, the quality and stability surprised me way over what i expected, but i was getting tired of reading too much techno crap wich is made (written) for people who already understand this OS. I on the other hand used windows for over 20 years (set up a few networks in the process, with XP and Window Server) considering myself with some network knowledge and a power computer user. So i was optimistic regarding the fact that i had to learn a new OS and all the new way of handling things, but was very quickly loosing all my initial enthusiasm and joy. You site gave me new courage to continue my quest for the free world. And i really mean this seriously. This is an awesome site and the way you write is greatly appreciated bye people like me i suppose and many many more.
    My really truly congratulations for this huge amount of information and work that you put online for all of us. I hope and wish you all the luck in the world.

    Registador | Mar 11, 2008 | Reply

  38. Really useful guide.

    Thanks for the help!!

    John Prado | Mar 18, 2008 | Reply

  39. @Registador and John Prado - Thanks for the support. I am glad that some people are getting a lot out of these tutorials.

    Sam | Mar 18, 2008 | Reply

  40. This guide is AMAZING! Taught me all the stuff I should have known and didnt make me look too far to learn it.

    Thanks!

    teqsun | Mar 21, 2008 | Reply

  41. Thanks for the invaluable information!

    Bill Ammon | Mar 28, 2008 | Reply

  42. thx for this useful guide,
    really helpful

    Tom | Mar 29, 2008 | Reply

  43. @Bill Ammon, Tom, and teqsun - Thanks for the support.

    Sam | Apr 3, 2008 | Reply

  44. hi everyone!

    I used to have a webserver running fine. But after upgrading to ubuntu 8.04 (from 7.10), my webserver is not functioning well. When i used http://127.0.0.1/ it displays “It Works”

    But then when I try http://localhost/, it will browse (in the status bar) http://www.localhost.com/ and returns an error like ’server cannot be contacted” or something like that.

    Please help me with this. What files should I edit and how would i edit them? I mean what changes should I make?

    Im using Ubuntu 8:04LTS

    Thank you.

    Best regards,
    Jess

    JessMagz | Apr 18, 2008 | Reply

  45. @JessMagz - It sounds like you have a problem with your /etc/hosts file. Open it up. there should be a line that reads 127.0.0.1 localhost. If there isn’t then add it. If there is an additional line that looks like ::1 localhost, comment it out. This line could be in the hosts file because you have the IPV6 network module installed. You may need to remove these. I hope this helps.

    Sam | Apr 18, 2008 | Reply

  46. Thank you sir! I edited /etc/hosts and followed your advice. I added the line “127.0.0.1 localhost” and now it’s working well. Thank you so much!

    JessMagz | Apr 18, 2008 | Reply

  47. Thank you so much for this fabulous walkthrough.

    Cheers mate

    Djodja | May 21, 2008 | Reply

  48. Hello,

    I have gone through a kazillion sites and cannot seem to resolve the issue I am having. Seeing how this is the most informative posting I have come across I will try my luck here.

    NO MATTER what I try and which userid I try it under I cannot get connected to the mysql database.
    I CAN connect using: mysql -u sa ~after which I can “show” but never edit.
    I need to change the root password, or at least add another account that I can setup a db to use for phpbb…but no method seems to work with mysql or mysqladmin.

    Any thoughts?

    Thanks, Bill

    SANweaver | May 30, 2008 | Reply

  49. @Bill-Have you installed Apache, MySQL and PHP as per this post? If not, then I would recommend a fresh install. It sounds like perhaps at some point a root user was created but the corresponding password was lost and this is preventing you from accessing MySQL. I don’t think I can be of much help other than suggesting a fresh start. Please reply to this comment if you solve the problem or discover more details that may help shed some light on the issue. Good luck.

    Sam | May 30, 2008 | Reply

  50. Dude this was the most amazing tutorial I have ever followed not one glitch was your fault. I am running this on 8.04 and I am eagerly waiting to see what else may have change but i haven’t had much errors. Thank you!

    Ergy | Jun 5, 2008 | Reply

  51. this guy really knows his shit.. this is an excellent, virtually flawless guide. using it, i was able to install LAMP on an older 486 box with no hitches …
    kudos to Sam

    caligula | Jun 22, 2008 | Reply

  52. Sam,

    A million thanks for putting this out there. This was a very clear path for this ubuntu newbie to follow.

    geostasis | Jun 27, 2008 | Reply

Post a Comment rss

Not sure if your comment is appropriate? View our comment policy here.

© Copyright Zaphu 2007. All rights reserved. -- Valid XHTML

Sitemap XML Sitemap XHTML