centOS7.6精簡版上配置Apache服務
阿新 • • 發佈:2021-12-06
- 對於 Apache 服務的介紹及作用這裡不做說明,只記錄Apache服務的配置過程,及一些使用方式。作業系統環境是 CentOS 7.6 精簡版,CentOS主機的IP地址是192.168.0.40、192.168.10.40兩個。配置過程如下:
[root@localhost ~]# yum install -y httpd #第1步:安裝Apache服務程式,安裝過程省略 [root@localhost ~]# systemctl restart httpd #啟動httpd服務 [root@localhost ~]# systemctl enable httpd #加入開機啟動項中 Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service. #下面命令設定 windows 主機可以訪問 [root@localhost ~]# iptables -I INPUT -s 192.168.0.5 -p tcp --dport 80 -j ACCEPT [root@localhost ~]# iptables -I INPUT -s 192.168.10.1 -p tcp --dport 80 -j ACCEPT #下面命令設定允許所有人可以訪問,上面兩條命令可以省略 [root@localhost ~]# iptables -I INPUT -s 0.0.0.0/0 -p tcp --dport 80 -j ACCEPT [root@localhost ~]# iptables-save #儲存防火牆設定,下次重啟仍然有效 #低版本Linux儲存防火牆設定命令是:service iptables save #現在在windows電腦的瀏覽器訪問192.168.0.40、192.168.10.40均可訪問到 Apache 的測試頁面 [root@localhost ~]# cd /var/www/html/ #進入到儲存網站檔案的目錄 [root@localhost html]# echo '<h1>你好,網站服務程式</h1>' > index.html #建立首頁檔案 #現在在windows電腦的瀏覽器上可以正常看到index.html檔案中的內容
- 接下來配置將預設的網站存放目錄 /var/www/html 修改為 /home/wwwroot 目錄。修改服務的預設儲存目錄,要涉及到 SELinux 安全上下文技術限制。ls 命令的 -Z 引數可檢視目錄的 SELinux 許可權。配置過程如下:
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf #編輯httpd配置檔案 ...... DocumentRoot "/home/wwwroot" #修改這行的值 # # Relax access to content within /var/www. # <Directory "/home/wwwroot"> #還要修改這一行的值 ...... [root@localhost ~]# systemctl restart httpd [root@localhost ~]# mkdir -p /home/wwwroot [root@localhost ~]# ls -ldZ /var/www/html drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html #上面輸出可看到 /var/www/html 目錄有 httpd_sys_content_t 許可權 [root@localhost ~]# ls -ldZ /home/wwwroot drwxr-xr-x. root root unconfined_u:object_r:home_root_t:s0 /home/wwwroot [root@localhost ~]# cat /etc/selinux/config #檢視SELinux的值 SELINUX=enforcing #關注這行即可,enforcing表示強制,還有permissive、disalbed #setenforce 0 命令臨時關閉 SELinux,setenforce 1 臨時開啟SELinux #semanage 命令修改或新增 SELinux域,fcontext引數表明要編輯SELinux值 [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot [root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/* [root@localhost ~]# restorecon -Rv /home/wwwroot #讓SELinux值生效 restorecon reset /home/wwwroot context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0 [root@localhost ~]# echo '<h2>hello Apache</h2>' > /home/wwwroot/index.html
現在在 windows 電腦的瀏覽器上訪問192.168.0.40、192.168.10.40,都能正常看到 /home/wwwroot/index.html 檔案的內容。實驗配置成功。
- 為每個使用者配置一個網站。在使用者家目錄下建立一個 public_html 目錄,在這個目錄中存放html檔案。要想通過瀏覽器正常訪問這個目錄下的html檔案,需要修改 userdir.conf 配置檔案。此外還要設定 SELinux 域允許httpd服務程式訪問使用者目錄下的檔案。操作如下:
[root@localhost ~]# vim /etc/httpd/conf.d/userdir.conf #修改配置檔案 17 UserDir public_html #將17行的 disabled修改為 public_html [root@localhost ~]# systemctl restart httpd #重啟服務 [root@localhost ~]# su - tom #切換到tom使用者 [tom@localhost ~]$ mkdir public_html #建立public_html目錄 [tom@localhost ~]$ chmod 755 -Rf public_html #修改目錄許可權,設定小一些 [tom@localhost ~]$ chmod 755 -Rf /home/tom #還要修改tom使用者家目錄的許可權 [tom@localhost ~]$ cd public_html/ [tom@localhost public_html]$ echo "<h3>hello, tom</h3>" > index.html [tom@localhost public_html]$ exit #退出tom使用者 logout [root@localhost ~]# getsebool -a | grep httpd httpd_enable_homedirs --> off #將這個設定為on開啟訪問使用者家目錄的許可權 [root@localhost ~]# setsebool -P httpd_enable_homedirs=on #開啟許可權
現在訪問 http://192.168.10.40/tom/、http://192.168.0.40/tom/ 就可正常看到 tom 使用者家目錄下public_html 目錄下的 index.html 檔案內容,當然也可以訪問該目錄下的其它檔案內容。
- 接下來設定訪問使用者家目錄下的網站需要輸入密碼。配置過程如下:
[root@localhost ~]# vim /etc/httpd/conf.d/userdir.conf #第1步:編輯這個配置檔案
31 <Directory "/home/*/public_html"> #將31行directory標籤內容修改為這樣
32 AllowOverride all
33 authuserfile "/etc/httpd/passwd" #認證對應的檔案
34 authname "Please enter User ID and Password" #提示資訊
35 authtype basic #認證型別,基礎認證:使用者名稱+密碼
36 require user tom #訪問網站時需要驗證的使用者名稱稱
37 </Directory>
[root@localhost ~]# htpasswd -c /etc/httpd/passwd tom #第2步:生成tom使用者的密碼資料庫
New password: #提示輸入密碼一個密碼,這個密碼不是tom使用者登入shell的密碼
Re-type new password:
Adding password for user tom
[root@localhost ~]# systemctl restart httpd #第3步:重啟httpd服務
現在訪問 http://192.168.10.40/tom/、http://192.168.0.40/tom/ 就要輸入正確的使用者名稱和密碼才能訪問。
- 接下來配置虛擬主機功能:基於IP地址對伺服器硬體資源進行切割,讓使用者能準確訪問到想要的資料。這個實驗會利用本節前面已經建立好的 /home/wwwroot 目錄,在這個目錄下建立3個子目錄來實驗虛擬網站主機功能。配置過程如下:
[root@localhost ~]# cd /home/wwwroot/
[root@localhost wwwroot]# ls
index.html
[root@localhost wwwroot]# mkdir 40 #第1步:建立3個子目錄
[root@localhost wwwroot]# mkdir 41
[root@localhost wwwroot]# mkdir 42
#第2步:編輯網絡卡引數配置檔案
[root@localhost wwwroot]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
......
IPADDR0=192.168.0.40
IPADDR1=192.168.0.41 #在這個網絡卡配置檔案中新增41、42兩個IP地址
IPADDR2=192.168.0.42
......
#重啟網路服務,並保證40、41、42三個IP都能正常ping通
[root@localhost wwwroot]# systemctl restart network
#第3步:向3個子目錄中分別寫入一個 index.html 檔案
[root@localhost wwwroot]# echo "<h1>11111111</h1>" > 40/index.html
[root@localhost wwwroot]# echo "<h1>22222222</h1>" > 41/index.html
[root@localhost wwwroot]# echo "<h1>33333333</h1>" > 42/index.html
#第4步:編輯Apache配置檔案
[root@localhost wwwroot]# vim /etc/httpd/conf/httpd.conf #編輯apache配置檔案
113 <virtualhost 192.168.0.40> #在113行處開始新增下面的內容
114 documentroot /home/wwwroot/40
115 <directory /home/wwwroot/40>
116 allowoverride none
117 require all granted
118 </directory>
119 </virtualhost>
120 <virtualhost 192.168.0.41>
121 documentroot /home/wwwroot/41
122 <directory /home/wwwroot/41>
123 allowoverride none
124 require all granted
125 </directory>
126 </virtualhost>
127 <virtualhost 192.168.0.42>
128 documentroot /home/wwwroot/42
129 <directory /home/wwwroot/42>
130 allowoverride none
131 require all granted
132 </directory>
133 </virtualhost>
......
[root@localhost wwwroot]# systemctl restart httpd #重啟服務
#第5步:給wwwroot 目錄及其子目錄新增 SELinux 域許可權,子目錄下的檔案也需要新增
[root@localhost wwwroot]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
[root@localhost wwwroot]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/40
[root@localhost wwwroot]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/40/*
[root@localhost wwwroot]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/41
[root@localhost wwwroot]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/41/*
[root@localhost wwwroot]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/42
[root@localhost wwwroot]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/42/*
現在在windows主機的瀏覽器中訪問192.168.0.40、192.168.0.41、192.168.0.42,可以正常訪問到相應的資源。