1-APACHE創建WEB服務器


1-APACHE創建WEB服務器

課程目標:

  • 了解常見的WEB服務器
  • 熟悉apache服務的基本配置
  • 掌握apache服務的虛擬主機配置
  • 熟悉apache的訪問控制
  • 掌握MySQL的安裝

一、WEB服務概述

web服務是我們日常使用最多的服務,所有的網站都是以web服務的形式為我們呈現

1.WEB服務的特點

  • web服務分為客戶端和服務端

  • web客戶端通常是我們所使用的瀏覽器(IE/Chrome/Firefox/Safari或者命令行瀏覽器等)

  • web服務端就是我們所訪問的網站提供的web服務器

    • 常見的web服務器程序有:

      Apache/Nginx/Tengine/Lighttpd/IIS等,不同的web服務針對不同的系統平台各自有優缺點

2.HTTP協議

  • web服務端與客戶端是通過HTTP協議(HyperText Tranfer Protocol)超文本傳輸協議進行交互

  • Apache或Nginx都只支持靜態頁面的解析

3.靜態頁面和動態頁面

  • HTML語言

    • HTML超文本標記語言(HyperText Markup Language),是絕大多數網頁使用的語言,幾乎所有的網頁都是以HTML作為源代碼,通過瀏覽器解釋后展現出來

    • HTML有固定語法,用來存儲網頁數據,定義不同元素,如文字、標題、圖片等,現在的網頁都用CSS語言來存儲網頁的表現形式

    • 現代主流的網頁設計架構:

      內容存儲:HTML

      網頁架構:div

      網頁樣式:css

    • HTML形式的頁面我們稱之為靜態頁面,包含一些圖片,文本,表格等

  • 動態語言

    • 網站使用特定的語言編寫的一些程序,在用戶訪問的時候基於不同的條件生成不同的HTML代碼返回給用戶瀏覽器,這樣就實現網頁的動態化
    • 常見的動態語言:.php .jsp .cgi .asp python等

4.WEB服務的中間件

  • php:PHP-FPM、HHVM(HipHop Virtual Machine)
  • jsp:Tomcat、JBOSS、Resin、IBM WebSphere

5.常見的WEB組合方式

  • LAMP(Linux+Apache+MySQL+PHP) //php作為Apache的模塊
  • LNMP(Linux+Nginx+MySQL+PHP) //php-fpm進程(服務)
  • Nginx/Apache +Tomcat

二、Apache的介紹

Apache是著名的web服務器軟件,開源,由apche軟件基金會負責管理開發。

  • Apache服務特點:
    • 開放源碼
    • 跨平台
    • 支持多種編程語言
    • 采用模塊化設計
    • 安全穩定
  • 工作原理

  • Apache的官網和手冊

    • www.apache.org
    • 英文手冊:需要安裝
    httpd-manual.noarch		//安裝后啟動服務就可以本地查看
    yum -y install httpd-manual.noarch
    service httpd restart
    查看官方手冊
    IE:httpd:/10.1.1.2/manual
    
  • Apache的軟件包:httpd

三、開胃菜—搭建簡易WEB服務

1.關閉防火牆和selinux
2.配置yum源
3.軟件三步曲(查看安裝|確認成功|查看軟件的文件列表)
4.了解配置文件	man 5 xxx.conf
5.根據需求修改配置文件來完成服務搭建
6.重啟服務,開機自啟
7.測試驗證

需求1:訪問一個靜態頁面,內容:hello world 五一快樂!

環境:web-server:10.1.1.2

