首页 > 解决方案 > 子域不工作(Ubuntu 16.04 和 Apache)

问题描述

我一直在到处寻找很长一段时间,我就是想不通。我正在尝试创建一个名为bikestore的子域,它是example.com的一部分,(bikestore.example.com) - 我已经将此代码添加到 etc/apache2/apache2.conf:

<VirtualHost *:80>
   ServerName bikestore.example.com
    DocumentRoot /var/www/bikestore.example.com
</VirtualHost>

我还在 /etc/hosts 中添加了这一行:

(服务器 IP)bikestore.example.com

并创建一个新目录(/var/www/bikestore.example.com)

我已经重新启动了 apache,但我无法访问 bikestore.example.com,我也在为我的域使用 Cloudflare。你看看我是不是做错了什么?我正在使用 Ubuntu 16.04 和 Apache

标签: apacheubuntuapache2subdomain

解决方案


您必须在子目录sites-available中创建文件bikestore.example.com.conf ,例如 /etc/apache2/sites-available/bikestore.example.com.conf

该文件应该包含的不仅仅是您发布的代码。

<VirtualHost *:80>
   	ServerName bikestore.example.com
    	DocumentRoot /var/www/bikestore.example.com

	  <Directory /var/www/bikestore.example.com>
		   # directory settings sample
		   Options Indexes FollowSymLinks
		   AllowOverride None
	</Directory>

	# Protect files and directories from prying eyes.
	<FilesMatch "(\.(engine|inc|info|install|module|profile|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)|code-style\.pl|Entries.*| #Repository|Root|Tag|Template)$">
		        Order allow,deny
	</FilesMatch>

	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel crit
	ErrorLog  ${APACHE_LOG_DIR}/bikestore.example.com.error.log

	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	CustomLog  ${APACHE_LOG_DIR}/bikestore.example.com.access.log combined
</VirtualHost>

您还应该在此配置中考虑一些安全性。

完成后,您启用站点并重新加载 apache 设置:

sudo a2ensite bikestore.example.com

sudo 服务 apache2 重新加载

这将在启用站点的目录中创建指向您的配置文件的链接

之后你重新加载 apache,如果一切顺利,网站就会工作。但如果没有,你会在你的错误文件夹 /var/log/apache2 中找到错误

请检查本教程: https ://www.ostechnix.com/configure-apache-virtual-hosts-ubuntu-part-1/

https://httpd.apache.org/docs/2.4/vhosts/examples.html

https://www.digitalocean.com/community/tutorials/how-to-set-up-apache-virtual-hosts-on-ubuntu-16-04


推荐阅读