Apache tutorial - how to setup a virtual host for php's projects separation
Published: 2023-03-16 14:27:29

Hello guys!

Usually a developer is engaged in more projects at once, so for standard developer is pretty simple: they compile and run every application (usually from the IDE) like standalone "entity". How can php web developer had this separation between different projects ?

Forget about imaginative solutions, the right way to achieve this is using Apache's virtual hosts!!!

A virtual host allows to create a virtual server where the server root is the path given... and much much more!

In this tutorial i will cover the basic usage of the virtual host and at the end we will access our projects from different entry point. Let's start!

##Clarification
This is the way i use it in production mode, so i put every host on different port and access it through **_localhost_**. On windows you can modify your **_host_** file and access to the server with the server name.

###1. Requirements

- An installation of Apache ( I work on Windows so i use XAMPP).
- A text editor

###2. Listening the ports
The first step is to setup apache for listening on the desidered port, which will be the port that allow us to access the virtual host.

To achieve this let's edit the **_httpd.conf_** file.
In my case (XAMPP on Windows) i can open it from the XAMPP control panel or i can go in the installation folder of XAMMP and follow this path:

**__apache\conf\httpd.conf__**

Now is pretty simple, under the other __Listen__ we add another (i usually find with search text the __Listen__ word) with our port:


![httpd screen](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0rqv11qk7tz97gz473wj.png)

###3. Setting up the virtual host
The last point of this small tutorial! We have to move in another apache folder, and edit the **_httpd-vhost.conf_** file.

Usually this file is in this path:

**_apache\conf\extra\httpd-vhost.conf_**

So open this file, suppose you want to use port 7005, and append these lines  : 

```
NameVirtualHost *:7005

<VirtualHost *:7005>
    DocumentRoot "C:/xampp/htdocs/IDEWorkspace/someOtherFolders/Project1RootFolder"
    ServerName project1_server_name
      <Directory "C:/xampp/htdocs/IDEWorkspace/someOtherFolders/Project1RootFolder">
        DirectoryIndex index.php
        Require all granted
        AllowOverride All
    </Directory>
     ##SSLEngine on
     ##SSLCertificateFile "crt/localhost/server.crt"
     ##SSLCertificateKeyFile "crt/localhost/server.key"
</VirtualHost>
```

Some of these lines (like _DirectoryIndex index.php_) are optional.
I included (in form of comments) the lines for add the SSL certificate, this will enable the HTTPS protocol.

After this, restart apache!

###4. Testing!!
Now we have a virtual server called _project1_server_name_ where his root is _C:/xampp/htdocs/IDEWorkspace/someOtherFolders/Project1RootFolder_ and listening on the _port 7005_. This means that access to _localhost:7005_ will access to the root of the server.

We can test this writing a simple php script and make a get request using Postman:

I simply put an echo inside index.php in the root folder of the project

```
echo "Virtual hosts working!!";
```

and make an HTTP GET from postman to *_http://localhost:7005_* URL:

![Postman GET Test](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8mlo9j6cmuybg64ksvzm.png)

Perfect!! All is working, so enjoy with your new virtual hosts!!!

Hoping this will help you, dont' forget to follow me!!