Nginx(读作in gen ex)是Web服务器,类似Apache,不过更容易配置,而且使用系统内存通常更少。我没有时间详细介绍Nginx,不过我想告诉你如何在你的服务器中安装Nginx,以及如何把相应的请求转发给PHP-FPM进程池。
Centos7安装Nginx 有两种方法
- 通过yum安装
- 手动下载安装包解压安装
本文采用第一种安装方式,通过yum安装
当然,直接通过yum install nginx
肯定是不行的,因为yum没有nginx,所以首先把 nginx 的源加入 yum中
一、sudo yum install epel-release
命令,添加CentOS7 EPEL存储库
[root@localhost ~]# sudo yum install epel-release
Loaded plugins: fastestmirror
base | 3.6 kB 00:00:00
epel | 3.2 kB 00:00:00
extras | 3.4 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/7): base/7/x86_64/group_gz | 166 kB 00:00:00
(2/7): epel/x86_64/group_gz | 88 kB 00:00:00
(3/7): epel/x86_64/updateinfo | 943 kB 00:00:00
(4/7): extras/7/x86_64/primary_db | 187 kB 00:00:00
(5/7): epel/x86_64/primary | 3.6 MB 00:00:00
(6/7): base/7/x86_64/primary_db | 5.9 MB 00:00:00
(7/7): updates/7/x86_64/primary_db | 5.2 MB 00:00:00
Determining fastest mirrors
epel 12687/12687
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
========================================================================================================================
Package Arch Version Repository Size
========================================================================================================================
Installing:
epel-release noarch 7-11 epel 15 k
Transaction Summary
========================================================================================================================
Install 1 Package
Total download size: 15 k
Installed size: 24 k
Is this ok [y/d/N]:
键入 “y” 回车,安装成功
Downloading packages:
epel-release-7-11.noarch.rpm | 15 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : epel-release-7-11.noarch 1/1
warning: /etc/yum.repos.d/epel.repo created as /etc/yum.repos.d/epel.repo.rpmnew
Verifying : epel-release-7-11.noarch 1/1
Installed:
epel-release.noarch 0:7-11
Complete!
二、sudo yum install nginx
命令,现在,Nginx的存储库安装在服务器上,然后安装Web服务器。
[root@localhost ~]# sudo yum install nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.12.2-2.el7 will be installed
--> Processing Dependency: nginx-filesystem = 1:1.12.2-2.el7 for package: 1:nginx-1.12.2-2.el7.x86_64
--> Processing Dependency: nginx-all-modules = 1:1.12.2-2.el7 for package: 1:nginx-1.12.2-2.el7.x86_64
--> Processing Dependency: nginx-filesystem for package: 1:nginx-1.12.2-2.el7.x86_64
--> Processing Dependency: libcrypto.so.10(OPENSSL_1.0.2)(64bit) for package: 1:nginx-1.12.2-2.el7.x86_64
--> Processing Dependency: libprofiler.so.0()(64bit) for package: 1:nginx-1.12.2-2.el7.x86_64
--> Running transaction check
---> Package gperftools-libs.x86_64 0:2.6.1-1.el7 will be installed
---> Package nginx-all-modules.noarch 1:1.12.2-2.el7 will be installed
--> Processing Dependency: nginx-mod-stream = 1:1.12.2-2.el7 for package: 1:nginx-all-modules-1.12.2-2.el7.noarch
--> Processing Dependency: nginx-mod-mail = 1:1.12.2-2.el7 for package: 1:nginx-all-modules-1.12.2-2.el7.noarch
--> Processing Dependency: nginx-mod-http-xslt-filter = 1:1.12.2-2.el7 for package: 1:nginx-all-modules-1.12.2-2.el7.noarch
--> Processing Dependency: nginx-mod-http-perl = 1:1.12.2-2.el7 for package: 1:nginx-all-modules-1.12.2-2.el7.noarch
--> Processing Dependency: nginx-mod-http-image-filter = 1:1.12.2-2.el7 for package: 1:nginx-all-modules-1.12.2-2.el7.noarch
--> Processing Dependency: nginx-mod-http-geoip = 1:1.12.2-2.el7 for package: 1:nginx-all-modules-1.12.2-2.el7.noarch
---> Package nginx-filesystem.noarch 1:1.12.2-2.el7 will be installed
---> Package openssl-libs.x86_64 1:1.0.1e-60.el7_3.1 will be updated
--> Processing Dependency: openssl-libs(x86-64) = 1:1.0.1e-60.el7_3.1 for package: 1:openssl-1.0.1e-60.el7_3.1.x86_64
---> Package openssl-libs.x86_64 1:1.0.2k-12.el7 will be an update
--> Running transaction check
---> Package nginx-mod-http-geoip.x86_64 1:1.12.2-2.el7 will be installed
--> Processing Dependency: GeoIP for package: 1:nginx-mod-http-geoip-1.12.2-2.el7.x86_64
--> Processing Dependency: libGeoIP.so.1()(64bit) for package: 1:nginx-mod-http-geoip-1.12.2-2.el7.x86_64
---> Package nginx-mod-http-image-filter.x86_64 1:1.12.2-2.el7 will be installed
--> Processing Dependency: gd for package: 1:nginx-mod-http-image-filter-1.12.2-2.el7.x86_64
--> Processing Dependency: libgd.so.2()(64bit) for package: 1:nginx-mod-http-image-filter-1.12.2-2.el7.x86_64
---> Package nginx-mod-http-perl.x86_64 1:1.12.2-2.el7 will be installed
---> Package nginx-mod-http-xslt-filter.x86_64 1:1.12.2-2.el7 will be installed
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.18)(64bit) for package: 1:nginx-mod-http-xslt-filter-1.12.2-2.el7.x86_64
--> Processing Dependency: libxslt.so.1(LIBXML2_1.0.11)(64bit) for package: 1:nginx-mod-http-xslt-filter-1.12.2-2.el7.x86_64
--> Processing Dependency: libxslt.so.1()(64bit) for package: 1:nginx-mod-http-xslt-filter-1.12.2-2.el7.x86_64
--> Processing Dependency: libexslt.so.0()(64bit) for package: 1:nginx-mod-http-xslt-filter-1.12.2-2.el7.x86_64
---> Package nginx-mod-mail.x86_64 1:1.12.2-2.el7 will be installed
---> Package nginx-mod-stream.x86_64 1:1.12.2-2.el7 will be installed
---> Package openssl.x86_64 1:1.0.1e-60.el7_3.1 will be updated
---> Package openssl.x86_64 1:1.0.2k-12.el7 will be an update
--> Running transaction check
---> Package GeoIP.x86_64 0:1.5.0-11.el7 will be installed
---> Package gd.x86_64 0:2.0.35-26.el7 will be installed
--> Processing Dependency: libpng15.so.15(PNG15_0)(64bit) for package: gd-2.0.35-26.el7.x86_64
--> Processing Dependency: libjpeg.so.62(LIBJPEG_6.2)(64bit) for package: gd-2.0.35-26.el7.x86_64
--> Processing Dependency: libpng15.so.15()(64bit) for package: gd-2.0.35-26.el7.x86_64
--> Processing Dependency: libjpeg.so.62()(64bit) for package: gd-2.0.35-26.el7.x86_64
--> Processing Dependency: libfontconfig.so.1()(64bit) for package: gd-2.0.35-26.el7.x86_64
--> Processing Dependency: libXpm.so.4()(64bit) for package: gd-2.0.35-26.el7.x86_64
--> Processing Dependency: libX11.so.6()(64bit) for package: gd-2.0.35-26.el7.x86_64
---> Package libxslt.x86_64 0:1.1.28-5.el7 will be installed
--> Running transaction check
---> Package fontconfig.x86_64 0:2.10.95-11.el7 will be installed
--> Processing Dependency: fontpackages-filesystem for package: fontconfig-2.10.95-11.el7.x86_64
--> Processing Dependency: font(:lang=en) for package: fontconfig-2.10.95-11.el7.x86_64
---> Package libX11.x86_64 0:1.6.5-1.el7 will be installed
--> Processing Dependency: libX11-common >= 1.6.5-1.el7 for package: libX11-1.6.5-1.el7.x86_64
--> Processing Dependency: libxcb.so.1()(64bit) for package: libX11-1.6.5-1.el7.x86_64
---> Package libXpm.x86_64 0:3.5.12-1.el7 will be installed
---> Package libjpeg-turbo.x86_64 0:1.2.90-5.el7 will be installed
---> Package libpng.x86_64 2:1.5.13-7.el7_2 will be installed
--> Running transaction check
---> Package fontpackages-filesystem.noarch 0:1.44-8.el7 will be installed
---> Package libX11-common.noarch 0:1.6.5-1.el7 will be installed
---> Package libxcb.x86_64 0:1.12-1.el7 will be installed
--> Processing Dependency: libXau.so.6()(64bit) for package: libxcb-1.12-1.el7.x86_64
---> Package lyx-fonts.noarch 0:2.2.3-1.el7 will be installed
--> Running transaction check
---> Package libXau.x86_64 0:1.0.8-2.1.el7 will be installed
--> Finished Dependency Resolution
--> Finished Dependency Resolution
Dependencies Resolved
========================================================================================================================
Package Arch Version Repository Size
========================================================================================================================
Installing:
nginx x86_64 1:1.12.2-2.el7 epel 530 k
Installing for dependencies:
GeoIP x86_64 1.5.0-11.el7 base 1.1 M
fontconfig x86_64 2.10.95-11.el7 base 229 k
fontpackages-filesystem noarch 1.44-8.el7 base 9.9 k
gd x86_64 2.0.35-26.el7 base 146 k
gperftools-libs x86_64 2.6.1-1.el7 base 272 k
libX11 x86_64 1.6.5-1.el7 base 606 k
libX11-common noarch 1.6.5-1.el7 base 164 k
libXau x86_64 1.0.8-2.1.el7 base 29 k
libXpm x86_64 3.5.12-1.el7 base 55 k
libjpeg-turbo x86_64 1.2.90-5.el7 base 134 k
libpng x86_64 2:1.5.13-7.el7_2 base 213 k
libxcb x86_64 1.12-1.el7 base 211 k
libxslt x86_64 1.1.28-5.el7 base 242 k
lyx-fonts noarch 2.2.3-1.el7 epel 159 k
nginx-all-modules noarch 1:1.12.2-2.el7 epel 16 k
nginx-filesystem noarch 1:1.12.2-2.el7 epel 17 k
nginx-mod-http-geoip x86_64 1:1.12.2-2.el7 epel 23 k
nginx-mod-http-image-filter x86_64 1:1.12.2-2.el7 epel 26 k
nginx-mod-http-perl x86_64 1:1.12.2-2.el7 epel 36 k
nginx-mod-http-xslt-filter x86_64 1:1.12.2-2.el7 epel 26 k
nginx-mod-mail x86_64 1:1.12.2-2.el7 epel 54 k
nginx-mod-stream x86_64 1:1.12.2-2.el7 epel 76 k
Updating for dependencies:
openssl x86_64 1:1.0.2k-12.el7 base 492 k
openssl-libs x86_64 1:1.0.2k-12.el7 base 1.2 M
Transaction Summary
========================================================================================================================
Install 1 Package (+22 Dependent packages)
Upgrade ( 2 Dependent packages)
Total download size: 5.9 M
Is this ok [y/d/N]:
键入 “y” 回车,安装完成
Is this ok [y/d/N]: y
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/25): fontconfig-2.10.95-11.el7.x86_64.rpm | 229 kB 00:00:00
(2/25): fontpackages-filesystem-1.44-8.el7.noarch.rpm | 9.9 kB 00:00:00
(3/25): gd-2.0.35-26.el7.x86_64.rpm | 146 kB 00:00:00
(4/25): gperftools-libs-2.6.1-1.el7.x86_64.rpm | 272 kB 00:00:00
(5/25): libX11-1.6.5-1.el7.x86_64.rpm | 606 kB 00:00:00
(6/25): libX11-common-1.6.5-1.el7.noarch.rpm | 164 kB 00:00:00
(7/25): libXau-1.0.8-2.1.el7.x86_64.rpm | 29 kB 00:00:00
(8/25): libXpm-3.5.12-1.el7.x86_64.rpm | 55 kB 00:00:00
(9/25): libjpeg-turbo-1.2.90-5.el7.x86_64.rpm | 134 kB 00:00:00
(10/25): libpng-1.5.13-7.el7_2.x86_64.rpm | 213 kB 00:00:00
(11/25): libxcb-1.12-1.el7.x86_64.rpm | 211 kB 00:00:00
(12/25): libxslt-1.1.28-5.el7.x86_64.rpm | 242 kB 00:00:00
(13/25): lyx-fonts-2.2.3-1.el7.noarch.rpm | 159 kB 00:00:00
(14/25): nginx-all-modules-1.12.2-2.el7.noarch.rpm | 16 kB 00:00:00
(15/25): nginx-filesystem-1.12.2-2.el7.noarch.rpm | 17 kB 00:00:00
(16/25): nginx-1.12.2-2.el7.x86_64.rpm | 530 kB 00:00:00
(17/25): nginx-mod-http-geoip-1.12.2-2.el7.x86_64.rpm | 23 kB 00:00:00
(18/25): GeoIP-1.5.0-11.el7.x86_64.rpm | 1.1 MB 00:00:00
(19/25): nginx-mod-http-perl-1.12.2-2.el7.x86_64.rpm | 36 kB 00:00:00
(20/25): nginx-mod-http-xslt-filter-1.12.2-2.el7.x86_64.rpm | 26 kB 00:00:00
(21/25): nginx-mod-mail-1.12.2-2.el7.x86_64.rpm | 54 kB 00:00:00
(22/25): nginx-mod-stream-1.12.2-2.el7.x86_64.rpm | 76 kB 00:00:00
(23/25): nginx-mod-http-image-filter-1.12.2-2.el7.x86_64.rpm | 26 kB 00:00:00
(24/25): openssl-1.0.2k-12.el7.x86_64.rpm | 492 kB 00:00:00
(25/25): openssl-libs-1.0.2k-12.el7.x86_64.rpm | 1.2 MB 00:00:00
------------------------------------------------------------------------------------------------------------------------
Total 9.6 MB/s | 5.9 MB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : fontpackages-filesystem-1.44-8.el7.noarch 1/27
Updating : 1:openssl-libs-1.0.2k-12.el7.x86_64 2/27
Updating : 1:openssl-1.0.2k-12.el7.x86_64 3/27
Installing : lyx-fonts-2.2.3-1.el7.noarch 4/27
Installing : fontconfig-2.10.95-11.el7.x86_64 5/27
Installing : gperftools-libs-2.6.1-1.el7.x86_64 6/27
Installing : GeoIP-1.5.0-11.el7.x86_64 7/27
Installing : libXau-1.0.8-2.1.el7.x86_64 8/27
Installing : libxcb-1.12-1.el7.x86_64 9/27
Installing : libX11-common-1.6.5-1.el7.noarch 10/27
Installing : libX11-1.6.5-1.el7.x86_64 11/27
Installing : libXpm-3.5.12-1.el7.x86_64 12/27
Installing : libxslt-1.1.28-5.el7.x86_64 13/27
Installing : 1:nginx-filesystem-1.12.2-2.el7.noarch 14/27
Installing : 2:libpng-1.5.13-7.el7_2.x86_64 15/27
Installing : libjpeg-turbo-1.2.90-5.el7.x86_64 16/27
Installing : gd-2.0.35-26.el7.x86_64 17/27
Installing : 1:nginx-mod-http-xslt-filter-1.12.2-2.el7.x86_64 18/27
Installing : 1:nginx-mod-http-geoip-1.12.2-2.el7.x86_64 19/27
Installing : 1:nginx-mod-stream-1.12.2-2.el7.x86_64 20/27
Installing : 1:nginx-mod-mail-1.12.2-2.el7.x86_64 21/27
Installing : 1:nginx-mod-http-perl-1.12.2-2.el7.x86_64 22/27
Installing : 1:nginx-1.12.2-2.el7.x86_64 23/27
Installing : 1:nginx-mod-http-image-filter-1.12.2-2.el7.x86_64 24/27
Installing : 1:nginx-all-modules-1.12.2-2.el7.noarch 25/27
Cleanup : 1:openssl-1.0.1e-60.el7_3.1.x86_64 26/27
Cleanup : 1:openssl-libs-1.0.1e-60.el7_3.1.x86_64 27/27
Verifying : libX11-1.6.5-1.el7.x86_64 1/27
Verifying : lyx-fonts-2.2.3-1.el7.noarch 2/27
Verifying : 1:nginx-mod-http-xslt-filter-1.12.2-2.el7.x86_64 3/27
Verifying : libjpeg-turbo-1.2.90-5.el7.x86_64 4/27
Verifying : 1:nginx-mod-http-geoip-1.12.2-2.el7.x86_64 5/27
Verifying : 1:nginx-1.12.2-2.el7.x86_64 6/27
Verifying : libxcb-1.12-1.el7.x86_64 7/27
Verifying : 2:libpng-1.5.13-7.el7_2.x86_64 8/27
Verifying : 1:openssl-libs-1.0.2k-12.el7.x86_64 9/27
Verifying : 1:nginx-mod-stream-1.12.2-2.el7.x86_64 10/27
Verifying : fontpackages-filesystem-1.44-8.el7.noarch 11/27
Verifying : 1:nginx-all-modules-1.12.2-2.el7.noarch 12/27
Verifying : libXpm-3.5.12-1.el7.x86_64 13/27
Verifying : 1:nginx-filesystem-1.12.2-2.el7.noarch 14/27
Verifying : libxslt-1.1.28-5.el7.x86_64 15/27
Verifying : 1:nginx-mod-mail-1.12.2-2.el7.x86_64 16/27
Verifying : gd-2.0.35-26.el7.x86_64 17/27
Verifying : libX11-common-1.6.5-1.el7.noarch 18/27
Verifying : libXau-1.0.8-2.1.el7.x86_64 19/27
Verifying : GeoIP-1.5.0-11.el7.x86_64 20/27
Verifying : fontconfig-2.10.95-11.el7.x86_64 21/27
Verifying : gperftools-libs-2.6.1-1.el7.x86_64 22/27
Verifying : 1:nginx-mod-http-perl-1.12.2-2.el7.x86_64 23/27
Verifying : 1:nginx-mod-http-image-filter-1.12.2-2.el7.x86_64 24/27
Verifying : 1:openssl-1.0.2k-12.el7.x86_64 25/27
Verifying : 1:openssl-1.0.1e-60.el7_3.1.x86_64 26/27
Verifying : 1:openssl-libs-1.0.1e-60.el7_3.1.x86_64 27/27
Installed:
nginx.x86_64 1:1.12.2-2.el7
Dependency Installed:
GeoIP.x86_64 0:1.5.0-11.el7 fontconfig.x86_64 0:2.10.95-11.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-1.el7
libX11-common.noarch 0:1.6.5-1.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-5.el7
libpng.x86_64 2:1.5.13-7.el7_2 libxcb.x86_64 0:1.12-1.el7
libxslt.x86_64 0:1.1.28-5.el7 lyx-fonts.noarch 0:2.2.3-1.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
Dependency Updated:
openssl.x86_64 1:1.0.2k-12.el7 openssl-libs.x86_64 1:1.0.2k-12.el7
Complete!
三、sudo service nginx start
命令,安装成功后,启动Nginx在你的VPS:
[root@localhost ~]# sudo service nginx start
Redirecting to /bin/systemctl start nginx.service
四、curl -i localhost
验证安装顺利,提示:Welcome to nginx!
[root@localhost ~]# curl -i localhost
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 26 Sep 2018 07:14:30 GMT
Content-Type: text/html
Content-Length: 3700
Last-Modified: Tue, 06 Mar 2018 09:26:21 GMT
Connection: keep-alive
ETag: "5a9e5ebd-e74"
Accept-Ranges: bytes
Test Page for the Nginx HTTP Server on Fedora
Welcome to nginx on Fedora!
This page is used to test the proper operation of the
nginx HTTP server after it has been
installed. If you can read this page, it means that the
web server installed at this site is working
properly.
Website Administrator
This is the default index.html page that
is distributed with nginx on
Fedora. It is located in
/usr/share/nginx/html.
You should now put your content in a location of
your choice and edit the root configuration
directive in the nginx
configuration file
/etc/nginx/nginx.conf.
也可以通过你的域名或IP来访问你的Web页面来预览一下Nginx的默认页面,如下图:
五、Nginx常用操作
查看nginx版本
[root@localhost ~]# nginx -v
nginx version: nginx/1.12.2
查看默认安装目录
[root@localhost ~]# whereis nginx
nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man3/nginx.3pm.gz /usr/share/man/man8/nginx.8.gz
以下是Nginx的默认路径:
- Nginx配置路径:/etc/nginx/
- PID目录:/var/run/nginx.pid
- 错误日志:/var/log/nginx/error.log
- 访问日志:/var/log/nginx/access.log
- 默认站点目录:/usr/share/nginx/html
事实上,只需知道Nginx配置路径,其他路径均可在/etc/nginx/nginx.conf 及/etc/nginx/conf.d/default.conf 中查询到
是否安装正常
[root@localhost ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
启动nginx
命令一:nginx
命令二:
service nginx start #centos6
或
systemctl start nginx.service #centos7
[root@localhost ~]# service nginx start
Redirecting to /bin/systemctl start nginx.service
停止nginx
快速停止或关闭Nginx:nginx -s stop
正常停止或关闭Nginx:nginx -s quit
[root@localhost ~]# nginx -s stop
重启nginx
[root@localhost ~]#
CentOS 7 开机启动Nginx
[root@localhost ~]# sudo systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
Linux查看公网IP
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'