ubuntu Nginx源码安装并启动

一:官方下载nginx源包

官方下载地址:http://nginx.org/en/download.html

下载版本:nginx 1.16.1  下载地址是:http://nginx.org/download/nginx-1.16.1.tar.gz

可以先点击CHANGES 查看一下改动,查看一些新版本的新特性,寻找最适合自己的nginx版本

二:编译安装

2.1:解压缩

tar zxvf nginx-1.16.1.tar.gz

2.2:编译安装

2.2.1 编译

编译选项官方提供的有:

--prefix=path   定义一个目录来保存你的nginx的提供功能的文件夹,就这好比我们安装软件的时候软件存放的目录,如果我们在编译的不指定安装位置,那么默认的位置/usr/local/nginx 目录

 

--sbin-path=path 设置nginx执行脚本的位置,这里如果设置在path变量里面,就可以在bash环境下,任意使用nginx命令,默认位置prefix/sbin/nginx  注意这里的prefix是

在配置文件里面配置的路径

--conf-path=path 配置nginx配置文件的路径,如果不指定这个选项,那么配置文件的默认路径就会是 prefix/conf/nginx.conf

--pid-path =path 配置nginx.pid file的路径,一般来说,进程在运行的时候的时候有一个进程id,这个id会保存在pid file里面,默认的pid file的放置位置是prefix/logs/nginx.pid

--error-log-path=path 设置错误日志的存放路径,如果不指定,就默认 prefix/logs/error.log

--http-log-path= path 设置http访问日志的路径,如果不指定,就默认 prefix/logs/access.log

--user=name  设置默认启动进程的用户,如果不指定,就默认 nobody

--group=name 设置这个用户所在的用户组,如果不指定,依然是nobody

这些是我们常用的编译选项,其他的可以均保持默认,如需特殊指定,可上nginx官网查阅 http://nginx.org/en/docs/configure.html

编译安装步骤如下:

①:sudo ./configure

这里我选择默认配置

(./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module  --add-module=/{my_software_path}/echo-nginx-module)

②:make  

③:sudo make install 

如果编译过程出现the HTTP rewrite module requires the PCRE library.的错误

>安装pcre  

sudo apt install libpcre3 libpcre3-dev

>ssl扩展模块出现错误,就安装

sudo apt install libssl-dev

然后重新执行上面①②③步骤

注:

扩展什么的都很顺利 但是make的时候还是出了问题 我确定所有需要的扩展都已经安装好了,出现问题如下:

$:/nginx-1.5.6$ make

cc1: all warnings being treated as errors
objs/Makefile:458: recipe for target 'objs/src/core/ngx_murmurhash.o' failed
make[1]: *** [objs/src/core/ngx_murmurhash.o] Error 1
make[1]: Leaving directory '/nginx-1.5.6'
Makefile:8: recipe for target 'build' failed

make: *** [build] Error 2

解决办法:

将对应的makefile文件夹中(在 nginx/nginx-1.16.1/objs) 找到 -Werrori 并去掉 在重新make即可

 

image.png

查了-Werrori意思之后 发现原来它要求GCC将所有的警告当成错误进行处理 所有导致错误输出 并不能进行下一步

 

注:源码的安装路径为 /usr/local/nginx/sbin/nginx 而采用apt安装的路径为/usr/sbin/nginx
apt安装的版本为1.15.10 源代码安装的版本是1.16.1
以下为了实现平移

  1. 结束nginx进程
  2. 将/usr/sbin/nginx备份一下 并创建软链接

 

mv /usr/sbin/nginx /usr/sbin/nginx_old
ln -s /usr/local/nginx/sbin/nginx  /usr/sbin/nginx

 

2.3:使用sysv-rc-conf(chkconfig)的方式管理nginx服务

2.3.1首先添加nginx服务管理的脚本

在 /etc/init.d  目录下新建一个nginx文件

下面是官方提供的Nginx开机启动脚本 【当然了,是我改过路径的!需要使用的请自己改一下文件路径谢谢】
需要修改的路径有这3个

