Centos 安装 Nginx
1 源码安装Nginx
1.1 前期工具安装
# yum install gcc lsof wget zip elinks curl iotop
1.2 常用依赖安装
pcre pcre-devel zlib zlib-devel
# yum install pcre* zlib* openssl openssl-devel openssl*
1.3 添加 nginx 专用用户和组
# useradd -r www -s /sbin/nologin
1.4 源码下载 1.15.5 解压编译和安装
# wget http://nginx.org/download/nginx-1.15.5.tar.gz -P /usr/src
nginx安装文件放在 /usr/src 下面,安装其他模块时候可能需要其他模块src
# /usr/src/tar -zxvf nginx-1.15.5.tar.gz
# cd /usr/src/nginx-1.15.5
# ./configure --help (可以查看各类编译参数)
# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module
# make -j4
# make install
# /usr/local/ngins/sbin/nginx -v
nginx version: nginx/1.15.5 (安装成功)
# /usr/local/ngins/sbin/nginx -V (大写V 查看详细安装参数)
# /usr/local/ngins/sbin/nginx -t (检查配置文件)
1.5 目录权限更改
# chown www:www /usr/local/nginx -R
1.6 启动Nginx
# /usr/local/ngins/sbin/nginx
# ln -s /usr/local/nginx/sbin/nginx /bin/nginx (启动命令软链接)
将 nginx 作为系统服务管理下载 nginx 到/etc/init.d/,修改里面的路径然后赋予可执行权限。
# service nginx {start|stop|status|restart|reload|configtest}
# elinks http://127.0.0.1 --dump
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to [1]nginx.org.
Commercial support is available at [2]nginx.com.
Thank you for using nginx.
References
Visible links
1. http://nginx.org/
2. http://nginx.com/
1.7 版本查看 & 配置文件检查
# /usr/local/ngins/sbin/nginx -v
nginx version: nginx/1.15.5
# /usr/local/ngins/sbin/nginx -V (大写V 查看详细安装参数)
# /usr/local/ngins/sbin/nginx -t (检查配置文件)
启动NGINX / 端口进程查看
# /usr/local/nginx/sbin/nginx
# ps -ef | grep nginx
# netstat –ntpl
# lsof -i :80
1.8 隐藏版本号
A: 编辑 /root/nginx-1.15.5/src/core/nginx.h
修改或删除 #define NGINX_VERSION 中的数字
B: 修改 nginx.conf 文件中的
include mime.types;
default_type application/octet-stream;
server_tokens off; #隐藏软件版本信息
1.9 关闭nginx
# ./sbin/nginx -s stop (强制停止,离开关闭所有进程)
# ./sbin/nginx -s quit (优雅退出,等目前所有运行的进程结束后停止)
# ./sbin/nginx -s reload (平滑加载新配置文件,worker 旧>停>新配置)
# ./sbin/nginx -s reopen (旧log文件已经改名后,log平滑重新写入新文件)
或 pkill nginx
Nginx设置成服务并开机自动启动
Red Hat NGINX Init Script
在/etc/init.d下创建文件 nginx
#!/bin/sh # # 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 # config: /etc/sysconfig/nginx # 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/subsys/nginx make_dirs() { # make required directories user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` if [ -n "$user" ]; then if [ -z "`grep $user /etc/passwd`" ]; then useradd -M -s /bin/nologin $user fi options=`$nginx -V 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done fi } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs 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
内容参考nginx官方文档 https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/
需要注意的配置:
------------------------------------------------------------
nginx=”/usr/local/nginx/sbin/nginx” //修改成nginx执行程序的路径。
NGINX_CONF_FILE=”/usr/local/nginx/conf/nginx.conf” //修改成nginx.conf文件的路径。
------------------------------------------------------------
# chmod a+x /etc/init.d/nginx
# chkconfig --add /etc/init.d/nginx(将nginx服务加入chkconfig管理列表)
# chkconfig nginx on(最后设置开机自动启动)
2 使用YUM安装nginx
2.1 添加官方源
# vi /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
# yum install nginx
也可以yum install nginx-1.6.3安装指定版本
(前提是你去packages里看到有对应的版本,默认是最新版稳定版)
官方包路径 http://nginx.org/packages/centos/6/x86_64/RPMS/
yum安装rpm包会比编译安装简单很多,默认会安装许多模块,缺点无法安装第三方模块
官方说明
# sudo yum install yum-utils
# vi /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
HTTPS 站点
server {
listen 443 ssl;
server_name cnblog.xyz www.cnblog.xyz;
# ssl on;
ssl_certificate /usr/local/nginx/ssl/ssl.pem;
ssl_certificate_key /usr/local/nginx/ssl/ssl.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root /usr/share/nginx/html;
}
}