使用Apache搭建Web網站服務器
Apache服務
Apache被研發于1995年,是純開源軟體,用于HTTP協議提供web瀏覽服務,可在Unix、Linux、Windows上運行,
1、配置靜態IP
vim /etc/sysconfig/network-scripts/ifcfg-ens33
2、關閉防火墻或者安全模塊
systemctl disable firewalld #永久關閉防火墻
systemctl stop firewalld #暫時關閉防火墻,重啟虛擬機時防火墻會重新開啟
systemctl status firewalld #查看防火墻狀態
vim /etc/selinux/config #enforcing改為disabled,永久關閉安全模塊
3、Apache安裝:
方法一:
編譯安裝:RPM
方法二:
配置yum源進行yum安裝:

yum -y install httpd #安裝httpd
rpm -ql httpd | less #查看安裝檔案的所有
4、httpd軟體的配置
vim /etc/httpd/conf/httpd.conf #無需修改
:q #瀏覽完退出編譯模式
Apache組態檔:
ServerRoot "/etc/httpd" #httpd服務的安裝目錄
Listen 80 #默認監聽TCP協議80埠
Include conf.modules.d/*.conf #定義模板檔案
User apache #定義服務運行用戶
Group apache #定義服務運行組
ServerAdmin root@localhost #定義服務管理員
<Directory /> #定義服務根目錄的訪問權限
AllowOverride none #網站重寫功能
Require all denied #定義為拒絕訪問
</Directory>
DocumentRoot "/var/www/html" #定義網站根目錄【檔案】根目錄
<Directory "/var/www"> #定義網站根目錄的權限
AllowOverride None
Require all granted #定義允許所有用戶訪問
</Directory>
<Directory "/var/www/html"> #定義目錄訪問權限
Options Indexes FollowSymLinks
AllowOverride None
Require 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\"" combined
LogFormat "%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 None
Options None
Require all granted
</Directory>
<IfModule mime_module>
TypesConfig /etc/mime.types
AddType application/x-compress .Z
AddType application/x-gzip .gz .tgz
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8
<IfModule mime_magic_module>
MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional conf.d/*.conf
Apache的日志檔案:
cd /var/log/httpd
ls
access_log #瀏覽到httpd下有成功日志
error_log #瀏覽到httpd下有失敗日志
啟動httpd服務:
systemctl start httpd
查看行程
ps aux | grep httpd
PID檔案路徑:
/etc/httpd/run --> /run/httpd/httpd.pid
訪問檔案目錄:
/var/www/html
index.html #默認檔案名稱
echo "hello" > index.html
httpd訪問方式:
(1)Windows瀏覽器直接訪問
(2)Linux:
1. curl IP或域名進行訪問,顯示網頁源代碼,HTML陳述句
2. Firefox IP或域名 呼叫安裝的Firefox去訪問網站
3. elinks IP或域名,在終端打開一個簡易頁面,
安裝elinks命令:yum -y install elinks
構建web虛擬主機
1、基于域名:同一IP不同主機域名進行訪問不同檔案根目錄
配置hosts檔案:
vim /etc/hosts
192.168.139.140 www.test1.com
192.168.139.140 www.test2.com
組態檔:
vim /usr/share/doc/httpd-*
vim httpd-vhost.conf #模板
vim /etc/httpd/conf/httpd.conf #復制模板到這里面
<VirtualHost *:80>
DocumentRoot "/var/www/html/test1"
ServerName www.test1.com
ErrorLog "/var/log/httpd/test1-error_log"
CustomLog "/var/log/httpd/test1-access_log" common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/html/test2"
ServerName www.test2.com
ErrorLog "/var/log/httpd/test2-error_log"
CustomLog "/var/log/httpd/test2-access_log" common
</VirtualHost>
cd /var/www/html #建立2個訪問目錄
mkdir test1 → vim index.html → 隨意編輯一些內容
mkdir test2 → vim index.html → 隨意編輯一些內容
firefox 192.168.139.140 #進入火狐瀏覽器,分別訪問以下2個網址
192.168.139.140/test1
192.168.139.140/test2
做完以上操作需要重啟httpd
systemctl restart httpd
2、基于不同IP:不同IP,相同埠,不同域名,訪問不同檔案根目錄
給虛擬機添加網卡:

ifconfig ens36 up #啟動
配置ens36網卡:
cd /etc/sysconfig/network-scripts/
cp ifcfg-ens33 ifcfg-ens36
vim /etc/sysconfig/network-scripts/ifcfg-ens36#修改網卡配置資訊

重啟網卡:
systemctl restart network
配置hosts檔案:
vim /etc/hosts
192.168.139.140:80 www.test1.com
192.168.139.135:80 www.test2.com
組態檔:
vim /usr/share/doc/httpd-*
vim httpd-vhost.conf #模板
vim /etc/httpd/conf/httpd.conf #復制模板到這里面
#修改組態檔Listen 80為:
Listen 192.168.139.140:80
Listen 192.168.139.135:80
#在組態檔的最后添加代碼:
<VirtualHost 192.168.139.140:80>
DocumentRoot "/var/www/html/test1"
ServerName www.test1.com
ErrorLog "/var/log/httpd/test1-error_log"
CustomLog "/var/log/httpd/test1-access_log" common
</VirtualHost>
<VirtualHost 192.168.139.135:80>
DocumentRoot "/var/www/html/test2"
ServerName www.test2.com
ErrorLog "/var/log/httpd/test2-error_log"
CustomLog "/var/log/httpd/test2-access_log" common
</VirtualHost>
修改組態檔之后需要重啟httpd
systemctl restart httpd
firefox 192.168.139.140 #進入火狐瀏覽器,分別訪問以下2個網址
192.168.139.140:80/test1
192.168.139.135:80/test2
3、基于不同埠:不同埠,不同IP,不同域名訪問不同檔案根目錄
配置hosts檔案:
vim /etc/hosts
192.168.139.140:1061 www.test1.com
192.168.139.135:1062 www.test2.com
組態檔:
vim /usr/share/doc/httpd-*
vim httpd-vhost.conf #模板
vim /etc/httpd/conf/httpd.conf #復制模板到這里面
#修改組態檔
Listen 192.168.139.140:1061
Listen 192.168.139.135:1062
<VirtualHost 192.168.139.140:1061>
DocumentRoot "/var/www/html/test1"
ServerName www.test1.com
ErrorLog "/var/log/httpd/test1-error_log"
CustomLog "/var/log/httpd/test1-access_log" common
</VirtualHost>
<VirtualHost 192.168.139.135:1062>
DocumentRoot "/var/www/html/test2"
ServerName www.test2.com
ErrorLog "/var/log/httpd/test2-error_log"
CustomLog "/var/log/httpd/test2-access_log" common
</VirtualHost>
systemctl restart httpd #修改組態檔之后需要重啟httpd
firefox 192.168.139.140 #進入火狐瀏覽器,分別訪問以下2個網址
192.168.139.140:1061/test1
192.168.139.135:1062/test2
4、httpd服務的訪問限制:
客戶端地址的限制
Require all granted : 允許所有用戶都能訪問
Require all denied :拒絕所有用戶訪問
Require local :只允許本機訪問
Require [not] host <主機名或域名>:允許或者拒絕制定的主機或域訪問
Require [not] ip <IP或IP網段>:允許或者拒絕制定的IP或IP網段訪問
需要使用not拒絕訪問時,添加配置:
< RequireAll >
Requitre not host 或者Requitre not ip
< /RequireAll >
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/209129.html
標籤:其他
上一篇:湖湘杯 | Misc Wp
