安装类型
采用官方nginx源安装方法支持的环境
如果不支持,可以改为epel源
系统 | 版本 | 支持的平台 |
---|---|---|
RHEL/CentOS | 6.x | x86_64, i386 |
RHEL/CentOS | 7.4+ | x86_64, ppc64le |
也可以源码编译安装或直接yum安装。
域名解析请提前设置好
nginx版本选择
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
[root@node1 ~]# yum list | grep nginx
[root@node1 ~]# yum install nginx
[root@node1 ~]# nginx -v
以下为官方默认编译参数:
[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'
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
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
家目录 )
源码包下载地址: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
使用该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"
[root@node1 ~]# make
[root@node1 ~]# make install
配置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 ~]#
创建 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 ~]#
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 : 从配置文件中设置全局指令;
测试配置文件是否正确,在运行时需要重新加载配置的时候,此命令非常重要,用来检测所修改的配置文件是否有语法错误。
[root@node1 ~]# nginx -t -c /etc/nginx/nginx.conf
[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