centOS7安装nginx
1. 安装所依赖的组件
1.1 安装gcc
GCC(GNU Compiler Collection,GNU编译器套件)是由GNU开发的编程语言译器。GNU编译器套件包括C、C++、 Objective-C、 Fortran、Java、Ada和Go语言前端,也包括了这些语言的库(如libstdc++,libgcj等。)
GCC的初衷是为GNU操作系统专门编写的一款编译器。GNU系统是彻底的自由软件。此处,“自由”的含义是它尊重用户的自由
[root@iZ8vbjf2s0hom3thfsei8tZ ~
]
Using built-in specs.
COLLECT_GCC
=gcc
COLLECT_LTO_WRAPPER
=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with:
../configure --prefix
=/usr --mandir
=/usr/share/man --infodir
=/usr/share/info --with-bugurl
=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads
=posix --enable-checking
=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style
=gnu --enable-languages
=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl
=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog
=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune
=generic --with-arch_32
=x86-64 --build
=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623
(Red Hat 4.8.5-39
) (GCC
)
如果没有 任何提示,说明没有安装GCC, 可以采用yum安装
yum -y
install gcc
1.2 pcre、pcre-devel安装
pcre是一个perl库,包括perl兼容的正则表达式库,nginx的http模块使用pcre来解析正则表达式。
yum
install -y pcre pcre-devel
1.3 zlib安装
zlib库提供了很多种压缩和解压缩方式nginx使用zlib对http包的内容进行gzip。
yum
install -y zlib zlib-devel
1.4 安装openssl
openssl是web安全通信的基石,没有openssl,可以说我们的信息都是在裸奔
yum
install -y openssl openssl-devel
2. 安装Nginx
2.1 下载
Nginx 对应版本gz包 http://nginx.org/download/
这里 选择 Stable version nginx-1.18.0
wget http://nginx.org/download/nginx-1.18.0.tar.gz
2.2 加压缩
tar -zxvf nginx-1.18.0.tar.gz
2.3 复制压缩包到目标路径下
cp -r nginx-1.18.0 /usr/java/
2.4 进入nginx 目录下
cd /usr/java/nginx-1.18.0
2.5 安装 (依次执行以下命令)
./configure
make
make install
2.6 进入Nginx 并查看安装目录
[root@iZ8vbjf2s0hom3thfsei8tZ
]
[root@iZ8vbjf2s0hom3thfsei8tZ nginx
]
total 36
drwx------ 2 nobody root 4096 Jun 29 16:02 client_body_temp
drwxr-xr-x 2 root root 4096 Jun 30 20:10 conf
drwx------ 2 nobody root 4096 Jun 29 16:02 fastcgi_temp
drwxr-xr-x 2 root root 4096 Jun 29 16:01 html
drwxr-xr-x 2 root root 4096 Jun 29 18:17 logs
drwx------ 5 nobody root 4096 Jul 1 09:59 proxy_temp
drwxr-xr-x 2 root root 4096 Jun 29 16:01 sbin
drwx------ 2 nobody root 4096 Jun 29 16:02 scgi_temp
drwx------ 2 nobody root 4096 Jun 29 16:02 uwsgi_temp
2.7 启动 Nginx
进入 sbin目录下 执行
./nginx
2.8 查看Nginx 是否启动成功
[root@iZ8vbjf2s0hom3thfsei8tZ ~
]
root 8161 1 0 Jun30 ? 00:00:00 nginx: master process ./nginx
nobody 14458 8161 0 12:15 ? 00:00:00 nginx: worker process
nobody 14459 8161 0 12:15 ? 00:00:00 nginx: worker process
nobody 14460 8161 0 12:15 ? 00:00:00 nginx: worker process
nobody 14461 8161 0 12:15 ? 00:00:00 nginx: worker process
root 21728 21616 0 14:34 pts/8 00:00:00
grep --color
=auto nginx
2.9 加入开机自启动
2.9.1 进入 /lib/systemd/system/
[root@iZ8vbjf2s0hom3thfsei8tZ system
]
2.9.2 创建并编辑 nginx.service文件
[root@iZ8vbjf2s0hom3thfsei8tZ system
]
[Unit
]
Description
=nginx
service
After
=network.target
[Service
]
Type
=forking
ExecStart
=/usr/local/nginx/sbin/nginx
ExecReload
=/usr/local/nginx/sbin/nginx -s reload
ExecStop
=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp
=true
[Install
]
WantedBy
=multi-user.target
[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
2.9.3 加入开机自启动
[root@iZ8vbjf2s0hom3thfsei8tZ system
]
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
2.9.4 常用管理命令
[root@iZ8vbjf2s0hom3thfsei8tZ
]
[root@iZ8vbjf2s0hom3thfsei8tZ
]
[root@iZ8vbjf2s0hom3thfsei8tZ
]
[root@iZ8vbjf2s0hom3thfsei8tZ
]
[root@iZ8vbjf2s0hom3thfsei8tZ
]
[root@iZ8vbjf2s0hom3thfsei8tZ
]
[root@iZ8vbjf2s0hom3thfsei8tZ
]
3. Nginx 常用命令
nginx -s reopen
nginx -s reload
nginx -s stop
nginx -s quit
nginx -t
nginx -?,-h
nginx -v
nginx -V
nginx -t
nginx -T
nginx -q
nginx -p prefix
nginx -c filename
nginx -g directives
killall nginx
参数官方解释翻译
-hthis help帮助-vshow version and exit显示版本信息-Vshow version and configure options then exit显示版本信息和配置信息-ttest configuration and exit检查配置文件语法-Ttest configuration, dump it and exit-qsuppress non-error messages during configuration testing-ssignalsend signal to a master process: stop, quit, reopen, reload控制服务平滑重启 停止-p prefixset prefix path (default: /etc/nginx/)-c filenameset configuration file (default: /etc/nginx/nginx.conf)-g directivesset global directives out of configuration file
示例使用
[root@iZ8vbjf2s0hom3thfsei8tZ sbin
]
nginx version: nginx/1.18.0
Usage: nginx
[-?hvVtTq
] [-s signal
] [-c filename
] [-p prefix
] [-g directives
]
Options:
-?,-h
: this
help
-v
: show version and
exit
-V
: show version and configure options
then exit
-t
: test configuration and
exit
-T
: test configuration, dump it and
exit
-q
: suppress non-error messages during configuration testing
-s signal
: send signal to a master process: stop, quit, reopen, reload
-p prefix
: set prefix path
(default: /usr/local/nginx/
)
-c filename
: set configuration
file (default: conf/nginx.conf
)
-g directives
: set global directives out of configuration
file
[root@iZ8vbjf2s0hom3thfsei8tZ sbin
]
nginx version: nginx/1.18.0
[root@iZ8vbjf2s0hom3thfsei8tZ sbin
]
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623
(Red Hat 4.8.5-39
) (GCC
)
configure arguments:
[root@iZ8vbjf2s0hom3thfsei8tZ sbin
]
nginx: the configuration
file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration
file /usr/local/nginx/conf/nginx.conf
test is successful