Apache virtual host without domain name

heychez picture heychez · Nov 3, 2014 · Viewed 32.1k times · Source

I have a VPS with apache2 installed and I would like to access some PHP projects without a domain name just with the IP address. For example:

http://162.243.93.216/projecta/index.php
http://162.243.93.216/projectb/index.php

I have other projects with domain like example.com, in my directory /var/www/

/html/
   info.php
/projecta/
/projectb/
/example/

When I go to

http://162.243.93.216/info.php then /var/www/html/info.php is opened. 

My file 000-default.conf

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    <Directory /var/www/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
     </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Answer

kums picture kums · Nov 3, 2014
" http://162.243.93.216/info.php then /var/www/html/info.php is opened "

I am assuming this already works (If not, uncomment the ServerAlias line shown in the conf below)

You now want to map

http://162.243.93.216/projecta/ to /var/www/projecta
http://162.243.93.216/projectb/ to /var/www/projectb

For this you need to use the Apache Alias directive.

Update your 000-default.conf file to:

<VirtualHost *:80>
    # ServerAlias 162.243.93.216
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    Alias /projecta /var/www/projecta
    Alias /projectb /var/www/projectb

    <Directory /var/www/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
     </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>