我试图在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
我错过了配置的东西吗?有任何想法吗...
提前致谢,
您需要为每个希望apache处理的主机提供VirtualHost条目。如果没有其他VirtualHosts与请求匹配,则配置文件中的第一个将用作默认值。
例如,如果我们有:
<VirtualHost *:80>
DocumentRoot /xampp/htdocs/gift
ServerName gift.loc
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /example/htdocs/gift
ServerName example.com
</VirtualHost>
对foobar.org的请求将由gift.loc虚拟主机处理。
你需要将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。
在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中指定)添加到主机file.add中:
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即可。
从docs看,我们需要为您想要服务的每个不同主机创建一个块。
此外,在同一文档中,如果要将虚拟主机添加到现有Web服务器,还必须为现有主机创建块。