一、安裝、啟動與停止Apache服務(wù)
1、安裝Apache服務(wù)軟件
# yum install -y httpd
2、啟動Apache服務(wù)
# systemctl start httpd
systemctl的其它選項(xiàng)
start:啟動
stop:停止
restart:重啟
enable:設(shè)置開機(jī)自動啟動
disable:禁用服務(wù),從開機(jī)啟動項(xiàng)把服務(wù)移除
status:查看服務(wù)狀態(tài)
3、關(guān)閉selinux
#setenforce 0
4、關(guān)閉防火墻
# systemctl stop firewalld
5、測試
(1)安裝火狐瀏覽器
#yum install -y firefox
(2)打開網(wǎng)頁
#firefox http://127.0.0.1
出現(xiàn)以下頁面(Apache默認(rèn)首頁)即表示 httpd 服務(wù)啟動


二、認(rèn)識Apache服務(wù)器的配置文件
配置文件的名稱 |
存放位置 |
主配置文件 |
/etc/httpd/conf/httpd.conf |
網(wǎng)站數(shù)據(jù)目錄 |
/var/www/html |
|
默認(rèn)網(wǎng)站首頁文件/var/www/html/index.html |
虛擬主機(jī)目錄 |
/etc/httpd/conf.d/vhost.conf vhost.conf這個文件默認(rèn)是沒有,需要新建 |
訪問日志 |
/var/log/httpd/access_log |
錯誤日志 |
/var/log/httpd/error_log |
測試:修改默認(rèn)首頁的內(nèi)容,然后重新打開
# echo "我來測試默認(rèn)首頁" > /var/www/html/index.html

重新打開網(wǎng)頁
發(fā)現(xiàn)內(nèi)容已經(jīng)改了

主配置文件/etc/httpd/conf/httpd.conf
1、先備份原配置文件

2、為了方便查看,從備份文件中反選不包含“#”的行,覆蓋輸出到原文件

3、打開配置文件
常用配置說明:
ServerRoot "/etc/httpd" #服務(wù)目錄
Listen 80 #監(jiān)聽端口
User apache #運(yùn)行服務(wù)的用戶
Group apache #運(yùn)行服務(wù)的用戶組
ServerAdmin root@localhost #管理員郵箱
DocumentRoot "/var/www/html" #網(wǎng)站數(shù)據(jù)目錄(網(wǎng)頁文件)
#/var/www的權(quán)限設(shè)置
<Directory "/var/www">
AllowOverride None
Require all granted
</Directory>
#/var/www/html的權(quán)限設(shè)置
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
#<IfModule>會判斷 dir_module 是否載入,如果載入則會執(zhí)行 DirectoryIndex index.html 指令
<IfModule dir_module>
DirectoryIndex index.html #默認(rèn)的索引頁頁面
</IfModule>
#錯誤日志文件路徑
ErrorLog "logs/error_log"
三、配置虛擬主機(jī)
需要在一臺web物理服務(wù)器上,配置多個網(wǎng)站
三種方法:基于不同的IP地址;基于不同的端口號;基于不同的域名(主機(jī)號)。
方法一:基于不同的IP地址
要求
IP地址 |
網(wǎng)站數(shù)據(jù)目錄 |
默認(rèn)網(wǎng)頁內(nèi)容 |
192.168.128.77 |
/var/www/ip77 |
l am from 192.168.128.77 |
192.168.128.78 |
/var/www/ip78 |
l am from 192.168.128.78 |
1、添加第二個IP地址:192.168.128.78

保存退出,重啟網(wǎng)絡(luò)
#systemctl restart network
2、創(chuàng)建網(wǎng)站數(shù)據(jù)目錄

3、創(chuàng)建默認(rèn)網(wǎng)頁文件,并寫入內(nèi)容

4、創(chuàng)建虛擬主機(jī)配置文件,并編輯內(nèi)容
#vi /etc/httpd/conf.d./vhost.conf

5、重啟Apache服務(wù)
#systemctl restart httpd
6、測試


方法二:基于不同的端口號
IP地址 |
端口號 |
網(wǎng)站數(shù)據(jù)目錄 |
默認(rèn)網(wǎng)頁內(nèi)容 |
192.168.128.77 |
8080 |
/var/www/p8080 |
l am from port 8080 |
192.168.128.77 |
8088 |
/var/www/p8088 |
l am from port 8088 |
1、創(chuàng)建網(wǎng)站數(shù)據(jù)目錄
#mkdir /var/www/p8080 /var/www/p8088
2、創(chuàng)建默認(rèn)網(wǎng)頁文件,并寫入內(nèi)容

3、修改文件,把原來的內(nèi)容刪掉
#vi /etc/httpd/conf.d/vhost.conf

4、修改虛擬主機(jī)配置文件
#vi /etc/httpd/conf/httpd.conf

5、重啟Apache服務(wù)
#systemctl restart httpd
6、測試

方法三:基于不同的域名(主機(jī)號)
IP地址 |
域名 |
網(wǎng)站數(shù)據(jù)目錄 |
默認(rèn)網(wǎng)頁內(nèi)容 |
192.168.128.77 |
www1.lcvc.com |
/var/www/www111 |
l am from www111 |
192.168.128.77 |
www2.lcvc.com |
/var/www/www222 |
l am from www222 |
1、創(chuàng)建網(wǎng)站數(shù)據(jù)目錄
#mkdir /var/www/www111 /var/www/www222
2、創(chuàng)建默認(rèn)網(wǎng)頁文件,并寫入內(nèi)容

3、修改文件,把原來的內(nèi)容刪掉
#vi /etc/httpd/conf.d/vhost.conf

4、修改虛擬主機(jī)配置文件
#vi /etc/httpd/conf/httpd.conf

5、重啟Apache服務(wù)
#systemctl restart httpd
6、測試
(1)修改/etc/hosts文件,添加以下域名解析信息
#vi /etc/hosts文章來源:http://www.zghlxwxcb.cn/news/detail-492707.html

(2)測試文章來源地址http://www.zghlxwxcb.cn/news/detail-492707.html

到了這里,關(guān)于配置與管理Apache服務(wù)器(linux)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!