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 ~]# gcc -v
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
1.2 pcre、pcre-devel安装

pcre是一个perl库,包括perl兼容的正则表达式库,nginx的http模块使用pcre来解析正则表达式。

yum install
1.3 zlib安装

zlib库提供了很多种压缩和解压缩方式nginx使用zlib对http包的内容进行gzip。

yum install
1.4 安装openssl

openssl是web安全通信的基石,没有openssl,可以说我们的信息都是在裸奔

yum install

2. 安装Nginx

2.1 下载

Nginx 对应版本gz包 http://nginx.org/download/

这里 选择 Stable version ​​ nginx-1.18.0​

wget
2.2 加压缩
tar
2.3 复制压缩包到目标路径下
cp -r nginx-1.18.0  /usr/java/
2.4 进入nginx 目录下
cd
2.5 安装 (依次执行以下命令)
./configure

make

make install
2.6 进入Nginx 并查看安装目录
[root@iZ8vbjf2s0hom3thfsei8tZ]# cd /usr/local/nginx
[root@iZ8vbjf2s0hom3thfsei8tZ nginx]# ll
2.7 启动 Nginx

进入 sbin目录下 执行

./nginx
2.8 查看Nginx 是否启动成功
[root@iZ8vbjf2s0hom3thfsei8tZ ~]# ps -ef | grep nginx
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]# cd /lib/systemd/system/
2.9.2 创建并编辑 nginx.service文件
[root@iZ8vbjf2s0hom3thfsei8tZ system]# vim nginx.service
[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]# systemctl enable nginx
2.9.4 常用管理命令
[root@iZ8vbjf2s0hom3thfsei8tZ]# systemctl start nginx.service          启动nginx服务
[root@iZ8vbjf2s0hom3thfsei8tZ]# systemctl stop nginx.service  停止服务
[root@iZ8vbjf2s0hom3thfsei8tZ]# systemctl restart nginx.service  重新启动服务
[root@iZ8vbjf2s0hom3thfsei8tZ]# systemctl list-units --type=service 查看所有已启动的服务
[root@iZ8vbjf2s0hom3thfsei8tZ]# systemctl status nginx.service 查看服务当前状态
[root@iZ8vbjf2s0hom3thfsei8tZ]# systemctl enable nginx.service 设置开机自启动
[root@iZ8vbjf2s0hom3thfsei8tZ]# systemctl disable nginx.service 停止开机自启动

3. Nginx 常用命令

nginx -s reopen  #重启Nginx

nginx -s reload #重新加载Nginx配置文件,然后以优雅的方式重启Nginx

nginx -s stop #强制停止Nginx服务

nginx -s quit #优雅地停止Nginx服务(即处理完所有请求后再停止服务)

nginx -t #检测配置文件是否有语法错误,然后退出

nginx -?,-h #打开帮助信息

nginx -v #显示版本信息并退出

nginx -V #显示版本和配置选项信息,然后退出

nginx -t #检测配置文件是否有语法错误,然后退出

nginx -T #检测配置文件是否有语法错误,转储并退出

nginx -q #在检测配置文件期间屏蔽非错误信息

nginx -p prefix #设置前缀路径(默认是:/usr/share/nginx/)

nginx -c filename #设置配置文件(默认是:/etc/nginx/nginx.conf)

nginx -g directives #设置配置文件外的全局指令

killall nginx #杀死所有nginx进程

参数

官方解释

翻译

-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

signalsend signal to a master process: stop, quit, reopen, reload

控制服务平滑重启 停止

-p prefix

set prefix path (default: /etc/nginx/)

-c filename

set configuration file (default: /etc/nginx/nginx.conf)

-g directives

set global directives out of configuration file

示例使用

[root@iZ8vbjf2s0hom3thfsei8tZ sbin]# ./nginx -h
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 -v
nginx version: nginx/1.18.0
[root@iZ8vbjf2s0hom3thfsei8tZ sbin]# ./nginx -V
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 -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test