步驟:
1.略
2.略
3.
/etc/httpd
/etc/httpd/conf		//主配置文件的主目錄
/etc/httpd/conf.d	//子配置文件的目錄
/etc/httpd/conf.d/README
/etc/httpd/conf.d/welcome.conf	//歡迎頁面
/etc/httpd/conf/httpd.conf	//主配置文件
/etc/httpd/conf/magic	
/etc/httpd/logs		//日志	/var/log/httpd/  硬鏈接	同一個文件	inode號一樣
/etc/httpd/modules
/etc/httpd/run		//	硬鏈接	//var/run/httpd/
/etc/logrotate.d/httpd		//日志輪轉
/etc/rc.d/init.d/htcacheclean	
/etc/rc.d/init.d/httpd		//服務啟動腳本
/etc/sysconfig/htcacheclean
/etc/sysconfig/httpd

/usr/sbin/apachectl
/usr/sbin/htcacheclean
/usr/sbin/httpd

/var/log/httpd
/var/run/httpd
/var/www		//apache服務的數據目錄
/var/www/html	//apache服務的靜態頁面存放路徑
/var/www/error/HTTP_NOT_FOUND.html.var	404
/var/www/error/HTTP_FORBIDDEN.html.var	403


互為硬鏈接,同時改變
[root@web-server ~]# ll -id /var/log/httpd/
267919 drwx------. 2 root root 4096 Apr 23 18:52 /var/log/httpd/
[root@web-server ~]# ll -id /etc/httpd/logs/
267919 drwx------. 2 root root 4096 Apr 23 18:52 /etc/httpd/logs/

