RedHat7的Apache配置介紹
這裡我們介紹yum安裝httpd yum install -y httpd
[root@100 ~]# systemctl restart httpd [root@100 ~]# systemctl enable httpd ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service' [root@100 ~]# vim /etc/httpd/
安裝的版本介紹

image.png
備份一下配置檔案
[root@100 ~]# cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.bak
需要了解的知識塊
1、瞭解Apache的基本配置
2、配置動態頁面:CGI、wsgi、ssl
3、配置一個虛擬主機
4、配置https
1、瞭解Apache的基本配置
編輯Apache的配置檔案httpd.conf 路徑在/etc/httpd/conf/httpd.conf
預設的配置檔案路徑ServerRoot "/etc/httpd"
預設監聽的埠Listen 80
模組存放路徑[root@100 ~]# ls /etc/httpd/conf.modules.d/
所屬者和所屬組
User apache
Group apache
預設安裝的時候他會建立一個不可登入系統的使用者
apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
預設的管理員郵箱是ServerAdmin root@localhost
站點配置#ServerName ofollow,noindex">www.example.com:80 預設是禁用的
站點的目錄設定
<Directory "/var/www"> ------------<Directory "/var/www/html">
AllowOverride None ------------------Options Indexes FollowSymLinks
# Allow open access: -----------------AllowOverride None
Require all granted --------------------Require all granted
</Directory>----------------------------</Directory>
站點設定項:
(Options 裡的indexes是開啟開啟索引,FollowSymLinks是允許連結檔案--需要修改上下文chcon -R --reference=/var/www/html /new)
(AllowOverride None 是否允許單前目錄下的一個隱藏檔案.htaccess裡的配置覆蓋當前httpd.conf
的裡設定--用於http的認證
在網頁的根目錄下建立一個
[root@100 html]# cat /var/www/html/.htaccess
AuthType Basic
AuthName haha
AuthUserfile /etc/httpd/conf/.htpasswd
Require user tom
/etc/httpd/conf/.htpasswd這個檔案需要用
[root@100 html]# htpasswd -cm /etc/httpd/conf/.htpasswd tom ##-cm 建立用MD5
加密
New password:
Re-type new password:
Adding password for user tom
[root@100 html]#
修改後的AllowOverride AuthConfig 接下來訪問就要提示使用者密碼
)
(Require all granted 允許所有
Require all denied 拒絕所有
Require ip 192.168.1.1 允許這地址訪問
Require ip 192.168.1.0/24 這個端地址可以訪問
Require local
Require ip 192.168.1.1 192.168.2.2 這倆個地址可以訪問
)
站點的文件目錄DocumentRoot "/var/www/html"
設定檔案的許可權
<Files ".ht*">
Require all denied
</Files>
錯誤日誌ErrorLog "logs/error_log"
Alias 設定別名 # Alias /webpath /full/filesystem/path預設是禁用的--每設定一個目錄就需要加一個
<Directory "/mnt/html">
AllowOverride None
# Allow open access:
Require all granted
</Directory>
2、配置動態頁面:CGI、wsgi、ssl
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/" 這是CGI的指令碼路徑
[root@100 cgi-bin]# pwd
/var/www/cgi-bin
[root@100 cgi-bin]# cat *
#!/usr/bin/perl print "Content-Type: text/html; charset=UTF-8\n\n"; $time=localtime(); print "$time\n" #!/bin/bash echo "Content-Type: text/html; charset=UTF-8" echo date +'%F %T' hostname
[root@100 cgi-bin]#
瀏覽器訪問 http://xxxxxxxxx/cgi-bin/aa.sh 實現的效果就是獲取本地時間
wsgi是python實現的一種動態頁面功能預設沒有按照
[root@100 cgi-bin]# yum install -y mod_wsgi
在/etc/httpd/conf/httpd.conf配置檔案裡修改
253 ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
254
255 </IfModule>
256
257 WSGIScriptAlias /wsgi-bin/ "/var/www/cgi-bin/"
瀏覽器訪問測試 http://xxxxxx/wsgi-bin/aa.wsgi 實現的效果就是顯示本地時間
[root@100 wsgi-bin]# cat aa.wsgi
#!/usr/bin/env python import time def application (environ, start_response): a = time.ctime(time.time()) response_body = 'This Dynamic WSGI Page Was Generated at: \n%s\n'%a status = '200 OK' response_headers = [('Content-Type', 'text/plain'), ('Content-Length', '1'), ('Content-Length',str(len(response_body)))] start_response(status, response_headers) return [response_body]
SSI 設定動態頁面是需要在配置檔案裡設定
在站點目錄這裡新增includes
Options Indexes FollowSymLinks Includes##include包含
ssi設定的頁面預設是*.shtml
Vim /var/www/html/index.shtml
<h2>
<span style="background-color:#E56600;">helloworld</span>
</h2>
<p>
當前的使用者是: <!--#exec cmd="whoami" --> 當前的時間是:<!--#echo var="DATE_LOCAL" --> 訪問者的ip是:<!--#echo var="REMOTE_ADDR"-->
</p>
~
##呼叫剛才的cgi指令碼
注意點:
站點根目錄下的預設是訪問index.html怎麼把它設定成index.shtml呢?
就需要用到地址重寫功能
在http的配置檔案裡追加
362 RewriteEngine on
363 RewriteRule ^/?$ /index.shtml [R]
他會使你的瀏覽器預設重定向站點的shtml裡
3、虛擬主機的配置
預設http的配置檔案在這裡
# Load config files in the "/etc/httpd/conf.d" directory, if any. IncludeOptional conf.d/*.conf
這裡我們寫一個基於一個IP解析倆個不同站點的案例(基於主機名的虛擬主機)

