php 没有域名的Apache虚拟主机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26706846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:00:50  来源:igfitidea点击:

Apache virtual host without domain name

phpapachednsvirtualhost

提问by heychez

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:

我有一个安装了 apache2 的 VPS,我想仅使用 IP 地址访问一些没有域名的 PHP 项目。例如:

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/

我的目录 /var/www/ 中还有其他域的项目,例如 example.com

/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

我的文件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>

回答by kums

" 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 ServerAliasline shown in the conf below)

我假设这已经有效(如果没有,请取消注释ServerAlias下面 conf 中显示的行)

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

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

For this you need to use the Apache Aliasdirective.

为此,您需要使用 ApacheAlias指令。

Update your 000-default.conffile to:

将您的000-default.conf文件更新为:

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

回答by Gipsz Jakab

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

添加 serveralias,它也会识别 IP 地址...如果您想添加更多 IP 地址(如本地网络第二个接口),您可以添加更多 serveralias 行...

回答by talib

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.

第六步 — 设置本地主机文件(可选)如果您一直使用示例域而不是实际域来测试此过程,您仍然可以通过临时修改“本地计算机”上的主机文件来测试虚拟主机的功能。这将拦截对您配置的域的任何请求,并将它们指向您的 VPS 服务器,就像 DNS 系统在您使用注册域时所做的那样。不过,这只适用于“您的计算机”,并且仅用于测试目的。

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.

注意:请确保您在本地计算机上执行这些步骤,而不是您的 VPS 服务器。您将需要访问该计算机的管理凭据。

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

如果您使用的是 Mac 或 Linux 计算机,请键入以下命令以管理权限编辑本地主机文件:

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

sudo vi /etc/hosts 如果您使用的是 Windows 计算机,则可以在此处找到有关更改主机文件的说明。

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:

您需要添加的详细信息是您的 VPS 的公共 IP 地址,然后是您要用于访问该 VPS 的域:

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

127.0.0.1 本地主机 127.0.1.1 来宾桌面 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

参考: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