4.配置文件
serverRoot	"/etc/httpd"	//服務主目錄
Listen	80					//監聽端口
IncludeOptional	conf.d/*.conf	//包含conf.d下的*.conf文件
User apache					//運行Apache的用戶
Group apache				//允許Apache的用戶組
DirectoryIndex index.html index.php		//設置默認主頁
DocumentRoot		/var/www/html/		//站點默認數據主目錄

<Directory />		//系統的根目錄授權
	Option FollowSymLinks		//支持軟連接
	AllowOverride	None		//不支持 .htaccess訪問列表 .htaccess文件提供了針對每個目錄改變配置的方法
</Directory>

RHEL6:
<Directory "/var/www/html">	//授權
    Options Indexes FollowSymLinks	//支持索引	支持軟連接
    AllowOverride None				//不支持	.htaccess 訪問列表
    Order allow,deny				//排序	先允許再拒絕
    Allow from all					//允許all
</Directory>

RHEL7:
<Directory "/var/www/html">
    AllowOverride None
	Require all granted		//允許所有人訪問
</Directory>
擴展:配置進程和線程	針對apache2.2 針對面試
# perfork MPM		//進程模式
<IfModule prefork.c>
StartServers		10		//初始建立的進程數
MinSpareServers	10			//最小空閑的進程數
MaxSpareServers	15			//最大空閑的進程數
ServerLimit		2000		//最大啟動的進程數	默認256
MaxClients		2000		//最大並發連接數	 默認256
MaxRequestsPerChid	4000	//每個子進程在其生命周期內允許響應的最大請求數,0不限制
</IfModule>

# worker MPM		//線程模式
<IfModule worker.c>			
StartServer			2		//初始創建的進程數
ThreadPerChild	50			//每個進程建立的線程數
MinSpareThreads	100			//最小空閑的線程數
MaxSpareThreads	200			//最大空閑的線程數
MaxClients		2000		//最大的並發訪問量(線程)
MaxRequestsPerChild	0		//每個子進程在其生命周期內允許響應的最大請求數,0不限制
</IfModule>
5.修改配置文件
創建一個首頁文件
echo "hello world 五一快樂" > /var/www/html/index.html
6.啟動服務
7.測試驗證

四、更改默認數據根目錄

mkdir /webserver
vim /etc/httpd/conf/httpd.conf
DocumentRoot "/webserver"
<Directory "/webserver/">
修改這兩處后重啟服務

#ServerName www.example.com:80	265行
ServerName 10.1.1.2:80		//解決下面重啟出現的問題

[root@web-server conf]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd: httpd: apr_sockaddr_info_get() failed for web-server
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
                                                           [  OK  ]
[root@web-server conf]# vim /etc/hosts
10.1.1.2	web-server
[root@web-server conf]# vim httpd.conf
[root@web-server conf]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]

[root@web-server conf]# cp /var/www/html/index.html /webserver

五、Apache共享文件

Apache如何共享文件呢?

  • 軟連接方式
需求1:共享web-server服務端的/data目錄里面的所有文件
1.apache服務默認的情況下下到哪個目錄里找相應的文件
2.將本地的/data目錄做一個快捷方式(軟連接)到指定的目錄里

步驟:
1.創建/data共享目錄
[root@web-server ~]# mkdir /data/liu
[root@web-server ~]# ll /data
total 4
-rw-r--r-- 1 root root    0 Apr 28 14:10 file1
-rw-r--r-- 1 root root    0 Apr 28 14:10 file2
-rw-r--r-- 1 root root    0 Apr 28 14:10 file3
-rw-r--r-- 1 root root    0 Apr 28 14:10 file4
-rw-r--r-- 1 root root    0 Apr 28 14:10 file5
drwxr-xr-x 2 root root 4096 Apr 28 14:10 liu
[root@web-server ~]# ll /webserver/
total 4
-rw-r--r-- 1 root root 25 Apr 28 14:05 index.html
2.創建/data/的軟連接/webserver/share
[root@web-server ~]# ln -s /data/ /webserver/share
[root@web-server ~]# ll /webserver/
total 4
-rw-r--r-- 1 root root 25 Apr 28 14:05 index.html
lrwxrwxrwx 1 root root  6 Apr 28 14:13 share -> /data/	軟鏈接
3.測試驗證
http://10.1.1.2
出現首頁
http://10.1.1.2/share
出現目錄文件
結論:當默認數據目錄里有index.html的首頁文件時,優先找首頁文件
當沒有首頁文件時,會顯示測試頁
不管有沒有首頁文件,都不會直接找數據文件
[root@web-server ~]# mv /webserver/index.html /webserver/index.html.bak
兩種解決方式:
1)加路徑,如上http://10.1.1.2/share
2)刪掉或重命名測試頁

[root@web-server ~]# cd /etc/httpd/conf.d/
[root@web-server conf.d]# ll
total 16
-rw-r--r--  1 root root 295 Aug  2  2013 manual.conf
-rw-r--r--. 1 root root 118 May 20  2009 mod_dnssd.conf
-rw-r--r--. 1 root root 392 Aug 14  2013 README
-rw-r--r--. 1 root root 299 Aug  2  2013 welcome.conf
[root@web-server conf.d]# mv welcome.conf welcome.conf.bak
[root@web-server conf.d]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]

結果依次如下面三圖顯示

  • 別名方式
[root@web-server conf]# vim httpd.conf
[root@web-server conf]# service httpd restart
照貓畫虎:
Alias /icons/ "/var/www/icons/"

<Directory "/var/www/icons">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>
在配置文件末尾添加后重啟服務
Alias /test/ "/data/"	別名是/test/,下面兩圖區別

<Directory "/data">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

注意:test后面加一個斜杠/后可以訪問,別名對訪問有影響,必須和別名一致

課堂練習

1.共享/home目錄(用別名方式,別名/rhome)


Alias /rhome "/home/"

<Directory "/home">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>
重啟服務

1556433786601

測試驗證:http://10.1.1.2/rhome沒有文件顯示,因為/home目錄下的文件是700,其他人沒有權限,新建一個test目錄后顯示出來。

2.共享/tmp目錄(用軟鏈接方式)

[root@web-server conf]# ln -s /tmp/ /webserver/tmp
[root@web-server conf]# ll /webserver/
total 4
-rw-r--r-- 1 root root 25 Apr 28 14:05 index.html.bak
lrwxrwxrwx 1 root root  6 Apr 28 14:13 share -> /data/
lrwxrwxrwx 1 root root  5 Apr 28 14:47 tmp -> /tmp/
測試驗證
http://10.1.1.2/tmp

其他文件不顯示的原因是權限不足


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM