Apache程序是目前擁有最高市場佔有率的web服務程序之一,其跨平臺和安全普遍被承認且擁有快速、可靠、簡單的API發展。下面是使用Apache服務部署靜態網站html
一.1.咱們首先建立目錄/media/cdrom,把光盤設配系統中鏡像掛載到目錄/media.cdrom命令爲:linux
[root@localhost ~]# mkdir -p /media/cdrom
[root@localhost ~]# mount /dev/cdrom /media/cdrom/web
2.配置yum源文件,編輯yum源文件名稱爲rhel7,後綴名REPO不可缺乏命令爲:數據庫
[root@localhost ~]# vim /etc/yum.repos.d/rhel7.repovim
3.配置文件內容以下瀏覽器
4.安裝http服務程序,命令爲:安全
[root@localhost ~]# yum install httpd服務器
5.啓動http服務,重啓服務器命令爲:tcp
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl enable httpd
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'網站
6.進入瀏覽器Firefox命令爲:
[root@localhost ~]# firefox
7.進入瀏覽器,輸入地址http://127.0.0.1,出現http默認頁面如圖所示
8.向/var/www/html/index.html中,寫入文件I like linux,替換http服務默認頁面命令爲;[root@localhost ~]# echo "I like linux" > /var/www/html/index.html
9.進入瀏覽器Firefox
10.http服務器默認頁面,替換成功
二.咱們下面進行創建網站數據
1.創建網站數據目錄,並建立首頁文件,給http服務程序的主配置文件,進行修改,吧119行和124行路徑改成/home/wwwroot
[root@localhost ~]# mkdir /home/wwwroot
[root@localhost ~]# echo "I like linux" > /home/wwwroot/index.html
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
文件內容爲
2..從新啓動http服務程序並驗證效果,可是頁面顯示仍是默認頁面,由於是selinux,因此往下看
[root@localhost ~]# systemctl restart httpd
[root@localhost ~]# firefox
3.進入Linux系統文件,查看selinux服務主配置文件中定義的默認狀態要爲enforcing,如不是就修改成enforcing。Enforcing是強制啓用安全策略模式,將攔截服務的不合法請求。
[root@localhost ~]# vim /etc/selinux/config
4.獲取當前運行模式,爲了確認上面的驗證效果是否由於selinux所致使的,用setenforce 0命令修改selinux的運行模式,0爲禁用1爲啓用,這只是臨時的。驗證頁面效果,爲正常頁面,問題確實出現selinux服務上
[root@localhost ~]# getenforce
Enforcing
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive
[root@localhost ~]# firefox
5.如今把selinux服務恢復到強制啓用安全策略模式,而後分別查看原始網站數據的保存目錄與當前網站數據目錄,二者有何不一樣的selinux安全上文值。
[root@localhost ~]# setenforce 1
[root@localhost ~]# ls -Zd /var/www/html/
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /var/www/html/
[root@localhost ~]# ls -Zd /home/wwwroot/
drwxr-xr-x. root root unconfined_u:object_r:home_root_t:s0 /home/wwwroot/
6.向新的網站數據目錄添加一條selinux安全上下文,讓這個目錄及全部文件都被httpd服務訪問到。
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t /home/wwwroot/*
7.使用restorecon命令設置selinux安全上下文當即將生效,再加上-RV參數對指定的目錄進行遞歸操做命令爲:restorecon –RV /home/wwwroot(目錄名稱),登陸頁面,頁面爲正常內容。
[root@localhost ~]# restorecon -Rv /home/wwwroot/
restorecon reset /home/wwwroot context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
restorecon reset /home/wwwroot/index.html context unconfined_u:object_r:home_root_t:s0->unconfined_u:object_r:httpd_sys_content_t:s0
[root@localhost ~]# firefox
三.設置我的用戶主頁功能
1.首先配置文件,將17行(表示http開啓我的用戶主頁面)加上註釋,24行(表示網站用戶家目錄中的保存目錄)去掉註釋。
[root@localhost ~]# vim /etc/httpd/conf.d/userdir.conf
2.切換到HOME目錄下,建立用戶linuxprobe ,.建立網站的首頁文件,對家目錄的權限修改成755,保證其餘人也能讀取內容。
[root@localhost home]# useradd linuxprobe
[root@localhost home]# su - linuxprobe
[linuxprobe@localhost ~]$ mkdir public_html
[linuxprobe@localhost ~]$ echo "I like linux" > public_html/index.html
[linuxprobe@localhost ~]$ chmod -Rf 755 /home/linuxprobe
3.重啓服務,頁面顯示報錯,又是selinux的錯誤。
4.使用getsebool命令查閱並過濾出全部與http協議相關的安全策略,off爲禁止on爲容許
[root@localhost ~]# getsebool -a | grep http
httpd_anon_write --> off
httpd_builtin_scripting --> on
httpd_can_check_spam --> off
httpd_can_connect_ftp --> off
httpd_can_connect_ldap --> off
httpd_can_connect_mythtv --> off
httpd_can_connect_zabbix --> off
httpd_can_network_connect --> off
httpd_can_network_connect_cobbler --> off
httpd_can_network_connect_db --> off
httpd_can_network_memcache --> off
httpd_can_network_relay --> off
httpd_can_sendmail --> off
httpd_dbus_avahi --> off
httpd_dbus_sssd --> off
httpd_dontaudit_search_dirs --> off
httpd_enable_cgi --> on
httpd_enable_ftp_server --> off
httpd_enable_homedirs --> off
httpd_execmem --> off
httpd_graceful_shutdown --> on
httpd_manage_ipa --> off
httpd_mod_auth_ntlm_winbind --> off
httpd_mod_auth_pam --> off
httpd_read_user_content --> off
httpd_run_stickshift --> off
httpd_serve_cobbler_files --> off
httpd_setrlimit --> off
httpd_ssi_exec --> off
httpd_sys_script_anon_write --> off
httpd_tmp_exec --> off
httpd_tty_comm --> off
httpd_unified --> off
httpd_use_cifs --> off
httpd_use_fusefs --> off
httpd_use_gpg --> off
httpd_use_nfs --> off
httpd_use_openstack --> off
httpd_use_sasl --> off
httpd_verify_dns --> off
named_tcp_bind_http_port --> off
prosody_bind_http_port --> off
5.用到selinux域安全策略爲http_enable_homedirs,用setsebool修改布爾值,-P爲參數,對修改後的selinux策略規則永久生效。頁面效果正常。
6..使用htpasswd命令生成密碼數據庫,-c第一次生成,而後分別添加密碼,以及驗證獲得用戶名稱。
7.編輯我的用戶主頁功能的配置文件。31到35行.
8.重啓服務,訪問用戶網站。網站需輸入用戶名和密碼,用戶名是上面建立的用戶名。密碼同上所設。