企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] 由于yum安装比较简单,这里就不做介绍了。下面是我个人安装源码安装nginx的过程。 ### 依赖关系安装以及安装包 * * * * * 需要下载的安装包openssl,zlib,pcre,nginx;链接: https://pan.baidu.com/s/1c7ixeY 密码: r35q 版本号如下图,也可自行搜索下载。 ![](https://box.kancloud.cn/2016-08-11_57ac76269212e.png) * * * * * 其中openssl,zlib,pcre 安装方法如下(本人这里未安装,我是和nginx一起安装的,安装nginx会介绍): * * * * * ![](https://box.kancloud.cn/2016-08-11_57ac7626a905f.png) * * * * * zlib和pcre方法类似,就不宜一介绍了,或者是直接使用yum install -y openssl zlib pcre的方法安装依赖关系 ### 源码安装 * * * * * 首先添加用户nginx,实现以之运行nginx进程: ![](https://box.kancloud.cn/2016-08-11_57ac7626c9f20.png) * * * * * 下面就开始编译安装了: ~~~ tar xzf nginx-1.8.0.tar.gz cd nginx-1.8.0 ~~~ ~~~ ./configure \ --prefix=/app/nginx \ --sbin-path=/usr/sbin/nginx \ --conf-path=/app/nginx/conf/nginx.conf \ --error-log-path=/app/nginx/log/nginx/error.log \ --http-log-path=/app/nginx/log/access.log \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_flv_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --http-client-body-temp-path=/app/nginx/tmp/client/ \ --http-proxy-temp-path=/app/nginx/tmp/proxy/ \ --http-fastcgi-temp-path=/app/nginx/tmp/fcgi/ \ --http-uwsgi-temp-path=/app/nginx/tmp/uwsgi \ --http-scgi-temp-path=/app/nginx/scgi \ --with-pcre=/data/nginx_softwave/pcre \ --with-openssl=/data/nginx_softwave/openssl \ --with-zlib=/data/nginx_softwave/zlib ~~~ * * * * * * * * * * 对于上面的编译配置情况这里只说明下最后三项(with-pcre,with-openssl,with-zlib)后面的路径都是源码包解压后的路径,它会在安装nginx的时候自动去从这些路径去安装这些依赖关系。若在前面安装这些依赖关系,则后面三个选项可以去掉。关于configure的参数说明后面会有一节单独说明。 下图是配置情况: ![](https://box.kancloud.cn/2016-08-11_57ac7626de6fe.png) * * * * * 接下来执行 make && make install 等待编译安装完成即可。 * * * * * 测试以及启动nginx ~~~ nginx -t #测试nginx是否安装成功 nginx #启动nginx ps -ef | grep nginx | grep -v grep #检查nginx是否启动成功 nginx -s stop #关闭nginx服务 ps -ef | grep nginx | grep -v grep #检查nginx是否已经关闭 ~~~ 若在启动的过程中报错,类似‘emerg] 19481#0: mkdir() "/app/nginx/tmp/client" failed (2: No such file or directory’的错误的时候,手动mkdir -p /app/nginx/tmp 即可 我安装完成测试结果如下: ![](https://box.kancloud.cn/2016-08-11_57ac762708541.png) * * * * * 下面是访问结果: ![](https://box.kancloud.cn/2016-08-11_57ac762720fe7.png) ### 启动脚本 * * * * * 到这里我们的基本安装已经完成了,但是为了我们的nginx服务更加方便的管理,我们可以将nginx加入init启动服务。针对我上面的配置情况,我们需要编写一个起停的控制脚本。 起停脚本: vim nginx_init.sh ~~~ #!/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:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` 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 } 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 ~~~ * * * * * 还需要做如下事情: ~~~ cp ./nginx_init.sh /etc/init.d/nginx chmod +x /etc/init.d/nginx chkconfig --add nginx chkconfig nginx on ~~~ * * * * * 然后就可以做启动停止的测试了 ~~~ service nginx start service nginx status service nginx reload service nginx stop ~~~