-2

Problem

I need to properly configure my httpd.conf on apache2 server to change it from one project (http://localhost/) to three projects. Would you be so kind and check the following codes and descriptions and help me to make a correct reconfiguration?

Current one project directory model:

Library
+++ WebServer

+++ +++ Documents
+++ +++ database

Desired three-project directory model:

Library
+++ WebServer

+++ +++ project_1
+++ +++ +++ public_html
+++ +++ +++ database

+++ +++ project_2
+++ +++ +++ public_html
+++ +++ +++ database

+++ +++ project_3
+++ +++ +++ public_html
+++ +++ +++ database

Accessibility

One project:

Three projects:

Relevant portion of httpd.conf for one project (currently works for one project):

DocumentRoot "/Library/WebServer/Documents"

<Directory "/Library/WebServer/Documents">
    Options Indexes FollowSymLinks MultiViews
    MultiviewsMatch Any
    AllowOverride All
    Require all granted
</Directory>

Suggested portion of httpd.conf for multiple projects:

<VirtualHost>
   ServerAdmin localhost
    DocumentRoot "/Library/WebServer/project_1/public_html"
    ServerName localhost
</VirtualHost>  

<Directory "/Library/WebServer/project_1/public_html">
    Options Indexes FollowSymLinks MultiViews
    MultiviewsMatch Any
    AllowOverride All
    Require all granted
</Directory>


<VirtualHost>
   ServerAdmin localhost
    DocumentRoot "/Library/WebServer/project_2/public_html"
    ServerName localhost
</VirtualHost>  

<Directory "/Library/WebServer/project_2/public_html">
    Options Indexes FollowSymLinks MultiViews
    MultiviewsMatch Any
    AllowOverride All
    Require all granted
</Directory>


<VirtualHost>
   ServerAdmin localhost
    DocumentRoot "/Library/WebServer/project_3/public_html"
    ServerName localhost
</VirtualHost>  

<Directory "/Library/WebServer/project_3/public_html">
    Options Indexes FollowSymLinks MultiViews
    MultiviewsMatch Any
    AllowOverride All
    Require all granted
</Directory>

Server version: Apache/2.4.28 (Unix)

Emma
  • 121
  • 1
  • 1
  • 8

1 Answers1

1

Because the part of the url path does not match the one with the library structure, so you have to trick it.

Trick 1: Virtualhost and local-only domain per project.

Trick 2: Use aliases within a virtual host (or don't use VirtualHost)

Trick 1 example:

<VirtualHost *:80>
   ServerAdmin localhost
    DocumentRoot "/Library/WebServer/project_1/public_html"
    ServerName project1.emma

    <Directory "/Library/WebServer/project_1/public_html">
        Options Indexes FollowSymLinks MultiViews
        MultiviewsMatch Any
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost> 

<VirtualHost *:80>
   ServerAdmin localhost
    DocumentRoot "/Library/WebServer/project_2/public_html"
    ServerName project2.emma

    <Directory "/Library/WebServer/project_2/public_html">
        Options Indexes FollowSymLinks MultiViews
        MultiviewsMatch Any
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

<VirtualHost *:80>
   ServerAdmin localhost
    DocumentRoot "/Library/WebServer/project_3/public_html"
    ServerName project3.emma

    <Directory "/Library/WebServer/project_3/public_html">
        Options Indexes FollowSymLinks MultiViews
        MultiviewsMatch Any
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

ServerName is important, so you can access the project in your browser with ServerName: http://project1.emma. For this to work, you should add the following line in /etc/hosts file:

127.0.0.1 project1.emma project2.emma project3.emma

ServerName can be anything. Tld can be anything. The important thing is to be in the hosts file and when you type in your browser, always use a protocol prefix (http: //)

Trick 2 example:

You do not need VirtualHost to do this.

Alias /project1 /Library/WebServer/project_1/public_html
Alias /project2 /Library/WebServer/project_2/public_html
Alias /project3 /Library/WebServer/project_3/public_html

<Directory "/Library/WebServer/project_1/public_html">
    Options Indexes FollowSymLinks MultiViews
    MultiviewsMatch Any
    AllowOverride All
    Require all granted
</Directory>

<Directory "/Library/WebServer/project_2/public_html">
    Options Indexes FollowSymLinks MultiViews
    MultiviewsMatch Any
    AllowOverride All
    Require all granted
</Directory>

<Directory "/Library/WebServer/project_3/public_html">
    Options Indexes FollowSymLinks MultiViews
    MultiviewsMatch Any
    AllowOverride All
    Require all granted
</Directory>

I like trick 1. It is much cleaner and forces me to organize everything into VirtualHost.

Laszlo Malina
  • 168
  • 2
  • 10