Nginx 安装
编译安装前所需要的准备:
1.GCC编译器
首先检查GCC是否安装,命令:gcc -v ,如果显示有相关版本信息,则说明已经安装好,没有就安装:
yum install -y gcc # -y参数表示一直确认安装 已安装则是更新操作
2.PCRE库
检查是否安装pcre
[root@www src]# rpm -qa pcre
pcre-8.32-15.el7_2.1.x86_64
Nginx的HTTP模块要用它来解析正则表达式。
[root@www src]# yum install -y pcre pcre-devel
pcre-devel是使用PCRE做二次开发时所需要的开发库。类似的你可以想到安装LAMP时安装的php-devel。
3.zlib库
查看是否安装
[root@www src]# rpm -qa zlib
zlib-1.2.7-17.el7.x86_64
gzip格式的压缩会用到它。
[root@www src]# yum install -y zlib zlib-devel
4.OpenSSL库
[root@www src]# rpm -qa openssl
openssl-1.0.1e-60.el7.x86_64
yum install -y openssl openssl-devel
5. 下载 安装
wget http://nginx.org/download/nginx-1.14.2.tar.gz
解压安装
tar -xzvf /home/rocky/Downloads/nginx-1.14.2.tar.gz
cd nginx-1.14.2/
./configure #生成 Makefile,为下一步的编译做准备
make #编译
make install #安装
6. 启动
创建nginx启动命令脚本
[root@www init.d]# cat /etc/init.d/nginx
#! /bin/bash # chkconfig: - 85 15 PATH=/usr/local/nginx DESC="nginx daemon" NAME=nginx DAEMON=$PATH/sbin/$NAME CONFIGFILE=$PATH/conf/$NAME.conf PIDFILE=$PATH/logs/$NAME.pid SCRIPTNAME=/etc/init.d/$NAME set -e [ -x "$DAEMON" ] || exit 0 do_start() { $DAEMON -c $CONFIGFILE || echo -n "nginx already running" } do_stop() { $DAEMON -s stop || echo -n "nginx not running" } do_reload() { $DAEMON -s reload || echo -n "nginx can't reload" } case "$1" in start) echo -n "Starting $DESC: $NAME" do_start echo "." ;; stop) echo -n "Stopping $DESC: $NAME" do_stop echo "." ;; reload|graceful) echo -n "Reloading $DESC configuration..." do_reload echo "." ;; restart) echo -n "Restarting $DESC: $NAME" do_stop do_start echo "." ;; *) echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2 exit 3 ;; esac exit 0
设置执行权限
chmod a+x /etc/init.d/nginx
注册成服务
chkconfig --add nginx
设置开机启动
chkconfig nginx on
7. 全局命令
[root@www profile.d]# cat nginx.sh
export NGINX_HOME=/usr/local/nginx
export PATH=$NGINX_HOME/sbin:$PATH
生效
[root@www profile.d]# source nginx.sh
[root@www profile.d]# nginx -v
nginx version: nginx/1.14.2
[root@www profile.d]# 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 is successful
8. 启动
[root@www profile.d]# nginx (完整命令带路径)
[root@www profile.d]# curl localhost 验证
附 Nginx部分控制命令
默认Nginx安装在/usr/local/nginx/中,因此
- /usr/local/nginx/sbin/nginx #默认启动方式 start
- /usr/local/nginx/sbin/nginx -t #测试配置信息
- /usr/local/nginx/sbin/nginx -v #显示版本信息,-V(大V)显示编译时的参数
- /usr/local/nginx/sbin/nginx -s stop #快速停止服务
- /usr/local/nginx/sbin/nginx -s quit #正常停止服务
- /usr/local/nginx/sbin/nginx -s reload #重启
Q: nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
A: [root@localhost nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
使用nginx -c的参数指定nginx.conf文件的位置
来源:https://www.cnblogs.com/rocky-fang/p/10321178.html