image.png
這裡複製一個模板檔案
[root@100 ~]# cp /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf /etc/httpd/conf.d/vhosts.conf [root@100 conf.d]# echo zzz > /var/www/html/index.html;echo cc /cc/index.html [root@100 conf.d]# systemctl restart httpd [root@100 conf.d]# cat vhosts.conf <VirtualHost *:80> DocumentRoot /cc ServerName www.cc.com ErrorLog "/var/log/httpd/www.cc.com-error_log" CustomLog "/var/log/httpd/dummy-www.cc.com.com-access_log" common </VirtualHost> <Directory "/cc"> AllowOverride None # Allow open access: Require all granted </Directory> <VirtualHost *:80> DocumentRoot /var/www/html ServerName www.zzz.com ErrorLog "/var/log/httpd/www.zzz.com-error_log" CustomLog "/var/log/httpd/dummy-www.zzz.com.com-access_log" common </VirtualHost> <Directory "/var/www/html"> AllowOverride None # Allow open access: Require all granted </Directory> cho [root@100 conf.d]# chcon -R --reference=/var/www/html /cc#更改上下文
(如果是基於ip的虛擬主機只需修改你的dns解析即可)
基於埠的虛擬主機
[root@100 conf.d]# cat vhosts.conf <VirtualHost *:801> DocumentRoot /cc ServerName www.cc.com ErrorLog "/var/log/httpd/www.cc.com-error_log" CustomLog "/var/log/httpd/dummy-www.cc.com.com-access_log" common </VirtualHost> <Directory "/cc"> AllowOverride None # Allow open access: Require all granted </Directory> <VirtualHost *:802> DocumentRoot /var/www/html ServerName www.zzz.com ErrorLog "/var/log/httpd/www.zzz.com-error_log" CustomLog "/var/log/httpd/dummy-www.zzz.com.com-access_log" common </VirtualHost> <Directory "/var/www/html"> AllowOverride None # Allow open access: Require all granted </Directory> [root@100 conf.d]# cat /etc/httpd/conf/httpd.conf #Listen 12.34.56.78:80 Listen 80 Listen 801 Listen 802
有探討的在下方留言
--END--