购买了一个云服务器,搭建了这个博客,但由于本人网站很多,而且访问量都不大。于是就考虑将网站全部移植到这个服务器来,可以减少成本支出。这就需要搭建虚拟主机:
一、新建网站目录(如不需改变网站根目录,请略过1-5条)
1.创建自定义网站根目录
mkdir /home/webroot
2.打开httpd主配置文件
vim /etc/httpd/conf/httpd.conf
3.找到以下代码
<Directory "/var/www"> AllowOverride None # Allow open access: Require all granted </Directory>
将其改为:
<Directory "/home/webroot"> AllowOverride All # Allow open access: Require all granted </Directory>
4.找到以下代码
<Directory "/var/www/html"> # # 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: # Options FileInfo AuthConfig Limit # AllowOverride None # # Controls who can get stuff from this server. # Require all granted </Directory>
改为[Options -Indexes为禁止访问目录列表]:
<Directory “/home/webroot”> # # 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 Options -Indexes # # AllowOverride controls what directives may be placed in .htaccess files. # It can be “All”, “None”, or any combination of the keywords: # Options FileInfo AuthConfig Limit # AllowOverride None # # Controls who can get stuff from this server. # Require all granted </Directory>
5.追加自动压缩网页代码功能,在主配置文件继续加入
# Gzip <IfModule deflate_module> SetOutputFilter DEFLATE SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary SetEnvIfNoCase Request_URI .(?:pdf|mov|avi|mp3|mp4|rm)$ no-gzip dont-vary AddOutputFilterByType DEFLATE text/* AddOutputFilterByType DEFLATE application/ms* application/vnd* application/postscript application/javascript appliction/x-javascript AddOutputFilterByType DEFLATE application/x-httpd-php application/x-httpd-fastphp </IfModule>
二、建立虚拟目录及主机(如不需改变网站根目录,请从本条开始浏览。主机目录为 /var/www/html)
6.建立一个项目目录
mkdir /home/webroot/demo
7.进入apache子配置文件夹,建立对应的项目配置文件
cd /etc/httpd/conf.d
vim demo.conf
8.在打开的配置文件中,输入以下代码
<VirtualHost *:80> DocumentRoot "/home/webroot/demo" ServerName 你的公网IP </VirtualHost> #如果已经申请下域名,则输入以下代码(上方作废),允许vvc.vc , www.vvc.vc两种方式的域名访问,并禁止直接访问服务器IP的方式访问项目 <VirtualHost *:80> ServerName 你的公网IP <Location /> Order Allow,Deny Deny from all </Location> </VirtualHost> <VirtualHost *:80> DocumentRoot "/home/webroot/demo" ServerName vvc.vc </VirtualHost> <VirtualHost *:80> DocumentRoot "/home/webroot/demo" ServerName www.vvc.vc </VirtualHost>
9.重启apache
systemctl restart httpd.service
评论前必须登录!
注册