Apache virtual host without domain name

2019-03-09 23:49发布

问题:

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>

回答1:

" 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>


回答2:

Create a new virtual host file, and setup like this:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        ServerAlias 192.168.1.1 
        DocumentRoot /somewhere/public_html
        <Directory /somewhere/public_html/>
         Options Indexes FollowSymLinks MultiViews ExecCGI
         AllowOverride Authconfig FileInfo
         Require all granted
        </Directory>
</VirtualHost>

add the serveralias and it will recognize the IP address as well ... if you want to add more IP addresses (like local network second interface), you can add more serveralias lines ...



回答3:

Step Six — Set Up Local Hosts File (Optional) If you have been using example domains instead of actual domains to test this procedure, you can still test the functionality of your virtual hosts by temporarily modifying the hosts file on your "LOCAL COMPUTER". This will intercept any requests for the domains that you configured and point them to your VPS server, just as the DNS system would do if you were using registered domains. This will only work from "YOUR COMPUTER", though, and is simply useful for testing purposes.

Note: Make sure that you are operating on your local computer for these steps and not your VPS server. You will need access to the administrative credentials for that computer.

If you are on a Mac or Linux computer, edit your local hosts file with administrative privileges by typing:

sudo vi /etc/hosts If you are on a Windows machine, you can find instructions on altering your hosts file here.

The details that you need to add are the public IP address of your VPS followed by the domain that you want to use to reach that VPS:

127.0.0.1 localhost 127.0.1.1 guest-desktop server_ip_address example.com server_ip_address example2.com

reference:https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-centos-7?utm_source=Customerio&utm_medium=Email_Internal&utm_campaign=Email_CentOSDistroNginxWelcome&mkt_tok=eyJpIjoiTnpWbU5tUTJPV1F5TVRBMyIsInQiOiJhd0JCQVI0NDd0ZWprUDFaaDlhbENcL0lyTjdSbnhwMEpkTE1QcXJTcHl1ZXFhNURKVmVBZHFKMk92RW1kSFwvMHowOW0zcExhaUdyOU42U2lLbk1Cd2FRYzB4XC9lbkhlWnd1ekZOcW1sZVhRYlwvT0xrTUpmQ2dEK2dNVUw4alFrc00ifQ%3D%3D