Apache服务器部署(1)
apache(web服務器)簡介:
???????? Apache是世界使用排名第一的Web服務器軟件。它可以運行在幾乎所有廣泛使用的計算機平臺上,其跨平臺和安全性被廣泛使用,是最流行的Web服務器端軟件之一。它快速、可靠并且可通過簡單的API擴充,將Perl/Python等解釋器編譯到服務中。
主配置目錄:/etc/httpd/conf
主配置文件:/etc/httpd/conf/httpd.conf
子配置目錄:/etc/httpd/conf.d
子配置文件:/etc/httpd/conf.d/*.conf默認發布目錄:/var/www/html
默認發布文件:index.html
默認端口:80
默認安全上下文:httpd_sys_content_t
程序開啟默認用戶:apache
apache 日志:/etc/httpd/logs/
環境:
rhel7.5?????? server1:172.25.1.1
關閉防火墻及selinux
apache服務器部署
1、配置yum源,安裝apache數據包
[root@server1 ~]# yum install -y httpd
2、關閉防火墻及selinux,開啟http服務
[root@server1 ~ ]# setenforce 0?????????????? //臨時關閉selinux
[root@server1 ~]# getenforce
3、查看監聽端口:??????????????????? //默認端口為80
4、簡單編寫一個網頁
[root@server1 ~]# cd /var/www/html/??????????? //切換到默認發布頁面
[root@server1 html]# vim index.html
去網頁輸入主機ip進行瀏覽
此時 ,可以看到剛才默認發布頁面中所寫的html網頁;這里因為默認端口為80,所以訪問時ip后面可以不用寫端口80
?
將發布端口修改為8080
[root@server1 html]# vim /etc/httpd/conf/httpd.conf
[root@server1 html]# systemctl restart httpd??????????? //重起服務
[root@server1 html]# netstat -antlp | grep httpd???????? //此時其端口已經被修改為8080
這時去網頁輸入主機ip進行瀏覽
發現不可訪問,但加上端口即可
默認發布目錄的更改
情況一:讓其顯示/westos/html下的html網頁
[root@server1 ~]# mkdir /westos/html -p?????????? //連續創建目錄,即想要發布目錄
[root@server1 ~]# cd /westos/html/??????????????????
[root@server1 html]# ls
[root@server1 html]# vim index.html????????????? //在其目錄下寫發布頁面
[root@server1 html]# vim /etc/httpd/conf/httpd.conf????????????? //修改配置文件,更改默認發布目錄及發布文件
#DocumentRoot "/var/www/html"???????????????? //注釋掉原本的默認發布目錄
?DocumentRoot "/westos/html"??????????????????? //重新設置發布目錄
# Relax access to content within /var/www.
<Directory "/westos">??????????????????????????????????
????? AllowOverride None
??? # Allow open access:
????? Require all granted
????? DirectoryIndex index??????????????????????? //將發布文件由原來的index.html設置為index
</Directory>
[root@server1 html]# systemctl restart httpd
去172.25.1.250主機輸入該主機ip及端口8080進行瀏覽:
總結:可以看出此時顯示的并不是/var/www/html下的index.html網頁,而是/westos/html下的html網頁。
情況二:讓其顯示/westos/html/cxx下的index.html網頁
[root@server1 html]# pwd
/westos/html?????????????
[root@server1 html]# mkdir cxx??????????????? //創建需要發布的目錄,該目錄必須存在
[root@server1 ~]# vim /etc/httpd/conf/httpd.conf
分別修改120、124、128行
[root@server1 html]# systemctl restart httpd
[root@server1 cxx]# vim index.html
此時在172.25.1.250主機網頁進行訪問:
訪問控制
基于ip的訪問控制
此時的默認發布路徑為/var/www/html??? 端口為80
防火墻與selinux關閉
server1的ip:?? 172.25.1.1
server2的ip:172.25.1.2
foundation的ip: 172.25.1.250?
[root@server1 ~]# cd /var/www/html/
[root@server1 html]# ls
index.html
[root@server1 html]# cat index.html???????????? //默認發布文件
<h1>172.25.1.1</h1>
[root@server1 html]# mkdir xx????????? //創建目錄
[root@server1 html]# cd xx/
[root@server1 xx]# vim index.html?????????????
[root@server1 xx]# vim /etc/httpd/conf/httpd.conf
?DocumentRoot "/var/www/html"????????????????? //默認發布目錄
<Directory "/var/www/html/xx">???????????????
????? Order deny,Allow??????????????????? //設置為先禁止后允許
????? Deny from All????????????????????????? //拒絕所有
????? Allow from 172.25.1.250?????????????? //允許白名單172.25.1.250主機訪問,其他都不允許
????? DirectoryIndex index.html???????????? //默認發布頁面
</Directory>
//先訪問禁止ip再訪問允許名單,讀的先后順序根據Order所在的行來定,最終結果由Order下的最后一個來定
[root@server1 xx]# systemctl restart httpd
瀏覽器訪問或者curl命令訪問:
在主機訪問:
在server2上訪問:
訪問172.25.1.1/xx 時顯示? 403 Forbidden,說明server2不可訪問
Order Allow,Deny???????????????????? Order Allow,Deny?????????????????????? ? ?? Order Deny,Allow?????????????????????? Order Deny,Allow
Allow from All???????????????????????????? Allow from 172.25.1.250 ?????????? ???Deny from 172.25.1.250??????????? Deny from All ???????
Deny from 172.25.1.250?????????? Deny from All ? ? ??????????????????????????? Allow from All?????????????????????????????? Allow from 172.25.1.250????
//除了250其他均可訪問???????????? // 所有用戶均不可訪問???????? ? ? ? ?? //所有用戶均可訪問 ? ? ? ? ?? ? ? ? ?? //僅250可訪問,其他均不可?
基于主機的訪問控制
[root@server1 html]# cd /etc/httpd
[root@server1 httpd]# ls
[root@server1 httpd]# htpasswd -cm apacheuser user1???????????????????? //創建apache用戶user1,生成用戶user1的密碼文件
注釋:?
參數:? -c :建立文件,m: 表示建立,當已經有創建好的apache用戶時不可以再加-c,否則會覆蓋文件的內容,例如出現下列情況:
[root@server1 httpd]# htpasswd -m apacheuser user2
[root@server1 httpd]# cat apacheuser??????????? //查看文件apahceuser中的所有用戶
[root@server1 httpd]# vim /etc/httpd/conf/httpd.conf
125行: AuthUserFile /etc/httpd/apacheuser????????????????????????????? //認證的用戶文件
126行:Authname "Please input user and password!"?????????????????? //訪問時顯示的信息內容
127行:AuthType basic?????????????????????????? //指定認證類型:基礎類型
128行:Require user user1?????????????????? //設置只允許user1進行訪問
[root@server1 httpd]# systemctl restart httpd
注釋:如果添加 Require valid-user 表示允許文件apahceuser中的所有用戶訪問
此時,在瀏覽器進行訪問:
輸入用戶名及密碼,可得到下面的網頁內容
若輸入有誤,則顯示禁止訪問:
指定域名解析
在瀏覽器所在主機進行域名解析:
我用的是foundation, ?????
[root@foundation1 ~]# vim /etc/hosts?????
?? //地址解析,即將www.westos.com?? news.westos.com?? music.westos.com都解析到172.25.1.1主機
[root@server1 ~ ]# mkdir /var/www/virtual/westos.com/news -p?????????????? //創建news的專屬發布目錄
[root@server1 ~ ]# mkdir /var/www/virtual/westos.com/music -p???????????? //創建music的專屬發布目錄
[root@server1 ~ ]# cd /var/www/virtual/westos.com/news/?????????????????????
[root@server1 news]# vim index.html?????????????? //編輯news的專屬網頁
[root@server1 news]# cd ..
[root@server1 westos.com]# cd music/????????????
[root@server1 music]# vim index.html???????????? //編輯music的專屬網頁
[root@server1 music]# cd /etc/httpd/conf.d???? //虛擬配置目錄為/etc/httpd/conf.d下創建目錄
[root@server1 conf.d]# vim default.conf??????????????????? //編寫默認情況下的配置文件
<VirtualHost _default_:80>
??????? DocumentRoot /var/www/html
??????? Customlog "logs/default.log" combined
</VirtualHost>
?
[root@server1 conf.d]# vim news.conf?????????????? //編輯news的配置文件
<VirtualHost *:80>
??????? ServerName news.westos.com
??????? DocumentRoot "/var/www/virtual/westos.com/news/"
??????? CustomLog "logs/default.log" combined
</VirtualHost>
<Directory "/var/www/virtual/westos.com/news/">
??????? Require all granted
</Directory>
[root@server1 conf.d]# cp news.conf music.conf
[root@server1 conf.d]# vim music.conf????????????//編輯music的配置文件 ???
<VirtualHost *:80>
??????? ServerName music.westos.com
??????? DocumentRoot "/var/www/virtual/westos.com/music/"
??????? CustomLog "logs/default.log" combined
</VirtualHost>
<Directory "/var/www/virtual/westos.com/music/">
??????? Require all granted
</Directory>
[root@server1 conf.d]# systemctl restart httpd
?
此時在主機瀏覽器進行訪問:
直接訪問westos.com
訪問news.westos.com
訪問music.westos.com
總結
以上是生活随笔為你收集整理的Apache服务器部署(1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux系统管理及vim
- 下一篇: Apache服务器部署(2)