一、Apache服務器簡介
??Apache HTTP Server(簡稱Apache)是Apache軟件基金會的一個開放源碼的網頁服務器,可以在大多數計算機操作系統中運行,由于其多平臺和安全性被廣泛使用。Apache曾經是世界使用排名第一的Web服務器軟件(2019年4月后nginx第一)。它可以運行在幾乎所有廣泛使用的計算機平臺上。Apache 源于NCSAhttpd服務器,經過多次修改,成為世界上最流行的Web服務器軟件之一。Apache取自“a patchy server”的讀音,意思是充滿補丁的服務器,因為它是自由軟件,所以不斷有人來為它開發新的功能、新的特性、修改原來的缺陷。Apache的特點是簡單、速度快、性能穩定,并可做代理服務器來使用。當前最新穩定版是2.4.52,博文實驗環境:
- 操作系統:centos7.6
- Apache版本:2.4.6
二、YUM安裝Apache
??在另外一篇博文Linux之WEB服務器Apache httpd源碼編譯安裝中介紹了如何源碼編譯安裝Apache服務,如果對于版本沒有特別要求,centos環境下最簡單快捷的安裝方式還是yum安裝,centos7環境下yum安裝版本為2.4.6。
1、YUM安裝Apache
[root@s152 ~]# yum install -y httpd
2、查看版本
[root@s152 ~]# httpd -v
Server version: Apache/2.4.6 (CentOS)
Server built: May 30 2023 14:01:11
3、服務管理
#啟動服務
[root@s152 /]# systemctl start httpd
#停止服務
[root@s152 /]# systemctl stop httpd
#服務開機自啟動
[root@s152 /]# systemctl enable httpd
#查看服務狀態
[root@s152 /]# systemctl status httpd
#檢查配置文件
[root@s152 /]# httpd -t
Syntax OK
#重載配置文件,不重啟服務,如下三種方式都可以
[root@s152 /]# httpd -k graceful
[root@s152 /]# apachectl graceful
[root@s152 /]# systemctl reload httpd
三、常見配置參數說明
??如下是Apache服務器安裝完成后的默認配置文件,這里我們只針對其中常用的配置參數進行釋義說明。
1、默認httpd.conf配置
[root@s152 ~]# cat /etc/httpd/conf/httpd.conf |grep -Ev "^$|#"
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin root@localhost
<Directory />AllowOverride noneRequire all denied
</Directory>
DocumentRoot "/var/www/html"
<Directory "/var/www">AllowOverride NoneRequire all granted
</Directory>
<Directory "/var/www/html">Options Indexes FollowSymLinksAllowOverride NoneRequire all granted
</Directory>
<IfModule dir_module>DirectoryIndex index.html
</IfModule>
<Files ".ht*">Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combinedLogFormat "%h %l %u %t \"%r\" %>s %b" common<IfModule logio_module>LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio</IfModule>CustomLog "logs/access_log" combined
</IfModule>
<IfModule alias_module>ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
<Directory "/var/www/cgi-bin">AllowOverride NoneOptions NoneRequire all granted
</Directory>
<IfModule mime_module>TypesConfig /etc/mime.typesAddType application/x-compress .ZAddType application/x-gzip .gz .tgzAddType text/html .shtmlAddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf
2、常用參數釋義
參數 | 參數說明 | 配置示例 |
---|---|---|
ServerRoot | Apache服務器的根目錄 | ServerRoot “/etc/httpd” |
Listen | 指定Apache監聽的IP地址和端口 | Listen 80 |
DocumentRoot | 指定Web服務器的文檔根目錄,即網站文件存放的位置 | DocumentRoot “/var/www/html” |
Directory | 配置目錄的權限和特性 | <Directory “/var/www/html”> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> |
DirectoryIndex | 指定當訪問一個目錄時默認顯示的文件 | DirectoryIndex index.html、 |
AllowOverride | 指定是否允許使用.htaccess文件覆蓋目錄配置 | AllowOverride All |
LogLevel | 設置日志級別,用于記錄錯誤和警告信息 | LogLevel warn |
ErrorLog | 指定錯誤日志文件的路徑 | ErrorLog “/var/log/httpd/error_log” |
CustomLog | 指定訪問日志文件的路徑和格式 | CustomLog “/var/log/httpd/access_log” combined |
ServerSignature | 控制服務器生成的錯誤頁面中是否包含服務器的簽名信息 | ServerSignature Off |
KeepAlive | 啟用或禁用Keep-Alive功能,決定是否保持持久連接 | KeepAlive On |
Timeout | 設置服務器等待客戶端請求的超時時間 | Timeout 300 |
MaxClients | 限制同時連接到服務器的最大客戶端數 | MaxClients 150 |
IncludeOptional | Apache主配置文件中引入其他配置文件 | IncludeOptional conf.d/*.conf |
User | httpd服務運行用戶 | User apache |
Group | httpd服務所屬群組 | Group apache |
ServerName | 虛擬服務器主機名和端口,主機名可以是IP地址也可以是域名 | ServerName 192.168.0.152:80 |
四、服務配置舉例
??這里我們以部署猜拳游戲為例,介紹如何在Apache服務上部署服務。
1、創建一個虛擬主機配置文件
??進入/etc/httpd/conf.d/目錄下創建一個虛擬主機配置文件,主機名為mytest.com,對應監聽的80端口,如果需要更換其他端口需要在主文件中listen添加或者修改,這是與nginx不同的地方。
[root@s152 mytest]# cd /etc/httpd/conf.d/
[root@s152 conf.d]# cat test.conf
<VirtualHost *:80># 設置虛擬主機的域名ServerName mytest.comServerAlias www.mytest.com# 設置文檔根目錄DocumentRoot "/var/www/mytest"# 日志文件ErrorLog "/var/log/httpd/mytest_error_log"CustomLog "/var/log/httpd/mytest_access_log" combined# 目錄權限<Directory "/var/www/mytest">Options Indexes FollowSymLinksAllowOverride NoneRequire all granted</Directory># 可以添加其他定制的配置項,如重定向、代理等</VirtualHost>
2、將猜拳服務代碼打包上傳到Directory
??配置文件中虛擬服務目錄位于/var/www/mytest目錄下,我們創建該目錄后將軟件包上傳到該目錄下。
[root@s152 www]# mkdir mytest
[root@s152 www]# cd mytest/
[root@s152 mytest]# ll
總用量 40
drwxr-xr-x. 2 root root 100 11月 1 2022 caiquan
-rw-r–r–. 1 root root 38541 11月 29 15:33 caiquan.zip
3、重啟httpd服務
??使用httpd -t檢查配置文件,檢查無誤后我們重啟服務或者重新加載配置文件。
[root@s152 conf.d]# httpd -t
Syntax OK
[root@s152 conf.d]# systemctl restart httpd
4、修改hosts文件
??這里實驗用的域名非正式域名,我們需要在hosts文件添加自定義解析。
5、訪問驗證
??打開瀏覽器,通過域名和路徑就可以訪問我們的猜拳游戲內容啦!
五、QA
1、啟動報錯httpd: Could not reliably determine the server’s fully qualified domain name
- 報錯信息:httpd: Could not reliably determine the server’s fully qualified domain name
- 報錯原因:httpd.conf配置文件中未配置ServerName
- 解決方案:修改httpd.conf配置文件,添加ServerName = domain.com:80 參數配置
2、啟動報錯httpd (pid xxxxx) already running導致無法啟動
- 報錯信息:httpd (pid xxxxx) already running 和 httpd.service: control process exited, code=exited status=1
- 報錯原因:httpd服務未正常退出導致無法啟動
- 解決方案:執行pgrep -f httpd |xargs kill后再次啟動。