In this article, we will delve into the process of mapping a local directory to a localhost URL using Apache and PHP on Ubuntu. This is a useful technique for developers who want to test their PHP websites locally before deploying them to a live server.
Mapping a local directory to a localhost URL with Apache and PHP on Ubuntu involves creating a symbolic link from the desired directory to the Apache document root directory, copying and editing a virtual host configuration file, and reloading the Apache server. This allows developers to test their PHP websites locally before deploying them to a live server.
Before we begin, ensure that you have the following installed on your Ubuntu system:
If not, you can install them using the following commands:
sudo apt update
sudo apt install apache2 php
Step 1: Create a Symbolic Link
The first step involves creating a symbolic link from the directory you want to map to the Apache document root directory. This can be done using the
ln command with the
-s option, which stands for symbolic.
For instance, if you want to map the directory
/home/znorg/foobar to a localhost URL, you can run the following command:
sudo ln -s /home/znorg/foobar /var/www/html/foobar
This will create a symbolic link named “foobar” in the Apache document root directory (
Step 2: Copy the Default Virtual Host Configuration
Next, we need to create a new Apache virtual host configuration for the directory. This can be done by copying the default configuration file,
000-default.conf, located in the
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/foobar.conf
This command creates a new configuration file named “foobar.conf” based on the default virtual host configuration.
Step 3: Edit the New Virtual Host Configuration
Now, open the newly created “foobar.conf” file using a text editor of your choice. In this file, locate the line that specifies the document root:
/var/www/html with the path to your desired directory:
This tells Apache that the document root for this virtual host is the “foobar” directory.
Step 4: Enable the New Virtual Host Configuration
After editing the configuration file, we need to enable it using the
a2ensite command followed by the name of the configuration file:
sudo a2ensite foobar.conf
a2ensite command is a script that creates the necessary symbolic links within the Apache configuration to enable a site.
Step 5: Reload the Apache Server
Finally, for the changes to take effect, we need to reload the Apache server. This can be done using the
service command followed by
sudo service apache2 reload
After following these steps, you should be able to access your PHP website through the URL “http://localhost/foobar”.
Mapping a local directory to a localhost URL with Apache and PHP on Ubuntu is a straightforward process that involves creating a symbolic link, copying and editing a virtual host configuration, and reloading the Apache server. This technique is particularly useful for developers who want to test their PHP websites locally. Remember to replace “foobar” with the actual name of your directory when following the steps in this guide.
Apache is a popular open-source web server software that is widely used to serve web pages on the internet. It is known for its robustness, scalability, and flexibility.
PHP is a server-side scripting language that is commonly used for web development. It is embedded within HTML code and is executed on the server to generate dynamic web pages.
Mapping a local directory to a localhost URL allows you to test and develop your PHP websites locally before deploying them to a live server. It provides a convenient way to preview your website’s functionality and appearance.
A symbolic link, also known as a symlink or soft link, is a special type of file that acts as a pointer to another file or directory. It allows you to access files or directories located in different locations using a single path.
The Apache document root directory is the main directory where Apache looks for files to serve. In Ubuntu, the default document root directory is
/var/www/html. You can access it by navigating to
http://localhost in your web browser.
Yes, you can map multiple directories to different localhost URLs by creating separate symbolic links and virtual host configurations for each directory. Simply repeat the steps outlined in this guide for each directory you want to map.
Yes, you can use a different web server software instead of Apache if you prefer. However, the steps outlined in this guide specifically apply to Apache. The process may vary for other web server software.