時間:2023-06-30 23:48:01 | 來源:網(wǎng)站運營
時間:2023-06-30 23:48:01 來源:網(wǎng)站運營
linux入門系列18--Web服務(wù)之Apache服務(wù)1:前面系列文章講解了Linux下通過文件傳輸、文件共享、郵件系統(tǒng)來分享和獲取資源,本文講解網(wǎng)絡(luò)資源獲取和共享的另外一種形式,通過Apache服務(wù)程序來提供Web服務(wù)。需要注意的是,在CentOS和RHEL上,Apache軟件包和服務(wù)稱為httpd而非apache。
[root@apache ~]# rpm -q httpdpackage httpd is not installed[root@apache ~]# yum install httpdLoaded plugins: fastestmirror, langpacks...省略部分內(nèi)容Complete![root@apache ~]# rpm -q httpdhttpd-2.4.6-90.el7.centos.x86_64[root@apache ~]#
安裝完成后,httpd服務(wù)是沒有啟動的,還需要將其啟動,并加入到開機啟動中[root@apache ~]# systemctl start httpd[root@apache ~]# systemctl enable httpdln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'[root@apache ~]#
此時在虛擬機Centos內(nèi)部,打開瀏覽器,即可看到apache部署成功[root@apache ~]# firewall-cmd --permanent --zone=public --add-service=httpsuccess[root@apache ~]# firewall-cmd --permanent --zone=public --add-service=httpssuccess[root@apache ~]# firewall-cmd --reload success[root@apache ~]#
這樣宿主機上也可以直接訪問該Web,如下圖[root@apache ~]# httpd -vServer version: Apache/2.4.6 (CentOS)Server built: Aug 8 2019 11:41:18[root@apache ~]#
[root@apache ~]# systemctl status httpdhttpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled) Active: active (running) since Mon 2020-02-10 10:52:35 CST; 7min ago Docs: man:httpd(8) man:apachectl(8)...省略部分內(nèi)容
[root@apache ~]# vim /etc/httpd/conf/httpd.conf
如果忘記VI或VIM編輯器使用方法的,請返回復(fù)習(xí)前面的文章:“l(fā)inux入門系列4--vi&vim編輯器”。
[root@apache ~]# ll /var/www/html/total 0[root@apache ~]# echo "hi,this is heimatengyun's blog">/var/www/html/index.html[root@apache ~]# ll /var/www/html/total 4-rw-r--r--. 1 root root 31 Feb 10 11:59 index.html[root@apache ~]#
在宿主機通過瀏覽器再次查看,即可查看剛才新加的網(wǎng)頁文件內(nèi)容[root@apache /]# mkdir /website[root@apache /]# cd /website/[root@apache website]# echo "this is my custom directory">index.html[root@apache website]# lltotal 4-rw-r--r--. 1 root root 28 Feb 10 12:21 index.html[root@apache website]#
(2)主配置文件中配置目錄[root@apache website]# vim /etc/httpd/conf/httpd.conf
[root@apache website]# systemctl restart httpd
在宿主機中瀏覽器查看一般情況只有網(wǎng)站的首頁頁面文件不存在或用戶權(quán)限不足時,才顯示httpd默認的頁面。比如后文的4.1,設(shè)置禁止的ip訪問后,就直接跳轉(zhuǎn)到此頁面。我們在加上文件名試下呢
[root@apache website]# getenforce Enforcing[root@apache website]# setenforce 0[root@apache website]# getenforce Permissive[root@apache website]#
再次在宿主機瀏覽器訪問[root@apache website]# cat /etc/selinux/config# This file controls the state of SELinux on the system.# SELINUX= can take one of these three values:# enforcing - SELinux security policy is enforced.# permissive - SELinux prints warnings instead of enforcing.# disabled - No SELinux policy is loaded.SELINUX=enforcing# SELINUXTYPE= can take one of these two values:# targeted - Targeted processes are protected,# minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection.SELINUXTYPE=targeted [root@apache website]#
該文件中的值定義的是SELinux默認的運行狀態(tài),也就是系統(tǒng)重啟后的狀態(tài),因此修改它不會立即生效。[root@apache website]# getenforce Enforcing[root@apache website]#
[root@apache website]# getenforce Enforcing[root@apache website]# setenforce 0[root@apache website]# getenforce Permissive[root@apache website]
因此,如果原來系統(tǒng)的SELinux是關(guān)閉的,如果要開啟它,需要在配置文件中將進行設(shè)置SELINUX=enforcing,并同時要通過setenforce 1進行設(shè)置。這樣即使系統(tǒng)重啟,依然生效。[root@apache ~]# getsebool usage: getsebool -a or getsebool boolean...[root@apache ~]# getsebool -aabrt_anon_write --> offabrt_handle_event --> offabrt_upload_watch_anon_write --> onantivirus_can_scan_system --> off...省略部分內(nèi)容
其中,on表示允許狀態(tài),off表示禁止狀態(tài)。[root@apache ~]# getsebool -a | grep xdm_write_homexdm_write_home --> off[root@apache ~]# setsebool -P xdm_write_home=on[root@apache ~]# getsebool -a | grep xdm_write_homexdm_write_home --> on[root@apache ~]# setsebool -P xdm_write_home=off[root@apache ~]# getsebool -a | grep xdm_write_homexdm_write_home --> off[root@apache ~]# getsebool xdm_write_homexdm_write_home --> off
以上實驗,我們先查看grep xdm_write_home狀態(tài),然后將其改為on,查看修改是否生效,然后再將其改回原值。查看某一項的值可以通過正則匹配,也可以直接通過該具體項獲取。[root@apache website]# ll -Z /var/www/html/-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html[root@apache website]# ll -Z /website/-rw-r--r--. root root unconfined_u:object_r:default_t:s0 index.html[root@apache website]#
我們通過ll命令的-Z參數(shù)進行查看,很明顯就看到了不同。(-Z參數(shù)專門用于查看SELinux域相關(guān)設(shè)置)[root@apache website]# ll -Z /var/www/html/-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html[root@apache website]# ll -Z /website/-rw-r--r--. root root unconfined_u:object_r:default_t:s0 index.html[root@apache website]# semanage fcontext -a -t httpd_sys_content_t /website[root@apache website]# semanage fcontext -a -t httpd_sys_content_t /website/*[root@apache website]# ll -Z /website/-rw-r--r--. root root unconfined_u:object_r:default_t:s0 index.html[root@apache website]# restorecon -Rv /website/restorecon reset /website context unconfined_u:object_r:default_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0restorecon reset /website/index.html context unconfined_u:object_r:default_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0[root@apache website]# ll -Z /website/ -rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html[root@apache website]#
需要注意的是,設(shè)置上下文值后并非立即生效,還需要執(zhí)行restorecon才會生效,過程如上,請自行體驗。本案例演示了SELinux上下文對資源的限制導(dǎo)致的web不能訪問,以及設(shè)置上下文值。
[root@apache website]# vim /etc/httpd/conf.d/userdir.conf
[root@apache website]# useradd heima[root@apache website]# echo "123456" | passwd --stdin heimaChanging password for user heima.passwd: all authentication tokens updated successfully.[root@apache website]# su - heima[heima@apache ~]$ ls[heima@apache ~]$ pwd/home/heima[heima@apache ~]$ mkdir public_html[heima@apache ~]$ echo "this is heima's website">public_html/index.html[heima@apache ~]$ ll public_html/total 4-rwxrwxr-x. 1 heima heima 24 Feb 10 16:11 index.html[heima@apache ~]$ chmod -Rf 775 /home/heima/
另外,還需要把家目錄的權(quán)限修改為755,保證其他人也有權(quán)限讀取里面的內(nèi)容。[root@apache ~]# systemctl restart httpd
訪問地址為ip/~用戶名,其中的波浪號是必需的,而且網(wǎng)址、波浪號、用戶名之間沒有空格。我們回想一下,這個現(xiàn)象和案例1遇到的癥狀有幾分相似,當時是因為我們新建了一個目錄/website,而沒有設(shè)置SELinux上下文所導(dǎo)致。那這次是不是也是因為SELinux的上下文導(dǎo)致呢?
[heima@apache ~]$ ll -Z /home/heima/drwxrwxr-x. heima heima unconfined_u:object_r:httpd_user_content_t:s0 public_html[heima@apache ~]$
對吧,很明顯不是SELinux上下文導(dǎo)致,因為httpd默認就給用戶目錄添加了上下文值。[root@apache ~]# getsebool httpd_enable_homedirshttpd_enable_homedirs --> off[root@apache ~]# setsebool httpd_enable_homedirs=on[root@apache ~]#
此時,我們再次訪問,即可正常訪問了。[root@apache ~]# htpasswd -c /etc/httpd/passed heimaNew password: Re-type new password: Adding password for user heima[root@apache ~]#
注意,文件名任意取,在下一步中保持一致即可,-c參數(shù)表示第一次生成。設(shè)置密碼為888888,以區(qū)別之前heima用戶登錄系統(tǒng)的密碼123456,這樣做的目的是為了區(qū)分說明不此處設(shè)置的密碼不是用戶登錄系統(tǒng)的密碼。[root@apache ~]# vim /etc/httpd/conf.d/userdir.conf <Directory "/home/*/public_html"># AllowOverride FileInfo AuthConfig Limit Indexes# Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec# Require method GET POST OPTIONS AllowOverride all authuserfile "/etc/htppd/passed" authname "heima website" authtype basic require user heima Require method GET POST OPTIONS</Directory>
將文末的內(nèi)容注釋并按如下進行修改即可[root@apache ~]# systemctl restart httpd
再次訪問,就要求輸入密碼了。這個案例演示了SELinux域?qū)M程的控制,以及如何對其進行設(shè)置。
關(guān)鍵詞:服務(wù),入門,系列
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。