Moving the localhost root to your working directory

Behind the curtain, your web server uses http to display files located on a machine. In our example, Apache is showing a file called index.html located in a subdirectory of our Apache installation. It would be better if we could work in whatever directory we please. (The file’s actually called index.html.en because of language switching, but let’s stick to index.html for now.)

Let’s create a directory and point our web server to the directory:

  • Create a directory called htdocs in your home directory, namely at /home/username/htdocs, where username stands for your user account’s real user name.
  • Open your Apache configuration file, located at /etc/apache2/sites-available/000-default.conf. Provide administrator credentials as needed. (If you aren’t using a recent version of Ubuntu or another Debian-based distro, try looking under /etc/apache2/httpd.conf or /etc/httpd/conf/httpd.conf.)
  • Within the file, find the following line: DocumentRoot /var/www/html. Change this line so that it points to your own htdocs directory: DocumentRoot /home/username/htdocs
  • At this point you should add some file access restrictions. Add the following lines under the DocumentRoot line, substituting your name for USERNAME:
<Directory />
                Options FollowSymLinks
                AllowOverride None
                Require all denied
        </Directory>
        <Directory /home/USERNAME/htdocs/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Require all granted
        </Directory>
  • Save the file.
  • Restart Apache with this terminal command: sudo apachectl restart. Provide your administrative password when prompted.
  • Reload localhost in your browser. If all went according to plan, you’ll see the directory’s index (as pictured below). By default the index is a list of all the files in the directory, but we’ll show you next how to customize the index page.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s