设为首页 收藏本站
查看: 963|回复: 0

[经验分享] RHEL/CentOS 安装 nginx

[复制链接]

尚未签到

发表于 2019-2-15 17:34:22 | 显示全部楼层 |阅读模式
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官方源(推荐)

[root@node1 ~]# vim /etc/yum.repos.d/nginx.repo
添加:
[nginx]
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

[root@node1 ~]# yum list | grep nginx
[root@node1 ~]# yum install nginx
1.3 查看版本

[root@node1 ~]# nginx -v
1.4 查看nginx 编译的参数
  以下为官方默认编译参数:

[root@node1 ~]# 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 libtool  patch -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

[root@node1 ~]# wget https://nginx.org/download/nginx-1.14.0.tar.gz
  解压

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

[root@node1 ~]# ./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 编译

[root@node1 ~]# make
2.5 安装

[root@node1 ~]# 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

  • 设置系统为开机自启动

[root@node1 ~]# chkconfig --list|grep nginx
nginx       0:关闭    1:关闭    2:关闭    3:关闭    4:关闭    5:关闭    6:关闭
[root@node1 ~]# chkconfig nginx on
[root@node1 ~]# chkconfig --list|grep nginx
nginx       0:关闭    1:关闭    2:启用    3:启用    4:启用    5:启用    6:关闭
[root@node1 ~]#

  • 启动

[root@node1 ~]#  service nginx start

  • 停止

[root@node1 ~]#  service nginx stop

  • 重启

[root@node1 ~]#  service nginx restart

  • 查看运行状态

[root@node1 ~]# service nginx status
nginx (pid  23961) 正在运行...
[root@node1 ~]#
3.2 CentOS 7.*


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

[Unit]
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
[Service]
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
[Install]
WantedBy=multi-user.target

  • 添加开机自启动

[root@node1 ~]# systemctl list-unit-files | grep nginx.service
nginx.service                                disabled
[root@node1 ~]# systemctl enable  nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
[root@node1 ~]# systemctl list-unit-files | grep nginx.service
nginx.service                                enabled
[root@node1 ~]#

  • 启动

[root@node1 ~]#  systemctl start nginx.service

  • 停止

[root@node1 ~]#  systemctl stop nginx.service

  • 重启

[root@node1 ~]#  systemctl restart nginx.service

  • 查看运行状态

[root@node1 ~]# 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[1]: Starting nginx - high performance web server...
Sep 29 11:34:01 node1 systemd[1]: Started nginx - high performance web server.
[root@node1 ~]#
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 检查配置文件
  测试配置文件是否正确,在运行时需要重新加载配置的时候,此命令非常重要,用来检测所修改的配置文件是否有语法错误。

[root@node1 ~]# nginx -t -c /etc/nginx/nginx.conf
4.3 修改Nginx配置文件

[root@lightserver ~]$ vi /etc/nginx/nginx.conf
# 运行用户
user  nginx;
# 启动进程, 通常设置成和cpu的数据相等
worker_processes  1;
# 全局错误日志及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_connections  1024;
}
http {
#设定mime类型,类型由mime.type文件定义
include       /etc/nginx/mime.types;
default_type  application/octet-stream;
#设定日志格式
log_format  main  '$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.log  main;
#
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_timeout  0;
keepalive_timeout  65;
tcp_nodelay     on;
#开启gzip压缩
gzip  on;
gzip_disable "MSIE [1-6]";
#设定请求缓冲
client_header_buffer_size   128k;
large_client_header_buffers  4 128k;
include /etc/nginx/conf.d/*.conf;
#设定虚拟主机配置
server {
#侦听80端口
listen       80;
#定义使用 本地 访问
server_name  localhost;
#charset koi8-r;
#设定本虚拟主机的访问日志
access_log  logs/nginx.dev.access.log  main;
#默认请求
location / {
#定义服务器的默认网站根目录位置
root   /data/www/html;
#定义首页索引文件的名称
index  index.php index.html index.htm;
}
# 定义错误提示页面
#error_page  404              /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_index  index.php;
fastcgi_param  SCRIPT_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 {
deny  all;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;
#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}
# HTTPS server
#
#server {
#    listen       443 ssl;
#    server_name  localhost;
#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;
#    ssl_session_cache    shared:SSL:1m;
#    ssl_session_timeout  5m;
#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers  on;
#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}
}
  新建指定目录,用于存放http的server段:

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

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

END




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-672733-1-1.html 上篇帖子: centos 部署jdk时区慢了8小时 下篇帖子: Mysql主从同步+读写分离(centos7)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表