1. 程式人生 > >Linux Apache虛擬主機配置方法

Linux Apache虛擬主機配置方法

.html hostname onf -a cli 修飾 ls -l ant 瀏覽器

  

apache 虛擬主機配置

註意:

  1. 虛擬主機可以開很多個
  2. 虛擬主機配置之後,原來的默認/etc/httpd/httpd.conf中的默認網站就不會生效了

練習:

  • 主機server0

    • ip:172.25.0.11
    • hostname:server0.example.com
  • 客戶機

    • desktop0

要求:配置2個虛擬主機,域名分別為
1、www0.example.com
2、其他任意的域名,只要是可以訪問到server0的域名,也可以用server0的ip和機器名進行訪問

準備環境:

  1、server0機器

    ip:172.25.0.11

    機器名:server0.example.com

    /etc/hosts文件中寫入了desktop0的信息

    firewalld和selinux開啟

    可以使用yum源來安裝httpd服務

  2、desktop0機器

    ip:172.25.0.10

    機器名:desktop0.example.com

    /etc/hosts文件中寫入了server0的信息 

一、安裝web服務端軟件包

[[email protected] conf.d]# yum install httpd -httpd-manual -y 
httpd-manual  幫助文檔可以不用安裝

二、配置虛擬主機的配置文件

1、其他域名訪問配置文件

[[email protected] conf.d]# vim /etc/httpd/conf.d/default-vhosts.conf

	<VirtualHost _default_:80>                      ---> 匹配本機其他虛擬主機不能匹配的其他任何域名
		DocumentRoot  /srv/default/www/              ---> 定義網站目錄
		CustomLog    "logs/default-vhost.log" combined    ---> 日誌存放位置
		
		<Directory /srv/default/www/>                ---> 指定網站目錄訪問控制
			Options Indexes FollowSymLinks           ---> 可以復制httpd.conf中的<Directory /var/www/html>參數
			AllowOverride None                  ---> 
			Require all granted                  ---> 所有人都可以訪問
		</Directory> 
	</VirtualHost>

  

2、www0.example.com域名配置文件

[[email protected] conf.d]# vim /etc/httpd/conf.d/www0.example.com-vhosts.conf

<VirtualHost *:80>											
		Servername   www0.example.com		      		---> 綁定域名
		ServerAlias  www0						---> 其他域名
		DocumentRoot  /srv/www0.example.com/www/			---> 定義網站目錄
		CustomLog     "logs/www0.example.com-vhost.log" combined	---> 日誌存放位置
		
		<Directory /srv/www0.example.com/www/>				---> 指定網站目錄訪問控制
			Options Indexes FollowSymLinks				---> 
			AllowOverride None					---> 
			Require all granted					---> 所有人都可以訪問
		</Directory>
	</VirtualHost>

  

註意配置文件格式,換行符

三、建立虛擬主機的網站目錄

[[email protected] conf.d]# mkdir -p /srv/{default,www0.example.com}/www

四、建立測試網站的index.html

[[email protected] conf.d]# echo "I‘m other" >> /srv/default/www/index.html
[[email protected] conf.d]# echo "I‘m www0.example.com" >> /srv/www0.example.com/www/index.html

五、修改網站目錄權限

[[email protected] conf.d]# chown apache"apache -R /srv/* 

六、配置selinux安全上下文

首先查看原來的目錄安全上下文為var_t
[[email protected] conf.d]# ll -Zd /srv/
drwxr-xr-x. root root system_u:object_r:var_t:s0       /srv/
修改安全上下文
[[email protected] conf.d]# semanage fcontext -a  -t ‘httpd_sys_content_t‘ ‘/srv(/.*)?‘
[[email protected] conf.d]# restorecon -Rv /srv/ 
可以看到安全上下文已經改變
[[email protected] conf.d]# ll -Zd /srv/default
drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 /srv/despotic/

註釋:

1、安全上下文記不住可參考httpd默認網站目錄的安全上下文
ls -lZd /var/wwwt/html/
2、/srv(/).*? 正則表達式,?是修飾()的,表示括號裏的內容可有可無
當括號裏的內容存在時,匹配到/srv/.*內容
當括號裏的內容不存在時,那麽就匹配到的是/srv
3、restorecon -Rv /srv/ 刷新,使安全上下文生效

七、修改防火墻配置

[[email protected] conf.d]# firewall-cmd --permanent --add-service=http
[[email protected] conf.d]# firewall-cmd --reload

查看防火墻規則,可以看到services中已經有了http的服務,表明http的服務已經放行
[[email protected] conf.d]# firewall-cmd --list-all
public (default, active)
  interfaces: eth0
  sources: 
  services: dhcpv6-client http ssh
  ports: 
  masquerade: no
  forward-ports: 
  icmp-blocks: 
  rich rules: 

八、設定apache開啟自啟動,並且重新啟動apache服務

[[email protected] conf.d]# systemctl enable httpd
[[email protected] conf.d]# systemctl restart httpd

以上就是apache虛擬主機完整的配置方法

desktop0機器訪問以上地址:

  • curl www0 返回 I‘m www0.example.com
  • curl www0.dxample.com 返回 I‘m www0.example.com
  • curl server0.example.com 返回 I‘m other
  • curl 172.25.0.11     返回I‘m other

本實驗到此結束,還記得剛開始安裝的httpd-manual包嗎?下面我們來看一下效果。

使用瀏覽器訪問172.25.0.11/manual可以打開Apache的幫助頁面,如果配置的時候有不懂的地方,可以訪問這個頁面來查詢幫助。

技術分享圖片

文中有什麽不對或者不明白的地方,請大家私信我或者留言區發評論,我看到之後第一時間處理。

本人Linux菜鳥,歡迎各位Linux屆大咖指導,共同進步,謝謝。

Linux Apache虛擬主機配置方法