This post is designed for those of you who have some experience working on webpages, but may not have a lot of background with actually setting up the webserver itself. Basically, if you want to setup a quick webserver (perhaps on a spare computer for testing), this will get things started. This will not tell you how to install Ubuntu (okay, I lied: put in the CD, boot from it, double-click the install icon, BAM!) This tutorial makes use of the apt-get install X command (and may be useful for Debian and Debian-based Linux installs). As always, what works for me might give you the plague…

Most of these commands have to be input into the terminal as the “super user,” which means you will have to put sudo in front of a command if you don’t have proper permissions. In other words, instead of apt-get install NEWPACKAGE, you would run sudo apt-get install NEWPACKAGE. If you try any of these directions and have permission problems, it’s probably something that needs to be done as the super user. The terminal can be opened via the Accessories folder in the Application menu, via shortcut (System, Preferences, Keyboard Shortcuts), or you can switch to a terminal if you must (CTRL+ALT+F1-F6, F7 returns to the desktop). I personally setup a shortcut, because terminal is something you should be using a lot in Linux.

Okay, here it is, get ready… apt-get install apache2 php5 libapache2-mod-php5 Run that command (using sudo if necessary) and pretty much everything will be done for you. Your root web directory will be /var/www, which can be accessed via 127.0.0.1 (or “localhost”) in a web browser. You can also create a public_html folder in your home directory (type cd, mkdir public_html). Then you can access your username files with 127.0.0.1/~USERNAME (or “localhost/~USERNAME”). For detailed information about the install, create a file in one of the web directories with <?php phpinfo(); ?> as the contents.

If you name it index.php, it will automatically load when you visit that web directory, or you can name it whatever you want (adding a “php” extension so that it is parsed properly). Viewing this file in your browser will tell you a lot about your install. One of the most important things to know is the location of your php.ini file (it should be /etc/php5/apache2/php.ini, but might be different). If you open it, you can make any changes you would like (for instance, I maxed out the error reporting since it’s a test machine). You may also want to install specific packages. I do a lot of image manipulation, so I installed the GD library with apt-get install php5-gd and Linux did the rest. You may also want to install PEAR (php-pear) and others.

If you refresh your phpinfo page, you’ll notice that no info is listed about GD (and no php.ini changes show up). Simply restart the web server (/etc/init.d/apache2 restart) and refresh the phpinfo page.

That should be all you need to know to get started, but you may want to play around some. You may also need or want additional software, perhaps MySQL (apt-get install mysql-server mysql-client php5-mysql) and PHPMyAdmin (apt-get install phpmyadmin). Fortunately, the Internet is a vast resource. Put it to use!


18 Responses to “Tutorial: Setting Up An Ubuntu Web Server (Apache And PHP)”

  1. 1 Gordaen

    Did this tutorial suck? Try the less serious version.

  2. 2 d3NieD

    My Kubuntu is now a web server thanks to you !!! :0)
    Just gotta work out the config IF anything needs doing to it that is.

    Many thanks, great tutorial,

    d3NieD

  3. 3 Brennan

    Great Tutorial… Short and to the point…
    And it works…

    Thanks

  4. 4 Linda

    Your suggestion about creating public_html under my home directory does not work.
    When I try to access my index.html file in localhost/~USERNAME/ with USERNAME replace by my username, I get error message.

  5. 5 Ian Clifton

    Hi Linda, what error message are you seeing? I just checked with my desktop again, and this method still works for me. If you’re seeing an internal server error, then you may be looking at the wrong username location. Make sure that your casing is correct (e.g., “linda” is different from “Linda”).

  6. 6 Sam

    Hey I like your tutorial, I just got it working on Ubuntu 8.04. HOWEVER, I can’t access the default page the says “It works!” from another computer? Is there anything else I need to do? If this is supposed to be a web server then I should be able to view it on another computer right? I have NO experience with setting up web servers. Please help, How can I access my server from any computer?

  7. 7 Sam

    Never mind, I got it working! Thanks!

  8. 8 Vandrian

    http://127.0.0.1/ in Firefox displays a webpage saying “It works!” However if I try to make any kind of simple webpage it won’t let me put it in the var/www directory For examples:

    chi@chi-desktop:~$ mv test.html /var/www
    mv: cannot move `test.html’ to `/var/www/test.html’: Permission denied

    I just got Ubuntu yesterday so please give me a dumbed down answer. I am trying to get Zoneminder for work and I told my boss I would have it done this weekend (so stupid of me!!!)

  9. 9 Vandrian

    ok there is a chmod command… but I have rw permission on both the file and the directory?

    chi@chi-desktop:~$ ls -l test.html
    -rw-r–r– 1 chi chi 27783 2009-05-03 17:41 test.html
    chi@chi-desktop:~$ ls -l /var
    total 48

    drwxr-xr-x 2 root root 4096 2009-05-03 16:06 www
    chi@chi-desktop:~$

  10. 10 Sam

    Vandrian,

    The folder “www” is owned by root so you have to type “sudo” in front of “mv test.html /var/www”.
    So in your case it would be “sudo mv test.html /var/www”. You have to type “sudo” in front of every command dealing with the “www” folder. So you don’t have to type “sudo” every time, put this in the terminal: “cd /var/” then put “sudo chown -R ‘yourusername’ www”. For example, for me I put “sudo chown -R sam www”. For a lot more help on this watch this video. http://www.lullabot.com/node/289/play. I hope that helps.

  11. 11 Ian Clifton

    As Sam said, “www” is owned by root. The “drwxr-xr-x 2 root root 4096 2009-05-03 16:06 www” readout shows you permissions (d = directory, then read, write, and/or execute for owner, group, and all) as well as owner (the first “root” is the user who owns it, the second is the group). Another option is to create a symlink called “www” rather than a folder. You can have that link point to a directory in your home directory, which can make it easier to keep track of your files (plus backing up your home directory then also gets your site; in addition, you can simply change the symlink’s target to completely change the site being hosted).

    Thanks, Sam :)

  12. 12 Todd

    A STATIC IP would be a great idea too (for the server). If you use DHCP, the address of your server “could” change.

  13. 13 Jovan

    Umm im confused now how do i change the 127.0.0.1 to a custom domain. and also if i shut the computer down will the IT WORKS page not work??

  14. 14 John Ortiz

    Thanks for this useful information. I recommend you that enter the commands in one new line.

    So long.

  15. 15 chamberlain 7th

    i’ve setup UBUNTU 10.10 desktop version? what is the difference between desktop version and server version?

  16. 16 Marc

    Chamberlain 7th, the difference is no GUI (desktop) on the server version.

  1. 1 Planeta Debian » Less-than-serious Tutorial On Ubuntu Web Server Installation
  2. 2 Less-than-serious Tutorial On Ubuntu Web Server Installation at Gordaen’s Blog