博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CentOS7.3编译安装Nginx设置开机启动
阅读量:4687 次
发布时间:2019-06-09

本文共 5114 字,大约阅读时间需要 17 分钟。

起因

最近想玩nginx了,本来用yum -y install nginx安装也启动好了,但是买了本《Nginx高性能Web服务器详解》,我咋能辜负我的书费呢?于是我就直接ps -ef |grep nginx kill -QUIT master的pid,然后yum -y remove nginx了。没错,就是这么帅。

经过

下载nginx

当然是去nginx(http://nginx.org/en/download.html)主页了,没错我现在安装的就是稳定版1.14.2了。

进入放置nginx的目录,我是/usr/local/tools/nginx
输入命令: wget http://nginx.org/download/nginx-1.14.2.tar.gz,这儿就等着吧。

解压

接下来解压targz包,你要是喜欢看动画呢,就输入 tar -zxvf nginx-1.14.2.tar.gz ,你要是不看呢就输入tar xf nginx-1.14.2.tar.gz

安装nginx

安装必要的工具:yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel open openssl-devel gd-devel perl-devel perl-ExtUtils-Embed.

接下来未来保证源文件和二进制文件不重复,我进行了一波骚操作: mv nginx-1.14.2 nginx-1.14.2-installer mkdir nginx-1.14.2就是把源码文件夹重命名,新建了一个nginx按照目录,现在的源码在/usr/local/tools/nginx\nginx-1.14.2-installer,我要安装在/usr/local/tools/nginx/nginx-1.14.2目录里。
进入源码文件: cd nginx-1.14.2-installer,
编译文件:./configure --prefix=/usr/local/tools/nginx/nginx-1.14.2,注意这儿改成你的目录就行了,有两点你的注意。其一、你的nginx.lock位置是在/var/lock/nginx.lock;其二、只安装了标准模块;其三、用户不限制,任何人都能启动nginx
我的完整的配置选项是这样子的(如果你也是用的修改的配置,一定要把这个配置信息记下来,否则以后查错,该配置你会哭的。):/configure --prefix=安装目录 --sbin-path=sbin/nginx --conf-path=conf/nginx.conf --pid-path=logs/nginx.pid --error-log-path=logs/error.log --http-log-path=logs/access.log --lock-path=logs/lock/subsys/nginx --user=你要启动nginx的用户 --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_perl_module --with-ld-opt="-Wl,-E" --with-http_image_filter_module--prefix是安装目录,一定要使用绝对路径;我把lock文件也放在了安装目录下的logs下;启用了几个基本模块
一定要注意这时候是不是报错了。
然后就是 make && make install

测试安装成功

进入安装目录:cd ../nginx-1.14.2,

执行启动: ./sbin/nginx,如果启动报错的话,看下错误信息,一般情况下是啥也没有。
查看服务: ps -ef |grep nginx
694294-20190320182010533-1798175995.png

停止服务:kill -QUIT 7555,这个7555对应的是上图master process的进程编号,为了后续启动这儿一定要停了服务

查看服务: ps -ef |grep nginx
694294-20190320182413103-816596705.png

配置系统服务

配置系统服务:vim /etc/init.d/nginx

将下面这段复制进去(这是官网提供的加入系统服务脚本,链接 ,找到nginx="/usr/local/tools/nginx/nginx-1.14.2/sbin/nginx" NGINX_CONF_FILE="/usr/local/tools/nginx/nginx-1.14.2/conf/nginx.conf"这两句改成你自己的目录):

#!/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:      /usr/local/tools/nginx/nginx-1.14.2/conf/nginx.conf# config:      /etc/sysconfig/nginx# pidfile:     /usr/local/tools/nginx/nginx-1.14.2/logs/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 0nginx="/usr/local/tools/nginx/nginx-1.14.2/sbin/nginx"prog=$(basename $nginx)NGINX_CONF_FILE="/usr/local/tools/nginx/nginx-1.14.2/conf/nginx.conf"[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginxlockfile=/usr/local/tools/nginx/nginx-1.14.2/logs/lock/subsys/nginxmake_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 2esac

配置启动脚本权限:chmod a+x /etc/init.d/nginx

启动nginx: /etc/init.d/nginx start
停止nginx: /etc/init.d/nginx stop
694294-20190320182829412-237661203.png

加入系统服务:chkconfig --add /etc/init.d/nginx

使用systemctl启动nginx: systemctl start nginx
使用systemctl停止nginx: systemctl stop nginx
694294-20190320183139358-1272479311.png

开机启动

配置开机启动:vi /etc/rc.local在最后加一句/etc/init.d/nginx start

结尾

打完收功!

转载于:https://www.cnblogs.com/Lenbrother/p/10567005.html

你可能感兴趣的文章
选拔赛 I 点进来吧,这里有你想要的
查看>>
SQL 优化经验总结34条
查看>>
开源 视频会议 收藏
查看>>
核心J2EE模式 - 截取过滤器
查看>>
test1
查看>>
.net开源CMS
查看>>
JdbcTemplate
查看>>
第一次使用maven记录
查看>>
SharePoint服务器端对象模型 之 使用CAML进展数据查询
查看>>
Building Tablet PC Applications ROB JARRETT
查看>>
Adobe® Reader®.插件开发
查看>>
【POJ 3461】Oulipo
查看>>
Alpha 冲刺 (5/10)
查看>>
使用Siege进行WEB压力测试
查看>>
斑马为什么有条纹?
查看>>
android多层树形结构列表学习笔记
查看>>
Android_去掉EditText控件周围橙色高亮区域
查看>>
《构建之法》第一、二、十六章阅读笔记
查看>>
Pandas基础(十一)时间序列
查看>>
arrow:让Python的日期与时间变的更好
查看>>