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