Tutorial – How to Set Up a LAMP Webserver in Ubuntu10.10

I’ve read guides galore, and none of them were straightforward enough to just tell me what to do. I decided to make one here. I felt it unfair that I had to have so much difficulty when Ubuntu is something I want to be easy for noobs.

Set up LAMP

I got a lot of my instruction from this page, but I’ll sift through the crap and give you the goods here.

Open a terminal (CTRL+ALT+T).

Type this:

1
sudo apt-get install lamp-server^

That’s not a typo… it does need that caret(^) at the end

It’ll prompt you to change password for your MySQL database. Type in a password, and keep going. The installation wizard should complete nicely. Close the terminal when you’re done.

Time to test it. Open a browser, and type in http://localhost/

You should get a page that says “IT WORKS!”

Getting a website to work

Where can I get a domain? And what the hell is a domain?

A domain is what you type instead of an ip address to get to a webpage. Try it with my site. Open command prompt, and type “ping hylianux.com”, and you’ll see what my ip address is.

OMG you just typed in your ip address! security fault security fault! Ah calm down… you can get the ip address of any website by opening a command prompt and pinging the domain. Just because you know my ip address doesn’t mean you know how to hack it. Besides, it’s an empty ubuntu installation with my site running on it and I have everything backed up.

I bought my domain at godaddy.com, but I’d recommend getting a free one at DynDNS. Why would I tell you to go someplace different than where I got it done? Because I’ll explain later what to do if you want to buy a domain. For now, let’s just go with a free one.

Alright. On dyndns.com, log in, and go to your domain. Where it says ip address, type in your ip address. Don’t know what your ip address is? Go here to get it. Do not use ifconfig/ipconfig to get your ip address. All that’s going to do is tell you what ip you have that’s been assigned by your router or modem that your ISP installed. (If your ip address starts with 192.168, that’s not your ip address).

Now that we have a domain that points to our ip address, we’ll need our server to point that to the right place.

Back into Ubuntu.

I’m pretty sure you don’t want to have to open your directory for your files as an administrator all the time, so let’s create a directory in your home folder somewhere. In my home folder, I have a folder called “dyndns”. Inside THAT folder is a file called index.html. You can call your folder whatever you want, but you have to have an index.html (or index.php) file in there. Create one. Put some kind of content in there that makes you SURE that it’s going to the right place. Try this code:

1
‹h1›Technobabble told me to!‹/h1›.

All we’re really testing right now is whether the site will work. You can add REAL content in later.

CTRL+ALT+T to open a new terminal. We need to get into the “sites” directory of apache. Type this in the terminal:

1
sudo nautilus /etc/apache2/sites-available

You’ll see a file there called “default”. Copy that file and paste it right there in the same directory. It’ll be called default(2). Rename it to whatever you want (in my case, I renamed it to dyndns). Now open it.

It’ll look something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
‹VirtualHost *:80›
ServerAdmin webmaster@localhost
DocumentRoot /var/www
‹Directory /›
Options FollowSymLinks
AllowOverride None
‹/Directory›
‹Directory /var/www/›
Options Indexes FollowSymLinks MultiViews +Includes
AllowOverride all
Order allow,deny
allow from all
‹/Directory›
...
‹/VirtualHost›

We’re going to change it.

First, we need a server name. The server name is the domain it’s looking for. I got the domain hylianux.dyndns.org, so that’s what my server name will be. Under the ServerAdmin line, add ServerName [yourdomainname]. In my case, it would be: ServerName hylianux.dyndns.org

Next, you’ll notice something that says DocumentRoot /var/www and ‹Directory /var/www/› you’ll want to change these to the directory you just created. (note in the ‹Directory› tag the directory has that slash on the end, unlike the DocumentRoot line).

Mine say this:

1
2
3
DocumentRoot /home/hylianux/dyndns
...
‹Directory /home/hylianux/dyndns/›

Again, make sure that last one has the / at the end.

Here’s what my file looks like now after those changes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
‹VirtualHost *:80›
ServerAdmin webmaster@localhost
ServerName hylianux.dyndns.org
DocumentRoot /home/hylianux/dyndns
‹Directory /›
Options FollowSymLinks
AllowOverride None
‹/Directory›
‹Directory /home/hylianux/dyndns/›
Options Indexes FollowSymLinks MultiViews +Includes
AllowOverride all
Order allow,deny
allow from all
‹/Directory›
...
‹/VirtualHost›

Save the file, and go back to the terminal.

We’re going to enable this site now with the a2ensite command (which means ‘apache2 enable site’). Just type ‘sudo a2ensite [name of file you just created and edited]’

So in my case:

1
sudo a2ensite dyndns

Now you have to restart the server. Every time you change the server config like this, you have to restart it. Here’s the command:

1
sudo /etc/init.d/apache2 restart

Tired of typing sudo /etc/init.d/apache2 restart every time? Check below to learn how to create your own command name to do that so you don’t have to remember that long-ass command. Update: If you really want to learn how to alias commands, you can click the spoiler. I’ve since learned that both debian and ubuntu have a “service” command for this. It works like this:

1
sudo service YourServiceName Action

So, for example, instead of:

1
sudo /etc/init.d/apache2 restart

You should instead use:

1
sudo service apache2 restart

Alright, now for the big moment… the moment of truth. Type your domain into your browser, and cross your fingers.

Congratulations!

All your content is in that folder you created, so if you want to add more, that’s where it goes.

Now, I said I’d talk to you a bit about buying your own domain. Personally, I find godaddy’s prices cheaper, but that doesn’t mean dyndns won’t have special deals too at some point. Check with both. In any case, let me explain how to make this work with godaddy. My personal preference now would be to choose no-ip, but the point of this guide is ease of use.

First and foremost: **do not forwards your domain to your ip address&&! There’s a nice little feature on there that will let you forward your domain to whatever you want, and you’ll think “oh, I just type in my ip address and I’m golden!” Then you wonder why every time you type in your domain, your ip address displays at the top of the page. This is what forwarding actually does: when you type in your domain, it redirects you to a place hard-coded by you. You may as well type in the ip address yourself each time, because effectively that’s what you’re doing. And don’t believe the “masking” thing either. All that does is put the content of the page in a big iframe. This is not what you want.

I spent 3 nights pulling my hair out because of this, and I felt like such an idiot after I figured out what I was REALLY supposed to do. On godaddy, log in, and click My Account. On your domain, click advanced details right beneath it. Under the DNS Manager category, click the “Launch” link beneath it.

This brings up a “zone file editor”. Under A (Host), you’ll see an entry for @ and some ip address that it points to. I didn’t get the hint until I read the fine print when you hover over the ? right next to the category:
“A records (also known as host records) simply point a domain name or sub domain to an IP address.”

Well now it should seem pretty obvious what to do. You need to fill in the @ entry with your ip address. You might also want to add another entry for * and point it to your ip address as well. This is so all subdomains point to the same ip address.

Save your changes, and wait about an hour. I’m not sure what godaddy’s deal is, but they take anywhere between 30 minutes to an hour before you can see your changes actually happening. So get up, go make some coffee or tea, watch an episode of family guy, or whatever. Your domain should now point to the right place.

I just bought a different domain… what do I change on my server to make it go to the right place?!

Remember that file you created in the apache sites directory (I called mine dyndns)? Edit the ServerName to be your new domain, save it, and restart your server (if you aliased it, this last part is REALLY easy now :) ).

I think that’s all you need to know about making a webserver to host your website. Have fun, and enjoy web designing!