慧9建 发表于 2019-2-15 17:34:22

RHEL/CentOS 安装 nginx

RHEL/CentOS 安装 nginx
  安装类型


[*]rpm包
[*]源码编译

1 方法一:rpm包安装
  采用官方nginx源安装方法支持的环境
如果不支持,可以改为epel源




系统
版本
支持的平台




RHEL/CentOS
6.x
x86_64, i386


RHEL/CentOS
7.4+
x86_64, ppc64le


  也可以源码编译安装或直接yum安装。
域名解析请提前设置好
  nginx版本选择
nginx的软件有三种版本,稳定版、开发版、及历史稳定版
选择标准如下:


[*]开发版更新快,新功能多,但bug多
[*]稳定版的更新很慢,但是bug较少,可以做为企业生产环境的首选。
但是在实际工作中,尽量避免使用最新的稳定版,应该选择比已出版本来的最新版本晚6-10个月的版本比较好
  官网的解释是这样的:


[*]Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以说是开发版
[*]Stable version:最新稳定版,生产环境上建议使用的版本
[*]Legacy versions:遗留的老版本的稳定版

1.1 配置nginx源

配置nginx官方源(推荐)

# vim /etc/yum.repos.d/nginx.repo
添加:

name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
或者 配epel源
  备份(如有配置其他epel源)(推荐阿里epel源)

mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
  下载新repo 到/etc/yum.repos.d/
epel(RHEL 7)

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  epel(RHEL 6)

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
1.2 yum安装nginx

# yum list | grep nginx
# yum install nginx
1.3 查看版本

# nginx -v
1.4 查看nginx 编译的参数
  以下为官方默认编译参数:

# nginx -V
./configure --prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--user=nginx \
--group=nginx \
--with-compat \
--with-file-aio \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' \
--with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
2 方法二:源码编译安装nginx

2.1 安装依赖


[*]安装gcc、gcc-c++、C/C++语言编译环境

yum install gcc gcc-c++ autoconf automake make -y
  或者

yum groupinstall "Development Tools" "Server Platform Development" -y

[*]yum安装nginx依赖库

yum install pcre-devel zlib-devel openssl openssl-devel libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed libtoolpatch -y

[*]创建一个名为nginx且没有登录权限的用户和一个名为nginx的用户组

groupadd -r nginx
useradd -r -g nginx -M -s /sbin/nologin -d /usr/local/nginx nginx
id nginx
  添加新用户参数:
-r: 添加系统用户( 这里指将要被创建的系统用户nginx)
-g: 指定要创建的用户所属组( 这里指添加到新系统用户nginx到nginx系统用户组 )
-s: 新帐户的登录shell( /sbin/nologin 这里设置为将要被创建系统用户nginx不能用来登录系统 )
-d: 新帐户的主目录( 这里指定将要被创建的系统用户nginx的家目录为 /usr/local/nginx )
-M: 不要创建用户的主目录( 也就是说将要被创建的系统用户nginx不会在 /home 目录下创建 nginx 家目录 )

2.2 下载、解压
  源码包下载地址:https://nginx.org/en/download.html
  下载稳定版(Stable version),例如我下载1.14.0

# wget https://nginx.org/download/nginx-1.14.0.tar.gz
  解压

# tar xf nginx-1.14.0.tar.gz
2.3 配置编译参数
  使用该configure命令配置构建。它定义了系统的各个方面,包括允许nginx用于连接处理的方法。最后它创造了一个Makefile。
官方参数说明:https://nginx.org/en/docs/configure.html
为方便以后支持tcp转发,推荐编译时添加stream模块,需要手动添加参数:--with-stream,

# ./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--user=nginx \
--group=nginx \
--with-compat \
--with-file-aio \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_random_index_module \
--with-http_realip_module \
--with-http_secure_link_module \
--with-http_slice_module \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_v2_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-ld-opt="-Wl,-E"
2.4 编译

# make
2.5 安装

# make install
3 启动与停止nginx服务

3.1 CentOS 6.*


[*]配置Nginx到系统服务
  配置Nginx启动脚本

vim /etc/init.d/nginx
  编写脚本文件

#! /bin/bash
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
#
# processname: nginx
# config:      /etc/nginx/nginx.conf
# pidfile:   /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/nginx.lock
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n "Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n "Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n "Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
;;
esac

[*]改变文件权限

chmod +x /etc/init.d/nginx

[*]添加到系统服务

chkconfig --add nginx

[*]设置系统为开机自启动

# chkconfig --list|grep nginx
nginx       0:关闭    1:关闭    2:关闭    3:关闭    4:关闭    5:关闭    6:关闭
# chkconfig nginx on
# chkconfig --list|grep nginx
nginx       0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭
#

[*]启动

#service nginx start

[*]停止

#service nginx stop

[*]重启

#service nginx restart

[*]查看运行状态

# service nginx status
nginx (pid23961) 正在运行...
#
3.2 CentOS 7.*


[*]配置 systemd Nginx 服务和设置 Nginx 自启动
centos 7 中采用 systemd 来管理系统,我们来为 nginx 创建服务文件,来实现通过 systemd 来管理 nginx 。
  创建 systemd 服务文件: /lib/systemd/system/nginx.service,内容如下:


Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID

