HOW-TO: configure Ubuntu WebServer
This tutorial is meant to help users who want to use the Ubuntu version of Linux as a webserver. This is not for a novice, though the instructions are pretty straightforward.
This tutorial assumes that you have gone through the basic install of Ubuntu server and have configured it to be connected to the internet.
Initially simply log into the server to gain access. You should be presented with all of the basic Linux jargon about licenses, etc. First step is to update and upgrade APT (The Package Manager).
:~# sudo apt-get update
:~# sudo apt-get upgrade:~# sudo apt-get install emacs23
The last item in that list is to install EMACS, a command line editor to edit various files. Very Handy.
That’s the basics, now on to the fun stuff;
1: Installing Apache + PHP
Apache is one of the most famous web servers which runs on most Linux based servers. With just a few commands you can configure apache to run with PHP5.
:~# apt-get install apache2 php5 libapache2-mod-php5
Now to test apache. The basic configuration file is now located at /etc/apache2/apache2.conf and your default web folder is /var/www, though we will change that later.
To check whether PHP5 and Apache are running correctly. In order to do so, we are going to create a test PHP file within the current web folder.
:~# sudo emacs /var/www/test.php
Type the following into the new file that comes up.
<?php phpinfo(); ?>
Point your browser at the server: http://*server.ip*/test.php. You should see all of the current settings and php configuration information.
2: Installing MySQL Database Server
Installing the MySQL database server is always necessary if you are running a website with any database driven content. For instalce, a shopping care, forum or blogging platform. The following commands will install MySQL5 and the MySQL5 client.
:~# apt-get install mysql-server mysql-client php5-mysql
The configuration file of MySQL is located at: /etc/mysql/my.cnf
3: Creating users to use MySQL and Changing Root Password
By default on my installation of MySQL on 2/10/2010, after invoking the install command prompted me to create a password for the root user. If your installation does not prompt you, then use the commands below to change/create that password.
:~# mysql -u root
mysql> USE mysql;
mysql> UPDATE user SET Password=PASSWORD(‘new-password’) WHERE user = ‘root’;
mysql> FLUSH PRIVILEGES;
It is never a good idea to use your root user in any applications. You really need to create individual users and individual databases for those users in order to keep you server secure.
Congratulations you have now just created a webserver in only 3 steps!
4: Configuring an Apache2 site
By default apache2 uses /var/www as it’s home directory. I generally don’t like doing that, I like having a good folder off the root, in this case /www. To create that folder, type the following;
:~# sudo mkdir /www
Now we want to create some site folders. Let’s assume that you have a domain and can DNS a few domain’s or domain aliases. In this case we are using http://web03.mydomain.com and http://tools.web03.mydomain.com. We want each domain to have their own web site folders and log folders. Type the following to accomplish this:
:~# sudo mkdir /www/web03.mydomain.com
:~# sudo mkdir /www/web03.mydomain.com/wwwroot
:~# sudo mkdir /www/web03.mydomain.com/logs:~# sudo mkdir /www/tools.web03.mydomain.com
:~# sudo mkdir /www/tools.web03.mydomain.com/wwwroot
:~# sudo mkdir /www/tools.web03.mydomain.com/logs
Ok, so now we have the folders, let’s go point some Apache2 config files to them. Type the following to get to the Apache2 configuration folder:
:~# cd /etc/apache2/sites-available
Now there are 2 files currently in this folder, “default” and “default-ssl”. What we really want to do is work with “default” as it is the basic incarnation. So we need to make unique copies of these for our 2 new domains, to do that type the following:
:~# sudo cp default web03.mydomain.com.conf
:~# sudo cp default tools.web03.mydomain.com.conf
Notice the inclusion of the “.conf” at the end of the document. This helps Apache realize that it is in face a config file. Now we need to edit them, so bring on EMACS:
:~# sudo web03.mydomain.com.conf
Now you will see a bunch of information that is the configuration file for that site. I will highlight and give instructions on what you need to change.VirtualHost *:80>
<VirtualHost *:80>
ServerAdmin webmaster@mydomain.comServerName web03.mydomain.com
DocumentRoot /www/web03.mydomain.com/wwwroot/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /www/web03.mydomain.com/wwwroot/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory “/usr/lib/cgi-bin”>
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>ErrorLog /www/web03.mydomain.com/logs/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warnCustomLog /www/web03.mydomain.com/logs/access.log combined
Alias /doc/ “/usr/share/doc/”
<Directory “/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
</Directory></VirtualHost>
- RED = simply change to your domain.
- BLUE = change these to the new folders you made under /www. See the examples above
- GREEN = you can remove this entirely if you wish. It simply maps the Apache Documentation to http://www.yourdomain.com/doc/ if you wish you read it.
When you are done with the editing of the file, go ahead and save it and let’s move on.
There are 2 folders in the /etc/apache2/ folder that we need to be concerned with. Those are “sites-available” and “sites-enabled”. The reason there are 2 is that you put all of the configs into the “sites-available”, but you only “symbolically link” the configs you want running inside the “sites-enabled”.
That being said, we now have to make a “symbolic link” for the new config we just made. I have found it easiest to make the links from inside the folder that you want the link to be, so change directories to the “sites-enabled” folder. Then create the link;
:~# sudo ln -s ../sites-available/web03.mydomain.com.conf web03.mydomain.com.conf
:~# sudo rm 000-default
That last line removes the original default entry from the Apache install so it doesn’t cause any problems.
Now, simply restart Apache and you should be all good.
:~# cd /etc/init.d
:~# sudo ./apache2 restart
TADA! All done, check your new domain and if you have done this right, you should be all good. These instructions may not work for everyone, but it’s a pretty good example for ubuntu which I have been using religiously as of late.
5: Further Study/Install
- Install PHP MCrypt extension
- Install Apache’s mod_rewrite
More to come!















