httpd是著名的開源項目,有ASF組織,是世界上用的最多的web服務器程序,今天咱們以最新的apache 2.4.6爲例,解釋httpd的安裝與應用web
1.http 源碼安裝數據庫
2.端口監聽apache
3.配置選用的MPMvim
4.Keep-alived支持centos
5.訪問控制bash
6.日誌服務器
7.虛擬主機ide
8.https網站
1.httpd源碼安裝ui
[root@stu11 ~]# yum groupinstall"Development tools" [root@stu11 ~]# yum groupinstall"Server Platform Development"
對於centos 6.4 安裝新版本的http2.4.6還須要三個包,一個是APR,APR-utils,pcre-devel,前面兩個對於新版本的http版本不夠,因此要下載新的版本去安裝,而對於後面的只須要yum安裝便可。
[root@stu11 apr-1.4.6]# ./configure--prefix=/usr/local/apr [root@stu11 apr-1.4.6]# make &&make install [root@stu11 apr-util-1.5.2]# ./configure--prefix=/usr/local/apr-utils --with-apr=/usr/local/apr [root@stu11 apr-util-1.5.2]# make&& make install [root@stu11 httpd-2.4.6]# yum installpcre-devel
OK,正式的去安裝
[root@stu11 ~]# tar xf httpd-2.4.6.tar.bz2 [root@stu11 ~]# cd httpd-2.4.6 [root@stu11 httpd-2.4.6]# ./configure--prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl--enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr--with-apr-util=/usr/local/apr-utils --enable-modules=most --enable-mpms-shared=all--with-mpm=event
--enable-so |
支持動態連接庫 DSO |
--enable-cgi |
支持通用網關界面 |
--enable-rewrite |
支持重寫 |
--enable-zlib |
支持zlib壓縮 |
--enable-modules=most |
把大部模塊編譯進httpd |
--enable-mpms-shared=all |
支持全部的MPM |
--with-mpm=event |
默認啓用事件驅動 |
[root@stu11 httpd-2.4.6]# make &&make install
修改環境變量使直接運行apachectl 就可運行,而且把所依賴的庫文件連接過去
[root@stu11 httpd-2.4.6]# echo "exportPATH=/usr/local/apache/bin:$PATH" > /etc/profile.d/apache.sh [root@stu11 httpd-2.4.6]# ./etc/profile.d/apache.sh [root@stu11 httpd-2.4.6]# ln -s/usr/local/apache/include/ /usr/include/
複製之前的啓動文件,而且修改文件
[root@stu11 ~]# cd /etc/rc.d/init.d/ [root@stu11 init.d]# cp httpd httpd24 apachectl=/usr/local/apache/bin/apachectl//修改apachectl的位置 httpd=${HTTPD-/usr/local/apache/bin/httpd} prog=httpd pidfile=${PIDFILE-/usr/local/apache/logs/httpd.pid}修改pid文件的位置
2.配置監聽端口
先備份一遍
配置文件在 /etc/httpd24/httpd.conf中配置文件
Listen [IP:]port Listen 80 說明監聽全部IP的的80端口 Listen 172.16.11.1:8080 監聽172.16.11.1的8080端口
3.配置選用的MPM
MPM即多道處理模塊,Apachehttpd2.4.6支持3中MPM,即prefork,worker還有event.
在配置文件中去掉前面的註釋符
Include /etc/httpd24/extra/httpd-mpm.conf
下面的一句中選一個,而後註釋掉其餘
LoadModule mpm_event_module modules/mod_mpm_event.so LoadModule mpm_worker_module modules/mod_mpm_worker.so LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
定義MPM配置文件的路徑在/etc/httpd24/extra/httpd-mpm.conf中
<IfModule mpm_prefork_module> //若是啓用的模塊式prefork StartServers 5 //啓動時候創建的子進程的個數 MinSpareServers 5 //空閒子進程的最大數量 MaxSpareServers 10 //空閒子進程的最小個數 MaxRequestWorkers 250 //同時接入的最大請數量,求至關httpd2.2中的MaxClients MaxConnectionsPerChild 0 </IfModule> <IfModule mpm_worker_module> StartServers 3 MinSpareThreads 75 MaxSpareThreads 250 ThreadsPerChild 25 MaxRequestWorkers 400 MaxConnectionsPerChild 0 </IfModule> <IfModule mpm_event_module> StartServers 3 MinSpareThreads 75 MaxSpareThreads 250 ThreadsPerChild 25 MaxRequestWorkers 400 MaxConnectionsPerChild 0 </IfModule>
4.Keep-alived支持
咱們首先說明爲何要使用KeepAlive(長鏈接),通常一個頁面會有不少個資源,好比說有70個,咱們若是沒有,若是每次請求資源都要創建鏈接,發送請求,等待響應,接受數據,那用戶的體驗是很是差的,爲了解決這個問題,就有了長鏈接,咱們的KeepAlived的配置文件在/extra/httpd-default.conf
首先在httpd.conf中啓用Include /etc/httpd24/extra/httpd-default.conf
KeepAlive On //打開長鏈接 MaxKeepAliveRequests 100 //每次請求最大的資源數目 KeepAliveTimeout 5 //每次最多響應時長
5. 配置日誌
咱們看怎麼定義日誌存放,日誌通常分爲訪問訪問日誌(通常須要自定義格式的)還有錯誤日誌
啓用日誌功能,去掉LoadModule log_config_module modules/mod_log_config.so前的註釋符號,果啓用了log_config_module則啓用一下配置,其中有一句爲
LogFormat "%h %l %u %t\"%r\" %>s %b \"%{Referer}i\"\"%{User-Agent}i\"" combined Combined 是一種日誌格式 <VirtualHost 172.16.11.1:80> ServerAdmin cyb@a.com DocumentRoot "/web/a.com" ServerName www.a.com CustomLog /var/log/access.log combined//定義訪問日誌在/var/log/access.log中,格式爲combined ErrorLog/var/log/err.log//定義錯誤日誌的/var/log/err.log <Directory "/web/a.com"> Options None AllowOverride None <RequireALL> Require all granted Require not ip172.16.254.83 </RequireALL> </Directory> </VirtualHost>
6,訪問控制
訪問控制能夠分爲兩類一類是基於IP的訪問控制,另一類是基於用戶的訪問控制
咱們先看基於用戶的訪問控制
咱們在Direcotry容器中增長新的內容
AuthTypeBasic//認證方式有兩種,一種是BASIC認證另一種digest數據庫認證,咱們使用basic認證 AuthUserFile/etc/httpd24/.htpasswd//認證文件所在的位置,事先咱們要新建一個叫.htpasswd的文件 Requirevalid-user//要求有效的用戶去驗證
咱們使用htpasswd命令去創建用戶名和密碼
[root@stu11extra]# htpasswd -c -m /etc/httpd24/.htpasswd test New password: Re-type newpassword: Adding passwordfor user test
添加第二個用戶名的時候去掉-c(建立),會覆蓋之前的用戶的
基於IP的訪問控制在httpd2.2和httpd2.4中略有不一樣
咱們看httpd2.4中的基於IP的訪問控制
咱們只容許192.168.0.0/16的主機訪問,咱們的客戶機是172.16.251.83,因此就會出現以下字樣。
Forbidden
You don't havepermission to access / on this server.
容許除了172.16.254.83訪問,其餘主機均可以訪問,咱們在虛擬主機了設定容器RequireAll
<RequireALL>
Require all granted
Require not ip 172.16.254.83
</RequireALL>
這樣咱們一樣看到了上面的Forbidden的字樣。
7.啓用虛擬主機
咱們新建4個虛擬主機分別爲,分別基於端口,IP地址,和FQDN的不一樣
FQDN |
IP |
端口 |
172.16.11.1 |
80 |
|
172.16.11.1 |
8080 |
|
172.16.11.11 |
80 |
首先註釋掉配置文件的DocumentRoot
#DocumentRoot"/usr/local/apache/htdocs"
去掉Vitrual Host #啓用配置文件
Include /etc/httpd24/extra/httpd-vhosts.conf
配置虛擬主機文件
[root@stu11htdocs]# vim /etc/httpd24/extra/httpd-vhosts.conf
新建Virtual Host
<VirtualHost 172.16.11.1:80>//指定主機所監聽的套接字 ServerAdmin cyb@a.com//出錯時聯繫的管理員郵箱 DocumentRoot "/web/a.com"//網站的根目錄 ServerName www.a.com//網站的名字 <Directory "/web/a.com">// Options None AllowOverride None Require all granted//容許全部主機訪問 </Directory> </VirtualHost> 使用讓虛擬主機監聽在172.16.11.1的8080端口上 <VirtualHost 172.16.11.1:8080> ServerAdmin cyb@b.com DocumentRoot "/web/b.com" ServerName www.b.com <Directory "/web/b.com"> Options None AllowOverride None Require all granted </Directory> </VirtualHost> <VirtualHost 172.16.11.11:80> ServerAdmin cyb@c.com DocumentRoot "/web/c.com" ServerName www.c.com <Directory "/web/c.com"> Options None AllowOverride None Require all granted </Directory> </VirtualHost>
上面的配置就是分別實現事先了不一樣的FQDN,不一樣的端口和IP地址之上配置虛擬主機。在生產環境中不一樣的FQDN去實現虛擬主機是最多見的。
8,https的實現
咱們要實現https,首先,咱們事先有一個CA,很明顯,咱們有一個,咱們只須要給咱們的http頒發一個證書就能夠了(我修改了這臺主機的名字)
首先咱們生成一個私鑰
[root@www CA]# (umask 077;openssl genrsa-out /root/www.pri 2048) 生成請求文件 [root@www CA]# openssl req -new -key/root/www.pri -out /root/www.csr You are about to be asked to enterinformation that will be incorporated into your certificate request. What you are about to enter is what iscalled a Distinguished Name or a DN. There are quite a few fields but you canleave some blank For some fields there will be a defaultvalue, If you enter '.', the field will be leftblank. ----- Country Name (2 letter code) [XX]:CN State or Province Name (full name)[]:shanxi Locality Name (eg, city) [DefaultCity]:houma Organization Name (eg, company) [DefaultCompany Ltd]:a.com Organizational Unit Name (eg, section)[]:tec Common Name (eg, your name or your server'shostname) []:www.a.com Email Address []: Please enter the following 'extra'attributes to be sent with your certificate request A challenge password []: An optional company name []:
由CA去簽署證書
[root@www CA]# openssl ca -in /root/www.csr-out /root/www.crt Listen 443//監聽443端口 <VirtualHost 172.16.11.1:443> //新建一個虛擬主機 DocumentRoot "/web/a.com/" //根目錄 ServerName www.a.com:443 //主機名 ServerAdmin you@example.com ErrorLog"/usr/local/apache/logs/error_log" TransferLog"/usr/local/apache/logs/access_log" SSLEngine on//開啓SSL引擎 SSLCertificateFile"/etc/httpd24/www.crt" //SSL的證書文件 SSLCertificateKeyFile"/etc/httpd24/www.pri" //SSL的私鑰文件 <Directory "/web/a.com/">//這裏別忘了修改 SSLOptions +StdEnvVars AllowOverride None Require all granted//受權全部用戶訪問 </Directory> </VirtualHost>
客戶端主機下載,CA的證書,去驗證。