Apache和Nginx實現(xiàn)虛擬主機的3種方式小結(jié)
首先介紹一下Apache和nginx:
Apache(Apache HTTP Server):是一個模型化的服務(wù)器,可以運行在幾乎所有的服務(wù)器上。其屬于應(yīng)用服務(wù)器
特點:支持模塊多、性能穩(wěn)定、Apache本身是靜態(tài)解析,適合靜態(tài)HTML、圖片,但是可以通過擴展腳本、模塊等支持動態(tài)頁面等
Nginx:是俄羅斯人編寫的十分輕量級的HTTP服務(wù)器,是一個高性能的HTTP和反向代理服務(wù)器,同時也是一個IMAP/POP3/SMTP代理服務(wù)器,
特點:占有內(nèi)存少,并發(fā)能力強、抑郁開發(fā)、部署方便。Niginx支持多語言通用服務(wù)器
Nginx和Apache的不同之處:
- Nginx配置簡介,Apache較為復(fù)雜;
- Nginx靜態(tài)處理性能比Apache高很多
- Apache是同步多進程模型,一個連接誒對應(yīng)一個進程;Nginx是異步的,多個連接可以對一個進程
- Nginx處理靜態(tài)文件好,耗費內(nèi)存少;動態(tài)請求Apache比較擅長,Nginx更適合去做靜態(tài)和反向
- Nginx適合做前端服務(wù)器,負載性能很好;Nginx本身就是一個反向代理服務(wù)器,且支持負載均衡
虛擬主機
解決的問題:解決一臺web服務(wù)器運行多個web應(yīng)用
準備工作
首先本次測試環(huán)境運行在虛擬機中:使用center os 7
然后進入配置文件中:
vim /etc/httpd/conf/httpd.conf
找到上圖的位置
1.將ServerName前的注釋刪除,即開啟了www.example.com這個域名的訪問
2.將Require all denied修改為 granted ,即允許所有的請求
3.找到這里增加一條;作用是配置php的配置環(huán)境讓php可以正常使用
4.找到這里在后面增加一個 index.php,即增加了一個php頁面
配置完成后重啟http服務(wù):
systemctl start httpd
配置LNMP環(huán)境:
安裝所有需要的軟件包:
yum install epel-release.noarch yum install nginx mariadb-server.x86_64 mariadb php php-fpm.x86_64 php-mysql -y
關(guān)閉selinux:
setenforce 0
啟動:
systemctl start nginx systemctl start php-fpm.service
Apache實現(xiàn):
方法1:使用不同的ip來實現(xiàn)
(1)首先創(chuàng)建一個目錄:
mkdir /www/
(2)創(chuàng)建日志存放的目錄:
mkdir /www/logs
(3)為兩個目錄下的index.html寫入內(nèi)容:
echo "Hello Apache" > /www/index.html
(4)為網(wǎng)卡ens160增加一個ip地址:
nmcli connection modify ens33 +ipv4.addresses 192.168.159.250/24 ipv4.method manual
(5)激活該地址:
nmcli con up ens33
(6)安裝離線幫助包:
dnf install -y httpd-manual
(7)重啟httpd服務(wù):
systemctl restart httpd
(8)在瀏覽器中打開幫助手冊:(192.168.159.200/mantal Apache HTTP Server Version 2.4 Documentation - Apache HTTP Server Version 2.4)
(9)進入配置路徑:
cd /etc/httpd/conf.d/
(10) vim VirtualHost.conf 將以下內(nèi)容寫入:
<Directory "/www/"> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> <VirtualHost 192.168.159.200:80> DocumentRoot "/www" ServerName www1.example.com ErrorLog "/www/logs/error_log" CustomLog "/www/logs/access_log" combined </VirtualHost> <VirtualHost 192.168.159.250:80> DocumentRoot "/www" ServerName www2.example.com ErrorLog "/www/logs/error_log" CustomLog "/www/logs/access_log" combined </VirtualHost>
(11)檢查一下是否有語法錯誤
httpd -t
(12)重啟httpd:
systemctl restart httpd
注:如果這里不成功很有可能和selinux有關(guān)
關(guān)閉selinux:
setenforce 0
(13)使用瀏覽器測試:
可以看到,我們成功的實現(xiàn)了不同的ip地址,訪問同一個資源
方法2:使用相同的ip,不同的端口來實現(xiàn)
(1)首先將上面添加的ip刪除:
nmcli connection modify ens33 -ipv4.addresses 192.168.159.250/24 ipv4.method manual nmcli con up ens33
(3)vim /etc/http/conf.d/VirtualHost寫入以下內(nèi)容:
Listen 81 Listen 82 <Directory "/www/"> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> <VirtualHost 192.168.159.200:81> DocumentRoot "/www" ServerName www1.example.com ErrorLog "/www/logs/error_log" CustomLog "/www/logs/access_log" combined </VirtualHost> <VirtualHost 192.168.159.200:82> DocumentRoot "/www" ServerName www2.example.com ErrorLog "/www/logs/error_log" CustomLog "/www/logs/access_log" combined </VirtualHost>
(4)重啟httpd:
systemctl restart httpd
(5)防火墻放行81和82端口:
firewall-cmd --permanent --add-port=81-82/tcpsuccess firewall-cmd --reload 讓防火墻立即生效success
(6)使用瀏覽器進行測試:
方法3:使用相同的ip,相同的端口,不同的主機名(域名)
(1)將配置文件修改為:
<Directory "/www/"> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> <VirtualHost 192.168.159.200:80> DocumentRoot "/www" ServerName www1.example.com ErrorLog "/www/logs/error_log" CustomLog "/www/logs/access_log" combined </VirtualHost> <VirtualHost 192.168.159.200:80> DocumentRoot "/www" ServerName www2.example.com ErrorLog "/www/logs/error_log" CustomLog "/www/logs/access_log" combined </VirtualHost>
(2)重啟:
systemctl restart httpd
(3)在windowC:\Windows\System32\drivers\etc的路徑中的hosts中添加對應(yīng)關(guān)系:
192.168.159.200 www1.example.com www2.example.com
測試:
Nginx實現(xiàn):
方法1:使用不同的ip來實現(xiàn)
為當(dāng)前網(wǎng)卡增加一個ip
nmcli con modify ens33 +ipv4.addresses 192.168.159.222/24 nmcli con up ens33
注:這里將ip地址修改為自己主機一個網(wǎng)段的ip地址
創(chuàng)建文件夾:
mkdir /www/
為主頁面中寫入內(nèi)容:vim index.html
"hello 我的路徑是www/index.html"
為這個頁面創(chuàng)建日志文件,后面配置虛擬主機會用到
mkdir /www/logs
然后修改配置文件:
vim /etc/nginx/conf.d/test.conf
將以下內(nèi)容寫入:
server { listen 80; server_name 192.168.159.200; charset utf-8; index index.html; root /www/; } server { listen 80; server_name 192.168.159.201; charset utf-8; index index.html; root /www/;}
測試:
根據(jù)結(jié)果我們可以看到,使用不容的ip地址訪都問到了我們寫的主頁面
方法2:使用相同的ip,不同的端口來實現(xiàn)
這里首先將之前新增加的ip刪除掉:
nmcli con modify ens33 -ipv4.addresses 192.168.159.201 nmcli con up ens33
修改虛擬主機為:
server { listen 81; server_name 192.168.159.200; charset utf-8; index index.html; root /www/web1; } server { listen 82; server_name 192.168.159.200; charset utf-8; index index.html; root /www/web2;}
防火墻放行tcp81/82端口,或者關(guān)閉防火墻
上面的Apache實現(xiàn)時使用了方形端口,那我這里直接關(guān)閉防火墻:
systemctl stop firewall.service
重啟nginx服務(wù):
systemctl restart nginx.service
測試:
方法3:使用相同的ip,相同的端口,不同的主機名(域名)
修改配置文件為:
server { listen 80; server_name www1.example.com; charset utf-8; index index.html; root /www/web1; } server { listen 80; server_name www2.example.com; charset utf-8; index index.html; root /www/web2;}
在window主機中的host中增加對應(yīng)關(guān)系
192.168.159.200 www1.example.com 192.168.159.200 www2.example.com
測試:
到這里Nginx和Apache三種虛擬主機的配置就已經(jīng)全部完成了!
到此這篇關(guān)于Apache和Nginx實現(xiàn)虛擬主機的3種方式的文章就介紹到這了,更多相關(guān)Apache和Nginx虛擬主機內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Nginx轉(zhuǎn)發(fā)丟失cookie表現(xiàn)形式及解決方案
本文主要介紹了Nginx轉(zhuǎn)發(fā)丟失cookie表現(xiàn)形式及解決方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01關(guān)于使用Keepalived實現(xiàn)Nginx的自動重啟及雙主熱備高可用問題
這篇文章主要介紹了使用Keepalived實現(xiàn)Nginx的自動重啟及雙主熱備高可用,本文通過幾個問題解析幫助大家學(xué)習(xí)Keepalived實現(xiàn)Nginx的自動重啟的相關(guān)知識,需要的朋友可以參考下2021-09-09nginx通過nginx_upstream_check_module實現(xiàn)后端健康檢查
nginx的健康檢查有兩種,一種是被動健康檢查,也就是nginx自帶健康檢查模塊ngx_http_upstream_module,另一種就是主動健康檢查,使用第三方模塊nginx_upstream_check_module,下面就來介紹一下,感興趣的可以了解一下2024-08-08Ubuntu系統(tǒng)下的Nginx服務(wù)器軟件安裝時的常見錯誤解決
這篇文章主要介紹了Ubuntu系統(tǒng)下的Nginx服務(wù)器軟件安裝時的常見問題解決,包括徹底卸載Nginx的方法介紹,需要的朋友可以參考下2016-03-03nginx配置gzip壓縮優(yōu)化傳輸效率加快頁面訪問速度的問題
本文介紹了如何在nginx服務(wù)器中配置gzip壓縮,通過壓縮HTTP響應(yīng)內(nèi)容,減少數(shù)據(jù)傳輸大小和響應(yīng)時間,從而提升網(wǎng)站性能和訪問速度,感興趣的朋友跟隨小編一起看看吧2024-09-09Nginx利用Lua+Redis實現(xiàn)動態(tài)封禁IP的方法
在站點遇到攻擊且無明顯攻擊特征,造成站點訪問慢,nginx不斷返回502等錯誤時,可利用nginx+lua+redis對該IP進行封禁,這篇文章主要給大家介紹了關(guān)于Nginx利用Lua+Redis實現(xiàn)動態(tài)封禁IP的相關(guān)資料,需要的朋友可以參考下2018-12-12