LAMP=Linux+Apache(httpd)+Mysql(mariadb)+PHPphp
Apache HTTP 服務器 2.4 文檔:http://httpd.apache.org/docs/2.4/html
(1).實驗環境mysql
LAMP服務器:youxi1 192.168.5.101web
測試主機:youxi2(CentOS7) 192.168.5.102sql
Windows 192.168.5.1apache
(2).安裝與說明vim
CentOS6使用以下命令:瀏覽器
yum -y install httpd mysql mysql-server php php-mysql
CentOS7使用以下命令:安全
[root@youxi1 ~]# yum -y install httpd mariadb mariadb-server php php-mysql
httpd說明:httpd是Apache服務的主程序包,服務器端必須安裝。httpd-devel是Apache開發程序包。httpd-manual是Apache手冊文檔,包含HTML格式的Apache計劃的Apache User's Guide說明指南。httpd-tools是Apache相關工具包。服務器
(3).開啓服務進行測試
測試httpd和mariadb是否能正常啓動,並設置開機自啓
[root@youxi1 ~]# systemctl start httpd.service [root@youxi1 ~]# systemctl enable httpd.service Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service. [root@youxi1 ~]# systemctl start mariadb.service [root@youxi1 ~]# systemctl enable mariadb.service Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
編寫php測試文件
[root@youxi1 ~]# vim /var/www/html/index.php <?php phpinfo(); ?>
重啓httpd
[root@youxi1 ~]# systemctl restart httpd.service
若是防火牆是打開的,還需將端口號加入防火牆中
[root@youxi1 ~]# firewall-cmd --permanent --zone=public --add-port=80/tcp success [root@youxi1 ~]# firewall-cmd --reload success
以後Windows就能夠在瀏覽器中查看了,結果以下:
固然也可使用Linux的elinks(須要安裝),固然elinks只能簡單查看
[root@youxi1 ~]# yum -y install elinks
(4).httpd主配置文件經常使用參數說明(2.4版本)
yum安裝的httpd主要配置文件是/etc/httpd/conf/httpd.conf。其中比較經常使用的參數以下:
ServerRoot "/etc/httpd" //httpd服務的根目錄 Timeout 60 //超時時間,默認60秒。默認配置文件中沒有,需手動添加 Listen 80 //監聽的IP和端口,完整格式爲[IP]:[port],IP省略則監聽全部本地IP Include conf.modules.d/*.conf //conf.modules.d目錄下的全部.conf文件都生效,該相對路徑以httpd服務的根目錄爲參照 User apache //以什麼用戶運行 Group apache //以什麼組運行 ServerAdmin root@localhost //設置管理員email地址 #ServerName www.example.com:80 //服務區用於標識自身的主機名和端口號 DocumentRoot "/var/www/html" //默認的主目錄,至少存在一個目錄設置相同的<Directory "[dir]">,不然將使用默認參數 <Directory "/var/www/html"> //一旦與DocumentRoot設置的目錄相同,將使用內部設置的參數 Options Indexes FollowSymLinks //當目錄沒有默認首頁時,容許瀏覽目錄結構。爲了安全建議禁止瀏覽目錄結構 AllowOverride None //設置爲None則忽略.htaccess Require all granted //支持全部訪問,Require all denied表示拒絕全部訪問 </Directory> <IfModule dir_module> DirectoryIndex index.html //設置目錄默認首頁,我沒改index.php也運行起來了,估計默認index都被掃描了 </IfModule> LogLevel warn //日誌等級 AddDefaultCharset UTF-8 //支持的編碼 IncludeOptional conf.d/*.conf //conf.d目錄下的全部.conf文件也屬於有效配置文件,該相對路徑以httpd服務的根目錄爲參照
與Require all granted相關的受權容器詳細查看:http://httpd.apache.org/docs/2.4/mod/mod_authz_core.html#require
Require all granted //容許全部 Require all denied //拒絕全部 Require method http-method [http-method] ... //只容許特定的HTTP方法 Require user userid [ userid ] ... //只容許特定用戶 Require group group-name [group-name] ... //只容許特定用戶組 Require valid-user //只容許有效用戶
另特別說明:
<RequireALL> Require ip 192.168.5.1 //只容許指定ip訪問,空格分隔 Require not ip 192.168.5.1 //不容許指定ip訪問,空格分隔 </RequireALL>
有Require ip能夠沒有Require not ip。但有Require not ip就必須有Require ip,不然httpd服務報錯。
(5).修改配置文件第一次嘗試
目標:設置超時時間爲60s,監聽端口9988,郵箱改一下。默認主目錄修改成/var/www/html/test,默認首頁爲index_test.html。
修改配置文件/etc/httpd/conf/httpd.conf的如下參數:
timeout 60 //添加 Listen 9988 //修改 ServerAdmin youxi@163.com //修改,我這裏隨便寫了 ServerName 192.168.5.101:9988 //能夠添加,能夠去除註釋後修改 DocumentRoot "/var/www/html/test" //修改 <Directory "/var/www/html/test"> //能夠修改,能夠新建 Options Indexes FollowSymLinks //若是沒有默認首頁,能夠訪問目錄結構 AllowOverride None Require all granted //容許全部人訪問 </Directory> <IfModule dir_module> DirectoryIndex index_test.html //修改 </IfModule>
接着取消默認的welcome頁面,註釋掉/etc/httpd/conf.d/welcome.conf配置文件的全部內容。
而後建立/var/www/html/test目錄和目錄下的首頁index_test.html
[root@youxi1 ~]# mkdir -p /var/www/html/test [root@youxi1 ~]# vim /var/www/html/test/index_test.html welcome to /var/www/html/test/index_test.html
從新啓動httpd服務
[root@youxi1 ~]# systemctl restart httpd.service
修改防火牆端口設置
[root@youxi1 ~]# firewall-cmd --permanent --zone=public --remove-port=80/tcp success [root@youxi1 ~]# firewall-cmd --permanent --zone=public --add-port=9988/tcp success [root@youxi1 ~]# firewall-cmd --reload success [root@youxi1 ~]# firewall-cmd --zone=public --list-ports 9988/tcp
最後查看了
(6).修改配置文件第二次嘗試
目標:限制訪問IP,不容許Windows訪問
修改配置文件/etc/httpd/conf/httpd.conf的如下參數:
<Directory "/var/www/html/test"> //能夠修改,能夠新建 Options Indexes FollowSymLinks //若是沒有默認首頁,能夠訪問目錄結構 AllowOverride None # Require all granted //註釋掉 <RequireALL> //添加 Require ip 192.168.5.1 192.168.5.131 //只容許指定的IP訪問 Require not ip 192.168.5.102 //不容許指定的IP訪問 </RequireALL> </Directory>
重啓httpd服務
[root@youxi1 ~]# systemctl restart httpd.service
Windows(192.168.5.1)瀏覽器查看
youxi2(192.168.5.102)使用elinks查看
又加了一臺CentOS7(192.168.5.131),桌面系統
(7).修改配置文件第三次嘗試
目標:引用主目錄外的目錄
修改配置文件/etc/httpd/conf/httpd.conf的如下參數:
Alias /test2/ "/test2/" //添加別名 <Directory "/test2/"> //添加別名的參數 Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory>
注意:<Directory "[dir]"></Directory>間的參數互不影響。
建立/test2/目錄和測試文件與目錄
[root@youxi1 ~]# mkdir /test2 [root@youxi1 ~]# touch /test2/File{1..5} [root@youxi1 ~]# mkdir /test2/Dir{1..5} [root@youxi1 ~]# ls /test2/ Dir1 Dir2 Dir3 Dir4 Dir5 File1 File2 File3 File4 File5
接着重啓httpd服務
[root@youxi1 ~]# systemctl restart httpd.service
Windows(192.168.5.1)瀏覽器訪問
youxi2(192.168.5.102)使用elinks訪問
(8).修改配置文件第四次嘗試
目標:禁止查看目錄結構,並使用用戶登陸
修改配置文件/etc/httpd/conf/httpd.conf的如下參數:
Alias /test2/ "/test2/" <Directory "/test2/"> # Options Indexes FollowSymLinks //註釋掉,禁止查看目錄結構 AllowOverride None # Require all granted //註釋掉 authtype basic //指定認證類型爲basic authname "my web site" //認證信息 authuserfile /etc/httpd/conf/user.passwd //指定包含用戶名和密碼的文件 require valid-user //只容許有效用戶登陸 </Directory>
生成存放文件和密碼的文件
[root@youxi1 ~]# htpasswd -cm /etc/httpd/conf/user.passwd youxi1 New password: Re-type new password: Adding password for user youxi1 [root@youxi1 ~]# htpasswd -m /etc/httpd/conf/user.passwd youxi2 New password: Re-type new password: Adding password for user youxi2 [root@youxi1 ~]# cat /etc/httpd/conf/user.passwd youxi1:$apr1$7AIP0HXy$6wudl4S6Rybz7.T3S8BBv. youxi2:$apr1$JHX3ZGwB$e5AmRlHr3DN3TUejBOsZ8/
注意:htpasswd命令-c選項是建立一個新的文件,因此除第一次使用外都不能使用,不然會覆蓋文件內容。-m選項是使用MD5加密方法。
重啓httpd服務
[root@youxi1 ~]# systemctl restart httpd.service
Windows(192.168.5.1)瀏覽器登陸
登陸後(沒有默認頁面,也不能查看目錄結構)