NGINX_BIN= /usr/sbin/$NAME
CONFIGFILE= /usr/local/nginx/conf/$NAME.conf
PIDFILE= /usr/local/nginx/logs/nginx/$NAME.pid

# apt-get install sysv-rc-conf   #ubuntu下替代chkconfig的一个服务,自我感觉比chkconfig 更强大!
# vim  /etc/init.d/nginx

#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run 'update-rc.d -f nginx defaults', or use the appropriate command on your
# distro. For CentOS/Redhat run: 'chkconfig --add nginx'

### BEGIN INIT INFO
# Provides:         nginx
# Required-Start:   $all
# Required-Stop:    $all
# Default-Start:    2 3 4 5
# Default-Stop:     0 1 6
# Short-Description: starts the nginx web server
# Description:      starts nginx using start-stop-daemon
### END INIT INFO

# Author:   licess
# website:  http://lnmp.org

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN= /usr/sbin/$NAME
CONFIGFILE= /usr/local/nginx/conf/$NAME.conf
PIDFILE= /usr/local/nginx/logs/nginx/$NAME.pid

case "$1" in
    start)
       echo -n "Starting $NAME... "

       if netstat -tnpl | grep -q nginx;then
           echo "$NAME (pid `pidof $NAME`) already running."
           exit 1
       fi

       $NGINX_BIN -c $CONFIGFILE

       if [ "$?" != 0 ] ; then
           echo " failed"
           exit 1
       else
           echo " done"
       fi
       ;;

    stop)
       echo -n "Stoping $NAME... "

       if ! netstat -tnpl | grep -q nginx; then
           echo "$NAME is not running."
           exit 1
       fi

       $NGINX_BIN -s stop

       if [ "$?" != 0 ] ; then
           echo " failed. Use force-quit"
           exit 1
       else
           echo " done"
       fi
       ;;

   status)
       if netstat -tnpl | grep -q nginx; then
           PID=`pidof nginx`
           echo "$NAME (pid $PID) is running..."
       else
           echo "$NAME is stopped"
           exit 0
       fi
       ;;

   force-quit)
       echo -n "Terminating $NAME... "

       if ! netstat -tnpl | grep -q nginx; then
           echo "$NAME is not running."
           exit 1
       fi

       kill `pidof $NAME`

       if [ "$?" != 0 ] ; then
           echo " failed"
           exit 1
       else
           echo " done"
       fi
       ;;

   restart)
       $0 stop
       sleep 1
       $0 start
       ;;

   reload)
       echo -n "Reload service $NAME... "

       if netstat -tnpl | grep -q nginx; then
           $NGINX_BIN -s reload
           echo " done"
       else
           echo "$NAME is not running, can't reload."
           exit 1
       fi
       ;;

   configtest)
       echo -n "Test $NAME configure files... "

       $NGINX_BIN -t
       ;;

    *)
       echo "Usage: $0 {start|stop|force-quit|restart|reload|status|configtest}"
       exit 1
       ;;

esac

# chmod +x /etc/init.d/nginx
# sysv-rc-conf

退出按【q】 就行了
上次忘记说了,这次补一下  这个【1\2\3\4\5\0\6\S】启动选项最后有个【 S 】的选项,他的意思表示开机后就会运行的服务。
运行级别说明:
S   表示开机后就会运行的服务
0   表示关机
1   表示单用户模式 (类似windows的安全模式)
2   表示无网络服务的多用户模式
3   表示多用户模式
4   系统预留(暂没使用)
5   表示多用户图形模式
6   表示重启
打X 表示开启该服务。

 

三:Nginx命令

{start|stop|force-quit|restart|reload|status|configtest}

3.1.启动Nginx服务

sudo /etc/init.d/nginx start

3.2.优雅停止Nginx服务

sudo /etc/init.d/nginx quit

3.3.加载最新配置

sudo /etc/init.d/nginx reload

3.4.立即停止Nginx服务

sudo /etc/init.d/nginx stop
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值