Apache2在Ubuntu和Arch下,配置文件的组织方式差别还是挺大的。小记一下,因为我过不久肯定就会忘的=.=
Ubuntu
在Ubuntu下,配置目录在/etc/apache2
。在此目录下,主要配置文件是apache2.conf
,但是为了更清晰,在Ubuntu上Apache的配置文件是拆分了的,最后在apache2.conf
中都Include
了进来。比如:
# Include module configuration:
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
# Include list of ports to listen on
Include ports.conf
Apache的配置文件主要拆出来的有这几块:ports.conf
/sites
/conf
/mods
,具体目录结构如下:
/etc/apache2/
|-- apache2.conf
| `-- ports.conf
|-- mods-enabled
| |-- *.load
| `-- *.conf
|-- conf-enabled
| `-- *.conf
|-- sites-enabled
| `-- *.conf
ports.conf
其中ports.conf
里面设置了监听的端口,默认已经有了Listen 80
。
sites-avaliable & sites-enable
sites
分为sites-available
和sites-enable
两个文件夹。一般是available
里面放配置文件,代表所有可用的站点,enable
里面放软链接,链到available
里面的配置文件,则这个配置文件就启用了。
Note: 切记软链接的时候一定要用全路径,如ln -s /etc/apache2/sites-available/puppy.conf /etc/apache2/sites-enable/
,不然会出现迷之错误。
sites
就是站点的意思。比如我有两个站点,apache-default.conf
和puppy.conf
,前者设置为端口81
,并在开头写上Listen 81
,虽然理论上应该写到ports.conf
文件中,不过写在这里会更清晰。不想要这个站点的时候直接删了这个文件就行了。
比如,apache的默认配置文件,我们把端口改成81
:
Listen 81
<VirtualHost *:81>
#ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
站点配置文件中除了修改对应的端口号,还要修改DocumentRoot
,即站点放置的位置。Ubuntu下Apache站点放置位置在/var/www
,比如默认站点在/var/www/html
。我们可以将puppy站点放置目录为/var/www/puppy
,这样在访问我们的Apache地址www.xxx.com.cn
(如果有域名的话。否则写架设Apache服务器的IP)时对应的就是/var/www/puppy[/index.html]
。访问www.xxx.com.cn:81
对应的就是Apache的站点/var/www/html
。
conf / mods
conf
和mods
与sites
类似,也是由available
和enable
构成。
这些东西最后都include
在apache2.conf
中。
DocumentRoot的概念
apache2.conf里面有<Directory blabla_path>
或者<File blabla_file>
,里面设置的是一些权限的问题,即给开放的站点加上权限。
比如apache2.conf
默认对几个地方进行了设置:
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Document Roots: By default, Ubuntu does not allow access through the web browser to any file apart of those located in /var/www, public_html directories (when enabled) and /usr/share (for web applications). If your site is using a web document root located elsewhere (such as in /srv) you may need to whitelist your document root directory in /etc/apache2/apache2.conf.
启动方式
Ubuntu启动Apache的方式为service apache2 start
,或者用systemctl或者/etc/init.d的启动方式都行。
Arch
Arch上Apache的默认配置文件在/etc/httpd
下,比如配置文件在conf
文件夹中。默认配置文件为/etc/httpd/conf/httpd.conf
,其他配置文件在/etc/httpd/conf/extra
文件夹里,也是通过Include
引用进来的,比如:
# User home directories
Include conf/extra/httpd-userdir.conf
# Various default settings
Include conf/extra/httpd-default.conf
装完php后就得把php的配置文件Include
进来:
Include conf/extra/php7_module.conf
装完phpmyadmin也一样:
Include conf/extra/phpmyadmin.conf
其实这样做的话,个人感觉会把主配置文件httpd.conf
搞得有点儿庞杂,不像Ubuntu下的主配置文件apache.conf
那么简洁。
ArchWiki上Apache的配置页面对httpd.conf
有比较详细的介绍,比如DocumentRoot "/srv/http"
是Arch下Apache的默认站点目录。ServerRoot "/etc/httpd"
是配置文件的目录。这些都和Ubuntu上的不同。Listen 80
也在这里面。ServerAdmin shininglhb@163.com
可以设置邮箱,当页面挂了的时候会显示给用户。
当然这里面也有关于DocumentRoot
和Directory
的设置,比如默认的:
DocumentRoot "/srv/http"
<Directory "/srv/http">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.4/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# AllowOverride FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Require all granted
</Directory>
构建多个站点
如果我们想构建多个站点,且希望更方便的维护,建议为每一个虚拟主机创建一个配置文件并文件存储到一个文件夹中 /etc/httpd/conf/vhosts
中。并在 /etc/httpd/conf/httpd.conf
中注释掉Include conf/extra/httpd-vhosts.conf
这一行。
比如我们新建一个puppy的站点,监听端口为81
,就新建一个/etc/httpd/conf/vhosts/puppy.dom
,格式按照下面这个示例进行替换即可:
<VirtualHost domainname1.dom:80>
ServerAdmin webmaster@domainname1.dom
DocumentRoot "/home/user/http/domainname1.dom"
ServerName domainname1.dom
ServerAlias domainname1.dom
ErrorLog "/var/log/httpd/domainname1.dom-error_log"
CustomLog "/var/log/httpd/domainname1.dom-access_log" common
<Directory "/home/user/http/domainname1.dom">
Require all granted
</Directory>
</VirtualHost>
这一点跟Ubuntu上新建站点的配置文件没有什么区别。
最后别忘了在httpd.conf
中加入:
Include conf/vhosts/puppy.dom
启动方式
Arch使用的是systemd,所以使用systemctl start httpd.service
来启动Apache。
Ubuntu配置文件是apache2.conf
,启动参数是apache2
;Arch配置文件是httpd.conf
,启动参数是httpd.service
。
两个不同的发行版有不同的配置组织方式。不过总体感觉,Ubuntu下的Apache的配置方式更整洁一点儿。