I am writing this post as part of Tero Karvinen’s course: Linux palvelimena (roughly translated: Linux as a server) http://terokarvinen.com/2012/aikataulu-linux-palvelimena-ict4tn003-4-ja-ict4tn003-6-syksylla-2012.
On this post I will attempt to go through the following:
- Installing & configuring apache 2 web server
- Run 3 virtual servers with different settings
- php enabled
- php NOT enabled
- password authentication (see apache2-doc)
- Produce the following http-statuses in the apache log
- 200
- 404
- 403
- 500
- 304 (a bonus)
- Analyze the log
I am using 32-bit xubuntu live-environment
1. Installing & configuring apache 2 web server
I first started by installing apache 2 server on my computer
$ sudo apt-get update
$ sudo apt-get install apache2
I opened firefox and tested that apache 2 works correctly by writing localhost in the address bar.
I enabled users to host files over the internet:
$ sudo a2enmod userdir
and restarted the server to apply the changes
$ sudo service apache2 restart
I created a folder under home called public_html
$ mkdir public_html
I searcher through the apache2-doc to determine the third website
$ sudo apt-get install apache2-doc
$ cd /usr/share/doc/apache2-doc/manual
$ firefox index.html
By looking at the available modules I picked authentication
I created 3 separate folders for each virtual server
$ cd
$ cd public_html/
$ mkdir php nophp authentication
1.1 php supported website
I started by creating a simple php file in the appropriate folder
$ cd php/
$ nano index.php
03 | < title >This website supports php!</ title > |
07 | print "this is php, 1+2 is ".(1+2); |
At the moment the php file won’t show.
to get php working I did the following:
$ sudo apt-get install libapache2-mod-php5
$ cd /etc/apache2/mods-enabled/
$ sudoedit php5.conf
the terminal on the left shows the original file and the one on the right shows how it must be modified to enable php.
$ sudo service apache2 restart
$ firefox http://localhost/~xubuntu/php/
1.2 php unsupported website
I copy the php file from the php -folder to nophp -folder and move inside the nophp -folder.
$ cd
$ cd public_html/
$ cp php/index.php nophp/
$ cd nophp/
To disable php from the nophp -folder I did the following:
I moved to the folder where apache stores the settings for virtual websites.
$ cd /etc/apache2/sites-available/
and edited the default -file
$ sudoedit default
This is how the relevant part of the file looks like after the changes (I added the third directory part with the “address” home/xubuntu… The lines 15-20 from the snippet of code below)
02 | ServerAdmin webmaster@localhost |
06 | Options FollowSymLinks |
10 | Options Indexes FollowSymLinks MultiViews |
15 | < Directory /home/xubuntu/public_html/nophp/> |
16 | < FilesMatch "\.(php5?|phtml)$"> |
source for the disabling of php: http://stackoverflow.com/questions/1271899/disable-php-in-directory-including-all-sub-directories-with-htaccess by Lance Rushing.
$ sudo service apache2 restart
Now when I open the address http://localhost/~xubuntu/nophp/ in firefox I get the forbidden error.
Optional but I decided to add a new index file with only html in it.
$ cd
$ cd public_html/nophp/
$ nano index.html
3 | < title >This website does not support php</ title > |
Now after reloading the webpage we don’t get an error anymore.
1.3 password authenticated website
Next up password protection!
$ cd
$ cd public_html/authentication/
I created a simple html file to add some content
$ nano index.html
3 | < title >Very important user</ title > |
I created a password for the “website” (username=”xubuntu”)
$ htpasswd -c .htpasswd xubuntu
(-c = create. makes a new file, if a file exists it is overwritten)
I made some changes to apache’s default -file again (lines 7-14)
$ sudoedit /etc/apache2/sites-available/default
01 | < Directory /home/xubuntu/public_html/nophp/> |
02 | < FilesMatch "\.(php5?|phtml)$"> |
07 | < Directory /home/xubuntu/public_html/authentication/> |
08 | AllowOverride AuthConfig |
09 | AuthName "Please insert your login data." |
11 | AuthUserFile /home/xubuntu/public_html/authentication/.htpasswd |
12 | AuthGroupFile /dev/null |
source for the creation of the password file and default -file modification: http://www.yolinux.com/TUTORIALS/LinuxTutorialApacheAddingLoginSiteProtection.html search for “using the program htpasswd” (first of the two hits) and “WITHOUT using the .htaccess file.”
$ sudo service apache2 restart
1.4 Adding names for the virtual websites
It is possible to add a domain name locally (only seen on this computer) so next I will be giving a name to the websites I created and test them.
I checked my ip with ifconfig -command (eth0 -> inet addr) and opened the hosts -file where I can map hostnames to IP addresses.
$ ifconfig
$ sudoedit /etc/hosts
the modified file: (I added the six lines starting with “192.”)
127.0.0.1 localhost
127.0.1.1 xubuntu
192.168.0.100 ilovephp.com
192.168.0.100 http://www.ilovephp.com
192.168.0.100 ihatephp.com
192.168.0.100 http://www.ihatephp.com
192.168.0.100 auth.com
192.168.0.100 http://www.auth.com
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
I added the information of the websites under /etc/apache2/sites-available/
$ cd /etc/apache2/sites-available/
$ sudoedit ilovephp.com
2 | ServerName ilovephp.com |
3 | ServerAlias www.ilovephp.com |
4 | DocumentRoot /home/xubuntu/public_html/php |
$ sudoedit ihatephp.com
2 | ServerName ihatephp.com |
3 | ServerAlias www.ihatephp.com |
4 | DocumentRoot /home/xubuntu/public_html/nophp |
$ sudoedit auth.com
3 | ServerAlias www.auth.com |
4 | DocumentRoot /home/xubuntu/public_html/authentication/ |
Next I enabled the sites:
$ sudo a2ensite ilovephp.com
$ sudo a2ensite ihatephp.com
$ sudo a2ensite auth.com
and I reloaded the server to apply the changes (restart not required this time)
$ sudo service apache2 reload
and now I can use the new addresses to access the virtual websites. (You might need to delete your cookies and/or cache if it doesn’t seem to work)
2. Creating http errors & checking the log
The log containing the http errors or status codes is in the other_vhosts_access.log where I’ll be going now:
$ cd
$ cd /var/log/apache2/
I can see the last 10 lines of a file by using tail
$ tail other_vhosts_access.log
the first status code was 200 which is when nothing is wrong and the site loads normally.
I opened http://ilovephp.com/ with firefox and checked the log
$ tail other_vhosts_access.log
ilovephp.com:80 192.168.0.100 - - [30/Sep/2012:09:37:58 +0000] "GET / HTTP/1.1" 200 380 "-" "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:14.0) Gecko/20100101 Firefox/14.0.1"
You can see the status code “200″ after the date: “GET / HTTP/1.1″ 200 380 “…
next up 404 (file not found):
http://ilovephp.com/thiswillproducea404
$ tail other_vhosts_access.log
ilovephp.com:80 192.168.0.100 - - [30/Sep/2012:09:40:33 +0000] "GET /thiswillproducea404 HTTP/1.1" 404 506 "-" "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:14.0) Gecko/20100101 Firefox/14.0.1
At this point I realized that my settings in nophp and authentication don’t seem to work anymore. php isn’t blocked and the password isn’t asked. Something to do with part 1.4? I will try to find out the reason.