WantedBy=multi-user.target

[*]添加开机自启动

# systemctl list-unit-files | grep nginx.service
nginx.service                              disabled
# systemctl enablenginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
# systemctl list-unit-files | grep nginx.service
nginx.service                              enabled
#

[*]启动

#systemctl start nginx.service

[*]停止

#systemctl stop nginx.service

[*]重启

#systemctl restart nginx.service

[*]查看运行状态

# systemctl status nginx.service
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2018-09-29 11:34:01 CST; 5h 37min ago
Docs: http://nginx.org/en/docs/
Main PID: 26114 (nginx)
CGroup: /system.slice/nginx.service
├─26114 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─26115 nginx: worker process
Sep 29 11:34:01 node1 systemd: Starting nginx - high performance web server...
Sep 29 11:34:01 node1 systemd: Started nginx - high performance web server.
#
4 常用参数

4.1 参数

nginx version: nginx/1.14.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h         : 帮助
-v            : 显示版本并退出
-V            : 显示版本并配置选项然后退出
-t            : 测试配置并退出
-T            : 测试配置,转储并退出
-q            : 在配置测试期间抑制非错误消息
-s signal   : 向主进程发送信号:stop(停止),quit(退出),reopen(重新打开),reload(重新加载)
-p prefix   : 设置前缀路径(默认值:/ etc/nginx/)
-c filename   : 指定配置文件启动(默认值:/etc/nginx/nginx.conf)
-g directives : 从配置文件中设置全局指令;
4.2 检查配置文件
  测试配置文件是否正确,在运行时需要重新加载配置的时候,此命令非常重要,用来检测所修改的配置文件是否有语法错误。

# nginx -t -c /etc/nginx/nginx.conf
4.3 修改Nginx配置文件

$ vi /etc/nginx/nginx.conf
# 运行用户
usernginx;
# 启动进程, 通常设置成和cpu的数据相等
worker_processes1;
# 全局错误日志及PID文件
error_log/var/log/nginx/error.log warn;
pid      /var/run/nginx.pid;
# 工作模式及连接数上限
events {
#epoll是多路复用IO(I/O Multiplexing)中的一种方式,
#仅用于linux2.6以上内核,可以大大提高nginx的性能
use   epoll;
#单个后台worker process进程的最大并发链接数
worker_connections1024;
}
http {
#设定mime类型,类型由mime.type文件定义
include       /etc/nginx/mime.types;
default_typeapplication/octet-stream;
#设定日志格式
log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log/var/log/nginx/access.logmain;
#
proxy_headers_hash_max_size 51200;
proxy_headers_hash_bucket_size 6400;
#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,
#对于普通应用,必须设为 on,
#如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,
#以平衡磁盘与网络I/O处理速度,降低系统的uptime.
sendfile      on;
#tcp_nopush   on;
#连接超时时间
#keepalive_timeout0;
keepalive_timeout65;
tcp_nodelay   on;
#开启gzip压缩
gzipon;
gzip_disable "MSIE ";
#设定请求缓冲
client_header_buffer_size   128k;
large_client_header_buffers4 128k;
include /etc/nginx/conf.d/*.conf;
#设定虚拟主机配置
server {
#侦听80端口
listen       80;
#定义使用 本地 访问
server_namelocalhost;
#charset koi8-r;
#设定本虚拟主机的访问日志
access_loglogs/nginx.dev.access.logmain;
#默认请求
location / {
#定义服务器的默认网站根目录位置
root   /data/www/html;
#定义首页索引文件的名称
indexindex.php index.html index.htm;
}
# 定义错误提示页面
#error_page404            /404.html;
# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504/50x.html;
location = /50x.html {
root   /data/www/html;
}
#静态文件,nginx自己处理
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
#过期30天,静态文件不怎么更新,过期可以设大一点,
#如果频繁更新,则可以设置得小一点。
expires 30d;
}
#PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
#    root         html;
fastcgi_pass   127.0.0.1:9000;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
include      fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#禁止访问 .htxxx 文件
location ~ /\.ht {
denyall;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_namesomenamealiasanother.alias;
#    location / {
#      root   html;
#      indexindex.html index.htm;
#    }
#}
# HTTPS server
#
#server {
#    listen       443 ssl;
#    server_namelocalhost;
#    ssl_certificate      cert.pem;
#    ssl_certificate_keycert.key;
#    ssl_session_cache    shared:SSL:1m;
#    ssl_session_timeout5m;
#    ssl_ciphersHIGH:!aNULL:!MD5;
#    ssl_prefer_server_cipherson;
#    location / {
#      root   html;
#      indexindex.html index.htm;
#    }
#}
}
  新建指定目录,用于存放http的server段:

mkdir -pv /etc/nginx/conf.d/
  可以把server段剪切到/etc/nginx/conf.d/目录下,以 ".conf" 结尾。

参考
  https://segmentfault.com/a/1190000016498647

END



页: [1]
查看完整版本: RHEL/CentOS 安装 nginx