Basic Apache2 How-To
This is a basic tutorial for starting an apache2 server on Ubuntu/Linux.
Start by installing apache2 using apt. $ sudo apt-get install apache2 The installation process will achieve a basic working apache2 server on your operating system. The configuration files will be found in the directory /etc/apache2. It is easy to access and modify these files using the nautilus file browser in administrative mode: $ gksu nautilus
Browse to the file, /etc/apache2/httpd.conf and add two lines:
ServerName localhost
ServerName <your_ip_address>
Your public ip address can be seen easily at www.whatismyip.com You can later change this number to a domain name by registering a domain with any dns service like, DYNDNS.org
Save the changes to httpd.conf and restart apache2 from the terminal: $ sudo /etc/init.d/apache2 restart or $ sudo apache2ctl restart
Anytime changes are made to apache config files, the server must be restarted for the changes to take effect.
By browsing (with nautilus) to the /etc/apache2/sites-available/default file, you can see the default configuration apache2 uses to direct web browsers to specific folders and files hosted on your computer.
Right click in the folder /etc/apache2/sites-available and select ‘create a document.’ Copy and paste the contents of the default file into this new document and choose a name for the file, perhaps ‘mysite.’ This file will now look just like the default file:
NameVirtualHost *
<VirtualHost *>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
The Document Root /var/www is where the default index.html is hosted. You will not want to host webpages and files from a root directory. It is better to do this from your home directory, and later I will talk about creating a unique, unprivileged owner for the files. For now, lets disable the default apache2 site and enable /mysite. In the terminal run: $ sudo a2dissite default && sudo a2ensite mysite (the page you see, as a result of typing localhost into the address bar of your browser, is generated by the file called ‘index.html’ in the directory /var/www.).
Let’s prepare /mysite to point to an index.html page in your /home directory. Use the file browser (not gksu nautilus) to open your home directory. Just navigate to Places>>Home Folder and create a folder called ‘my_www‘ or any name you like. For the rest of this tutorial, I will assume /my_www was used, and you may substitute another folder name, as appropriate. Inside this folder, apache2 will look for an index.html, by default. So using any text editor, create a simple .html page called ‘index.html’. If there is no index.html, apache can be told to index whatever files are in another folder in the /home/your_username/my_www folder. Let’s assume you have not created an .html page and you just want to host some files others can browse to and download. Create another folder called ‘my_files‘ inside the /my_www folder and put whatever files you choose into the /my_files folder. Now open /etc/apache2/sites-available/mysite with gksu nautilus again. It is necessary to edit this file as an administrator because it is a root owned document. You could also use a command line text editor of your liking, for example, $ sudo nano /etc/apache2/sites-available/mysite.
In the Document Root section change /var/www to point to your Document Root. In other words:
NameVirtualHost *
<VirtualHost *>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/
<Directory />
Becomes:
NameVirtualHost *
<VirtualHost *>
ServerAdmin webmaster@localhost
DocumentRoot /home/your_username/my_www/
<Directory />
The section below the Document Root is called a directory. The first directory will either point to your /my_www/my_files folder with indexes specified in the ‘Options’ for the directory, or to your index.html. As we are using the former, in this example, the directory section will look like this:
<Directory /home/your_username/my_www/my_files/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
If you had an index.html, the directory would simply point to /home/your_username/my_www/ and apache2 would find the index.html. The index.html can contain links to point to other folders. In this case, you would want to eliminate the option ‘indexes’ from the directory.
Restart apache2. You can now browse the files by typing localhost into the address bar of your web browser. To browse your files from the world wide web, it will be necessary to set your router up to forward http requests to the machine running apache, your computer. Run: $ ifconfig and make a note of your local ip address under the primary interface, eth0, for example. Look for: inet addr. 192.145.15.102
Next, obtain the address of your router by running the command: $ netstat -r
Look in the row labeled ‘default’ under the column ‘gateway.’ If your local address were the one above, 192.145.15.102, the default gateway might be: 192.145.15.1
Browse to this address by typing it into the address bar of your web browser. You will likely need a user name and password to gain access. See the following site for a list of default credentials, by brand, and be sure to change the default credentials when you are done. http://www.phenoelit-us.org/dpl/dpl.html
As an example, the screenshot shows port 80 being opened to forward requests to a machine with an ip address ending in .102. Apache2 uses port 80 by default, as specified in /etc/apache2/ports.conf.

Once the router setup is complete, you should be able to browse your files by entering your public ip address into the address bar of your web browser. Your public ip being the one entered in /etc/apache2/httpd.conf, obtained from www.whatismyip.com.
This has been merely a basic guide to getting apache running. There are numerous other tutorials available. There is also a great deal of information available for securing apache, establishing user authentication, and accessing apache via secure socket layer. I will try to follow up with some simple tips for securing apache for basic usage. This is not intended to be alarming, as apache is by default fairly secure, as should your root file system be, through the use of a strong password. However, basic security is everyones business, and there is no reason not to take a few simple steps to protect your file system from malicious tampering.
Ok. This has been a long time coming, but the following is a guide to adding basic user authentication to your apache server. This means anyone attempting to access your web server will need to provide a valid username and password to gain access.
Let’s start by creating the necessary file .htpasswords in your home directory. The following command will create the file and a user.
htpasswd -c ~/.htpasswords some_name In this example the username would be “some_name.” Obviously you can substitute any name you choose. Remember the username will be case sensitive. You will then be prompted to enter and re-enter a password. The “-c” flag in the htpassword command is only used the first time the command is run. It tells the program htpassword to create the file .htpasswords. As a note, we could have named the file something other than “.htpasswords.”
The next step is to tell apache2 to use basic authentication and to tell apache2 where to find the file containing the users and encrypted passwords. This is done in the virtual host file created above. We called the file my_site So edit the file /etc/apache2/sites-available/my_site Use the command sudo nano /etc/apache2/sites-available/my_site
Create a directory container directly beneath your DocumentRoot like so:
DocumentRoot /home/your_username/my_www/
<Directory />
AuthType Basic
AuthName “Restricted Files”
AuthUserFile /home/your_user_name/.htpasswords
Require valid-user
Options FollowSymLinks
AllowOverride None
</Directory>
In this example, “your_user_name” gets changed to reflect your actual username for the home directory where you created the .htpasswords file.
You should now restart your webserver. Then try to browse to your web site. You should be asked for credentials, and those credentials must correspond to users and passwords created with the htpassword command. To create additional users just run the command again without the -c. So: htpasswd ~/.htpasswords some_name
To delete a user, simply go into the file with your file browser View>>Show Hidden Files, and delete the user and encrypted password you wish to remove. I find the permissions 644 to be adequate for this file. For some reason I often see 755 recomended.