方式1 安裝Nginx源
- sudo rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@izm5e33l0ge76uqi8nq87az /]# sudo rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
獲取http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
警告:/var/tmp/rpm-tmp.j7U0Sv: 頭V4 RSA/SHA1 Signature, 密鑰 ID 7bd9bf62: NOKEY
準備中... ################################# [100%]
正在升級/安裝...
1:nginx-release-centos-7-0.el7.ngx ################################# [100%]
[root@izm5e33l0ge76uqi8nq87az /]#
- 查看 Nginx 源是否配置成功
[root@izm5e33l0ge76uqi8nq87az /]# cd /etc/yum.repos.d/
[root@izm5e33l0ge76uqi8nq87az yum.repos.d]# ls
CentOS-Base.repo mysql-community.repo nodesource-el7.repo
epel.repo mysql-community-source.repo
mongodb-org-4.0.repo nginx.repo
- 安裝Nginx
[root@izm5e33l0ge76uqi8nq87az yum.repos.d]# cd /
[root@izm5e33l0ge76uqi8nq87az /]# yum install -y nginx
已加載插件:fastestmirror
nginx | 2.9 kB 00:00
nginx/x86_64/primary_db | 45 kB 00:22
Loading mirror speeds from cached hostfile
正在解決依賴關系
--> 正在檢查事務
---> 軟件包 nginx.x86_64.1.1.16.0-1.el7.ngx 將被 安裝
--> 解決依賴關系完成
依賴關系解決
================================================================================
Package 架構 版本 源 大小
================================================================================
正在安裝:
nginx x86_64 1:1.16.0-1.el7.ngx nginx 766 k
事務概要
================================================================================
安裝 1 軟件包
總下載量:766 k
安裝大小:2.7 M
Downloading packages:
nginx-1.16.0-1.el7.ngx.x86_64.rpm | 766 kB 01:14
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
警告:RPM 數據庫已被非 yum 程序修改。
正在安裝 : 1:nginx-1.16.0-1.el7.ngx.x86_64 1/1
----------------------------------------------------------------------
Thanks for using nginx!
Please find the official documentation for nginx here:
* http://nginx.org/en/docs/
Please subscribe to nginx-announce mailing list to get
the most important news about nginx:
* http://nginx.org/en/support.html
Commercial subscriptions for nginx are available on:
* http://nginx.com/products/
----------------------------------------------------------------------
驗證中 : 1:nginx-1.16.0-1.el7.ngx.x86_64 1/1
已安裝:
nginx.x86_64 1:1.16.0-1.el7.ngx
完畢!
[root@izm5e33l0ge76uqi8nq87az /]#
- 啟動 Nginx 并設置開機自動運行
[root@izm5e33l0ge76uqi8nq87az /]# systemctl start nginx
[root@izm5e33l0ge76uqi8nq87az /]# systemctl enable nginx
- 開啟80端口
firewall-cmd --zone=public --list-ports
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
firewall-cmd --zone=public --list-ports
27017/tcp 9093/tcp 8000/tcp 5000/tcp 3306/tcp 8080/tcp
- 打開瀏覽器 顯示Welcome to nginx! 即配置nginx成功
- Nginx 反向代理配置
- 關閉 Selinux
vi etc/selinux/config
修改 SELINUX=enforcing 為 SELINUX=disabled
必須重啟 linux init 6
- 配置反向代理
[root@izm5e33l0ge76uqi8nq87az /]# cd /etc/nginx/conf.d
[root@izm5e33l0ge76uqi8nq87az conf.d]# ls
default.conf
配置default.conf的信息
server {
listen 80;
server_name www.bbb.com; location / {
#設置主機頭和客戶端真實地址,以便服務器獲取客戶端真實 IP proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #禁用緩存
proxy_buffering off;
#反向代理的地址
proxy_pass http://127.0.0.1:3001; }
}
或者
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /work/build/build;
index index.html index.htm;
}
- 檢查并重啟nginx
[root@izm5e33l0ge76uqi8nq87az conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@izm5e33l0ge76uqi8nq87az conf.d]# systemctl stop nginx
[root@izm5e33l0ge76uqi8nq87az conf.d]# systemctl start nginx
方式2 安裝Nginx源
- 安裝epel庫
- yum install epel-release -y
[root@iZm5e33l0ge76uqi8nq87aZ /]# yum install epel-release -y
已加載插件:fastestmirror
Loading mirror speeds from cached hostfile
正在解決依賴關系
--> 正在檢查事務
---> 軟件包 epel-release.noarch.0.7-11 將被 安裝
--> 解決依賴關系完成
依賴關系解決
======================================================================================================
Package 架構 版本 源 大小
=======================================================================================================
正在安裝:
epel-release noarch 7-11 epel 15 k
事務概要
=======================================================================================================
安裝 1 軟件包
總下載量:15 k
安裝大小:24 k
Downloading packages:
epel-release-7-11.noarch.rpm | 15 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安裝 : epel-release-7-11.noarch 1/1
警告:/etc/yum.repos.d/epel.repo 已建立為 /etc/yum.repos.d/epel.repo.rpmnew
驗證中 : epel-release-7-11.noarch 1/1
已安裝:
epel-release.noarch 0:7-11
完畢!
- 安裝nginx
- yum -y install nginx
驗證中 : gperftools-libs-2.6.1-1.el7.x86_64 20/25
驗證中 : libX11-common-1.6.5-2.el7.noarch 21/25
驗證中 : freetype-2.8-12.el7_6.1.x86_64 22/25
驗證中 : 1:nginx-mod-http-perl-1.12.2-2.el7.x86_64 23/25
驗證中 : 1:nginx-mod-http-image-filter-1.12.2-2.el7.x86_64 24/25
驗證中 : freetype-2.4.11-15.el7.x86_64 25/25
已安裝:
nginx.x86_64 1:1.12.2-2.el7
作為依賴被安裝:
dejavu-fonts-common.noarch 0:2.33-6.el7 dejavu-sans-fonts.noarch 0:2.33-6.el7
fontconfig.x86_64 0:2.13.0-4.3.el7 fontpackages-filesystem.noarch 0:1.44-8.el7
gd.x86_64 0:2.0.35-26.el7 gperftools-libs.x86_64 0:2.6.1-1.el7
libX11.x86_64 0:1.6.5-2.el7 libX11-common.noarch 0:1.6.5-2.el7
libXau.x86_64 0:1.0.8-2.1.el7 libXpm.x86_64 0:3.5.12-1.el7
libjpeg-turbo.x86_64 0:1.2.90-6.el7 libpng.x86_64 2:1.5.13-7.el7_2
libxcb.x86_64 0:1.13-1.el7 libxslt.x86_64 0:1.1.28-5.el7
nginx-all-modules.noarch 1:1.12.2-2.el7 nginx-filesystem.noarch 1:1.12.2-2.el7
nginx-mod-http-geoip.x86_64 1:1.12.2-2.el7 nginx-mod-http-image-filter.x86_64 1:1.12.2-2.el7
nginx-mod-http-perl.x86_64 1:1.12.2-2.el7 nginx-mod-http-xslt-filter.x86_64 1:1.12.2-2.el7
nginx-mod-mail.x86_64 1:1.12.2-2.el7 nginx-mod-stream.x86_64 1:1.12.2-2.el7
作為依賴被升級:
freetype.x86_64 0:2.8-12.el7_6.1
完畢!
啟動nginx
-
systemctl start nginx.service
打開IP地址
nginx.png 創建網站根目錄
mkdir -p /var/www/test/public_html
編輯根目錄下的文件
vim /var/www/test/public_html/index.html
<html>
<body>
<h1>Hello World</h1>
</body>
</html>
- 添加虛擬主機配置文件
- vim /etc/nginx/conf.d/vitual.conf
server{
listen 80;
server_name 主機地址;
root var/www/test/public_html;
index index.html;
}
- 查看nginx狀態 nginx -t
[root@iZm5e33l0ge76uqi8nq87aZ conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
- 重啟systemctl restart nginx.service
-
打開網址 配置成功
nginx.png