Apache:虚拟主机配置

发布于 2024-10-01 01:34:14 字数 497 浏览 5 评论 0原文

当我尝试在 apache 中配置我的虚拟主机时。我放了这样的东西,

NameVirtualHost *:80

<VirtualHost *:80>
   DocumentRoot /xampp/htdocs/gift
   ServerName gift.loc  
</VirtualHost>

在我的主机文件中我放了这样的东西,

127.0.0.1       localhost
127.0.0.1       gift.loc

然后在浏览器上运行它,

http://gift.loc - is fine

但是当我尝试使用它时,

http://localhost/othersite - can't found

我是否错过了一些配置?任何想法...

提前致谢,

As i tried to configure my virtual host in apache. I put something like this,

NameVirtualHost *:80

<VirtualHost *:80>
   DocumentRoot /xampp/htdocs/gift
   ServerName gift.loc  
</VirtualHost>

And in my hosts file i put something like this,

127.0.0.1       localhost
127.0.0.1       gift.loc

And i run it on the browser,

http://gift.loc - is fine

But when i tried using this,

http://localhost/othersite - can't found

Do i missed somehting to configure? ANy ideas...

Thanks in advance,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千秋岁 2024-10-08 01:34:14

对于您希望 apache 处理的每个主机,您都需要一个 VirtualHost 条目。如果没有其他 VirtualHost 与请求匹配,则配置文件中的第一个将用作默认值。

例如,如果我们有:

<VirtualHost *:80>
   DocumentRoot /xampp/htdocs/gift
   ServerName gift.loc  
</VirtualHost>

<VirtualHost *:80>
   DocumentRoot /example/htdocs/gift
   ServerName example.com  
</VirtualHost>

对 foobar.org 的请求将由gift.loc 虚拟主机处理。

You need a VirtualHost entry for every host you want apache to handle. The first one in the config file will be used as the default if no other VirtualHosts match the request.

For example if we have:

<VirtualHost *:80>
   DocumentRoot /xampp/htdocs/gift
   ServerName gift.loc  
</VirtualHost>

<VirtualHost *:80>
   DocumentRoot /example/htdocs/gift
   ServerName example.com  
</VirtualHost>

A request for foobar.org will get handled by the gift.loc virtual host.

巡山小妖精 2024-10-08 01:34:14

你需要将 localhost 放入 vhosts.conf

    NameVirtualHost *:80

    <VirtualHost *:80>
       DocumentRoot /xampp/htdocs/
       ServerName localhost
    </VirtualHost>

    <VirtualHost *:80>
       DocumentRoot /xampp/htdocs/gift
       ServerName gift.loc  
    </VirtualHost>

这工作正常(确保重新启动 apache)。如果您需要检查您的配置,您可以(至少在 Linux 上)运行 httpd -S。

you need to put localhost in the vhosts.conf

    NameVirtualHost *:80

    <VirtualHost *:80>
       DocumentRoot /xampp/htdocs/
       ServerName localhost
    </VirtualHost>

    <VirtualHost *:80>
       DocumentRoot /xampp/htdocs/gift
       ServerName gift.loc  
    </VirtualHost>

This works fine (Make sure you restart apache). If you need to check your configuration you can (on linux at least) run httpd -S.

阳光下的泡沫是彩色的 2024-10-08 01:34:14

在 ubuntu 上设置虚拟主机需要执行以下几个步骤:
假设您的项目文件夹名称是 myProject

第 1 步:将您的文件夹放入 /var/www/html

sudo mv ~/myProject /var/www/html/

第 2 步:将项目文件夹的所有权授予 www-data

sudo chown -R www-data:www-data /var/www/html/myProject

第 3 步:在可用站点中创建新站点:

cd /etc/apache2/sites-available/ 
ls

在这里您将看到现有的 000 -default.conf 和 default-ssl.conf 。将两个文件的内容复制到一个文件中并替换您的文件夹名称或将此文件复制到名为 myProject.conf 的新文件

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/myProject/
        ServerName project.com
        ServerAlias www.project.com

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

