DEV Community

gpisano97
gpisano97

Posted on

Apache tutorial - how to setup a virtual host for php's projects separation

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

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>
Enter fullscreen mode Exit fullscreen mode

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!!";
Enter fullscreen mode Exit fullscreen mode

and make an HTTP GET from postman to http://localhost:7005 URL:

Postman GET Test

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

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

Top comments (0)