nginx安装环境准备、shell脚本开机自启动nginx服务

2019-05-2508:17:32服务器及运维Comments3,005 views字数 3663阅读模式

(一) nginx安装环境准备文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/12880.html

nginx是 C 语言开发,建议在 linux 上运行,这里使用 Centos6.5 作为安装环境。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/12880.html

gcc
安装 nginx需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,
安装命令: yum install gcc-c++ -y
PCRE
PCRE(Perl Compatible Regular Expressions)是一个 Perl 库,包括 perl 兼容的正则表达式
库。nginx的 http 模块使用 pcre来解析正则表达式,所以需要在 linux 上安装 pcre库。
安装命令: yum install -y pcre pcre-devel
注:pcre-devel 是使用 pcre开发的一个二次开发库。nginx也需要此库。
zlib
zlib 库提供了很多种压缩和解压缩的方式,nginx 使用 zlib 对 http 包的内容进行 gzip,所
以需要在 linux 上安装 zlib 库。
安装命令: yum install -y zlib zlib-devel
openssl
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书
封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx不仅支持 http 协议,还支持 https(即在 ssl 协议上传输 http),所以需要在 linux
安装 openssl 库。
安装命令: yum install -y openssl openssl-devel
(二) 编译安装文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/12880.html

将 nginx-1.8.0.tar.gz拷贝至 linux 服务器。
解压:
tar -zxvf nginx-1.8.0.tar.gz
进入解压完成的目录: cd nginx-1.8.0
然后执行下面的命令:
./configure
–prefix=/usr/local/nginx
–pid-path=/var/run/nginx/nginx.pid
–lock-path=/var/lock/nginx.lock
–error-log-path=/var/log/nginx/error.log
–http-log-path=/var/log/nginx/access.log
–with-http_gzip_static_module
–http-client-body-temp-path=/var/temp/nginx/client
–http-proxy-temp-path=/var/temp/nginx/proxy
–http-fastcgi-temp-path=/var/temp/nginx/fastcgi
–http-uwsgi-temp-path=/var/temp/nginx/uwsgi
–http-scgi-temp-path=/var/temp/nginx/scgi
备注:上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建 temp及 nginx 目录
mkdir –p /var/temp/nginx
编译安装,
在解压的目录nginx-1.8.0依次执行下面两条命令
make
make install
到此安装完成.
(三) 启动,停止,命令操作,看效果文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/12880.html

启动 nginx
cd /usr/local/nginx/sbin/
./nginx
即可访问虚拟机上的 nginx:比如我安装的ip为http://192.168.88.220/
浏览器访问如下图:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/12880.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/12880.html

注意:执行./nginx 启动 nginx,这里可以-c指定加载的 nginx 配置文件,如下:
./nginx -c /usr/local/nginx/conf/nginx.conf
如果不指定-c,nginx 在启动时默认加载 conf/nginx.conf 文件,此文件的地址也可以在编译
安装 nginx 时指定./configure的参数(–conf-path= 指向配置文件(nginx.conf))
停止 nginx
方式 1,快速停止:
cd /usr/local/nginx/sbin
./nginx -s stop
此方式相当于先查出 nginx进程 id 再使用 kill 命令强制杀掉进程。
方式 2,完整停止(建议使用):
cd /usr/local/nginx/sbin
./nginx -s quit
此方式停止步骤是待 nginx进程处理任务完毕进行停止
重启 nginx
方式 1,先停止再启动(建议使用):
对 nginx进行重启相当于先停止 nginx再启动 nginx,即先执行停止命令再执行启动命令。
如下:
./nginx -s quit
./nginx
方式 2,重新加载配置文件:
当 nginx 的配置文件 nginx.conf修改后,要想让配置生效需要重启 nginx,使用-s reload 不用
先停止 nginx再启动 nginx即可将配置信息在 nginx 中生效,如下:
./nginx -s reload
(四) 开机自启动nginx服务文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/12880.html

1.编写shell 脚本,这里使用的是编写 shell 脚本的方式来处理nginx安装环境准备、shell脚本开机自启动nginx服务
vi /etc/init.d/nginx (输入下面的代码)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/12880.html

#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/12880.html

按 :wq 保存并退出.
设置文件的访问权限
chmod a+x /etc/init.d/nginx (a+x ==> all user can execute 所有用户可执行)
这样在控制台就很容易的操作 nginx了:查看 Nginx 当前状态、启动 Nginx、停止 Nginx、重
启 Nginx…文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/12880.html

nginx安装环境准备、shell脚本开机自启动nginx服务文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/12880.html

如果修改了 nginx 的配置文件 nginx.conf,也可以使用上面的命令重新加载新的配
运行,可以将此命令加入到 rc.local文件中,这样开机的时候 nginx就默认启动了
4.加入到 rc.local文件中
vi /etc/rc.local
加入一行 /etc/init.d/nginx start 保存并退出,下次重启会生效
---------------------
作者:itRexLin
来源:CSDN
原文:https://blog.csdn.net/itRexLin/article/details/90521924
版权声明:本文为博主原创文章,转载请附上博文链接!文章源自菜鸟学院-https://www.cainiaoxueyuan.com/yunwei/12880.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/yunwei/12880.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定