之前提到用Apache做下载站,不装MySQL,不装PHP,就只是单独用Apache做下载站,这样无非是为了省内存,当然如果是一台VPS一个IP地址做一个站的就无所谓了,只需要把域名绑定到IP,然后把文件丢到/var/www/html(CentOS 5)目录下就可以了,但是如果单个IP地址想做两个甚至多个下载站就需要修改配置文件了,我去网上搜到了很多资料,我发现很多资料要么写的不正确,要不写的不明确,所以我这里记录并详细解释一下如果修改的。
安装Apache
我的Apache是买完VPS就自带的,如果没有安装的只需要运行以下命令来自动安装Apache。Apache的服务在CentOS是用的httpd这个名字,加y参数是静默安装。
yum - y install httpd
这里大家可能问我为啥不用Debian,Debian省内存。我看了下只装Apache平时也只有18-25M的运行内存,Debian也不会省出太多,而且我用CentOS用习惯了。
装完了Apache以后如果在浏览器中打开IP地址提示该页无法显示大家可以去看看是不是Apache的服务有没有启动,一般yum安装完毕了以后没有启动,第一次需要手动启动,运行以下命令来启动Apache。
service httpd start
大家再重新刷新一下网页是不是出现以下的界面。
配置文件
一般的CentOS的Apache的配置文件是在/etc/httpd/conf中,可能朋友会问从上面的截图我们可以看到,这台服务器的网页主目录是在/var/www/html,配置文件是/etc/httpd/conf.d/welcome.conf。其实这个conf配置文件只是这个欢迎页面的配置文件。我们只需要把这个文件里面所有的配置前面全部都加上#号就会发现这个页面不见了。而是会有个403禁止访问的页面。
如果大家是用SSH登陆上去的需要输入以下命令修改:
vi /etc/httpd/conf.d/welcome.conf
welcome.conf原配置文件应该是这样的:
# # This configuration file enables the default "Welcome" # page if there is no default index page present for # the root URL. To disable the Welcome page, comment # out all the lines below. # <LocationMatch "^/+$"> Options -Indexes ErrorDocument 403 /error/noindex.html </LocationMatch>
我们需要把所有的行全部加上#号。
# # This configuration file enables the default "Welcome" # page if there is no default index page present for # the root URL. To disable the Welcome page, comment # out all the lines below. # #<LocationMatch "^/+$"> # Options -Indexes # ErrorDocument 403 /error/noindex.html #</LocationMatch>
这样原来的那个欢迎页面就没有了。
但是现在打开就会出现一个403禁止访问页面,这是因为Apache默认把Index目录浏览关闭了,意思就是如果没有index.html文件的话网站根目录是禁止访问的。我们需要修改/etc/httpd/conf/httpd.conf这个配置文件。
我们使用SSH登陆的话需要运行以下命令
vi /etc/httpd/conf/httpd.conf
然后输入/号来查找Indexes FollowSymLinks,查找到这个以后输入字母a来进行插入和修改。
如果不是下面这个结果的话要修改成下面这个。
<Directory /> Options FollowSymLinks AllowOverride None </Directory>
修改完毕以后按ESC退出插入模式,然后输入:wq来进行保存。
最后重启Apache,下面大家看看是不是403消失了。
service httpd restart
配置多网站
配置多网站是修改httpd.conf文件的最下面的配置。用Page Down切换到配置最下面,初始化配置应该是这样的。
# # Use name-based virtual hosting. # #NameVirtualHost *:80 # # NOTE: NameVirtualHost cannot be used without a port specifier # (e.g. :80) if mod_ssl is being used, due to the nature of the # SSL protocol. # # # VirtualHost example: # Almost any Apache directive may go into a VirtualHost container. # The first VirtualHost section is used for requests without a known # server name. # #<VirtualHost *:80> # ServerAdmin webmaster@dummy-host.example.com # DocumentRoot /www/docs/dummy-host.example.com # ServerName dummy-host.example.com # ErrorLog logs/dummy-host.example.com-error_log # CustomLog logs/dummy-host.example.com-access_log common #</VirtualHost>
我们可以参考一下他的方式进行修改,但是记得NameVirtualHost *:80这个参数中*一定要修改成你的网站IP地址,否则下面的配置不会起作用。
下面是我的网站参数,大家可以按照这个来进行修改。
NameVirtualHost 1.2.3.4 #网站IP地址是1.2.3.4 <VirtualHost abc.nicky1605.com> #虚拟主机网站abc.nicky1605.com ServerAdmin admin@nicky1605.com #网站管理员邮箱 DocumentRoot /var/www/html/abc #网站主目录 ServerName abc.nicky1605.com #绑定网站域名 ErrorLog logs/abc.nicky1605.com.log #错误日志 Customlog logs/abc.nicky1605.com.log common #访问日志 </VirtualHost> <VirtualHost def.nicky1605.com> ServerAdmin admin@nicky1605.com DocumentRoot /var/www/html/def ServerName def.nicky1605.com ErrorLog logs/def.nicky1605.com.log Customlog logs/def.nicky1605.com.log common ? ? <Directory "/var/www/html/def"> ? ? Options -Indexes FollowSymLinks #关闭目录访问 </VirtualHost>
这样我在根目录下放了两个文件夹,一个abc,一个def,然后在DNS处把这两个子域绑到1.2.3.4这个IP上,用户访问子域名的时候就可以到abc或是def这个文件夹下了,其中def这个子域我禁用了Index目录浏览,也就是说不能直接看到目录结构。
但是这样做大家直接访问IP会发现什么问题,会发现页面跟abc.nicky1605.com一样,但是我们明明定义的是/var/www/html/这个文件夹,按照正常的话应该是可以看到abc跟def这两个文件夹,这是怎么回事?
其实是启用了虚拟主机以后原来的配置就会发生变化了,如果想要实现IP地址直接访问到根目录还要建个根目录的虚拟机。所以整个配置应该是这样的。
NameVirtualHost 1.2.3.4 #网站IP地址是1.2.3.4 <VirtualHost 1.2.3.4> ServerAdmin admin@nicky1605.com DocumentRoot /var/www/html ServerName 1.2.3.4 </VirtualHost> <VirtualHost abc.nicky1605.com> #虚拟主机网站abc.nicky1605.com ServerAdmin admin@nicky1605.com #网站管理员邮箱 DocumentRoot /var/www/html/abc #网站主目录 ServerName abc.nicky1605.com #绑定网站域名 ErrorLog logs/abc.nicky1605.com.log #错误日志 Customlog logs/abc.nicky1605.com.log common #访问日志 </VirtualHost> <VirtualHost def.nicky1605.com> ServerAdmin admin@nicky1605.com DocumentRoot /var/www/html/def ServerName def.nicky1605.com ErrorLog logs/def.nicky1605.com.log Customlog logs/def.nicky1605.com.log common <Directory "/var/www/html/def"> Options -Indexes FollowSymLinks #关闭目录访问 ?</Directory> </VirtualHost>
大家看到了么,需要建立一个ServerName为 1.2.3.4的IP地址的虚拟主机配置,这样的话就是显示正常了。
另外如果大家不想把子域名的目录放到/var/www/html/下需要把实际的网站目录进行修改,而且需要在每个虚拟主机配置中都设置是否关闭目录访问。
<Directory "/var/www/html/def"> Options -Indexes FollowSymLinks #关闭目录访问 </Directory> <Directory "/var/www/html/def"> Options Indexes FollowSymLinks #打开目录访问 </Directory>
本文作者为Nicky,转载请注明。