</VirtualHost>

<VirtualHost *:443>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www/html/myProject/
        ServerName project.com
        ServerAlias www.project.com          

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        SSLEngine on

        SSLCertificateFile  /etc/ssl/certs/mobidev_cert.pem
        SSLCertificateKeyFile /etc/ssl/certs/mobidev_key.pem


        <FilesMatch "\.(cgi|shtml|phtml|php)$">
                SSLOptions +StdEnvVars
        </FilesMatch>
        <Directory /usr/lib/cgi-bin>
                SSLOptions +StdEnvVars
        </Directory>

</VirtualHost>

包含自签名证书的路径图中还显示了可以轻松下载的 ssl 密钥和 ssl 证书。

步骤 4:将您的项目添加到 apache 配置文件中。

sudo vi /etc/apache2/apache2.conf

将此行放入文件中:

DocumentRoot "/var/www/html/myProject"
<Directory /var/www/html/myProject/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

步骤 5:将您的虚拟服务器名称(在 myProject.conf 中指定)添加到主机文件中。添加该行:

sudo gedit /etc/hosts
127.0.1.1   project.com

步骤 6:现在全部设置,启用站点,重新启动 apache

sudo a2ensite /etc/apache2/sites-availabl/myProject.conf
sudo systemctl reload apache2
sudo update-rc.d apache2 defaults
sudo update-rc.d mysql defaults
sudo a2enmod ssl
sudo a2ensite default-ssl

只需在浏览器中点击 project.com 。

There are few steps you need to follow to setup the virtual host on ubuntu:
Let say that your project folder name is myProject

Step 1:Place your folder inside /var/www/html

sudo mv ~/myProject /var/www/html/

Step 2: Give ownership of project folder to www-data

sudo chown -R www-data:www-data /var/www/html/myProject

Step 3:Create new site inside Sites available:

cd /etc/apache2/sites-available/ 
ls

Here you will see existing 000-default.conf and default-ssl.conf .Copy the content of both file into one file and replace your folder name or copy this one into new file named myProject.conf

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    #ServerName www.example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/myProject/
        ServerName project.com
        ServerAlias www.project.com

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

</VirtualHost>

<VirtualHost *:443>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www/html/myProject/
        ServerName project.com
        ServerAlias www.project.com          

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        SSLEngine on

        SSLCertificateFile  /etc/ssl/certs/mobidev_cert.pem
        SSLCertificateKeyFile /etc/ssl/certs/mobidev_key.pem


        <FilesMatch "\.(cgi|shtml|phtml|php)$">
                SSLOptions +StdEnvVars
        </FilesMatch>
        <Directory /usr/lib/cgi-bin>
                SSLOptions +StdEnvVars
        </Directory>

</VirtualHost>

Include the path of self signed certificate also in this as shown ssl key and ssl certificate that can be downloaded easily.

Step 4:Add your project into apache configuration file.

sudo vi /etc/apache2/apache2.conf

put this lines in the file:

DocumentRoot "/var/www/html/myProject"
<Directory /var/www/html/myProject/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

Step 5:Add your virtual server name (specified in myProject.conf) into host file.add that line:

sudo gedit /etc/hosts
127.0.1.1   project.com

Step 6:Now all set ,enable site,restart apache

sudo a2ensite /etc/apache2/sites-availabl/myProject.conf
sudo systemctl reload apache2
sudo update-rc.d apache2 defaults
sudo update-rc.d mysql defaults
sudo a2enmod ssl
sudo a2ensite default-ssl

Just hit project.com in your browser.

彩扇题诗 2024-10-08 01:34:14

docs 来看,我们需要为您想要服务的每个不同主机创建一个块。

此外,在同一文档中,如果您要将虚拟主机添加到现有 Web 服务器,则还必须为现有主机创建一个块。

From the docs, it looks like we need to create a block for each different host that you would like to serve.

Further in the same doc, If you are adding virtual hosts to an existing web server, you must also create a block for the existing host.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文