1.安装nginx需要先安装安装openssl,pcre,zlib这三个包,这里是通过源码安装
先解压三个源码的压缩包,例如tar -zxvf openssl-1.0.0a.tar.gz
解压完成后进入openssl的目录,然后./config && make && make install 这三个命令可以分开执行 配置,编译,安装
配置的时候可以指定到其他的位置,使用./config --prefix=/opt/openssl_service这样指定安装到/opt/oppenssl_service这个目录下
安装前面的包的话可能提示缺少gcc包, 使用
yum -y install gcc-c++ 不过使用此命令的前提是要配置yum源,安装过程可能出现其他问题,需要自行百度解决
后面的pcre和zlib按照一样的解压步骤,然后使用./configure && make && make install安装
最后安装nginx ,使用的还是同样的步骤,我们一般把nginx安装在/opt/nginx_service这个目录下,所以解压完后进入目录,
使用./configure --prefix=/opt/nginx_service && make && make install 如果不指定的话会在安装到默认位置,nginx的默认位置是/usr/local/nginx
安装好了后进入安装目录,使用sbin/nginx启动,启动后可访问nginx所在ip,可以看到welcome to nginx页面即为安装成功
2.nginx设置开机启动
在/etc/init.d目录下 新建一个文本文件,可以使用vim nginx,然后将下面的代码复制进去然后保存。
在脚本中添加如下命令:
*********************************************************************************************************************************
#!/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
*********************************************************************************************************************************
红色的地方自己改一下相关的路径,像我们装到/opt/nginx_service这个目录下,那红色的部分的路径都要改到相关的路径下,
nginx.pid在/opt/nginx_service/logs/nginx.pid
然后授权 chmod +x nginx 授予可执行权限
然后就可以使用service nginx start|stop|restart来启动停止重启nginx了
然后
vi /etc/rc.local
添加
/etc/init.d/nginx start
或者使用
chkconfig --add nginx
chkconfig nginx on
也可以实现开机启动
/etc/rc.local中的脚本是开机时候执行的脚本,必须以exit 0 返回0来退出脚本,如果不能返回0,系统会卡着一直执行这个脚本而无法继续开机
chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息。谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接。首先使用chkconfig --list可以看到所有的有启动信息的服务列表,刚添加的nginx服务是不在这个里面的,需要添加进来,chkconfig --add nginx
添加进来之后再使用chkconfig --list或者chkconfig --list nginx可以看到这个服务,发现6个等级都是关闭的,这个时候开机是不会启动这个服务的,所以需要我们改一下启动信息,可以简单的使用chkconfig nginx on这样来启动,这样会默认将2 3 4 5这4个等级设置为开启,也可以具体一点的
chkconfig --level 35 nginx on
需要注意的是:
每个被chkconfig管理的服务需要在对应的init.d下的脚本加上两行或者更多行的注释。第一行告诉chkconfig缺省启动的运行级以及启动和停止的优先级。如果某服务缺省不在任何运行级启动,那么使用 - 代替运行级。第二行对服务进行描述,可以用\ 跨行注释。
第一行是必须加的 否则不能使用chkconfig --add nginx
例如,random.init包含三行:
# chkconfig: 2345 20 80
# description: Saves and restores system entropy pool for \
# higher quality random number generation
上面的第一行chkconfig: 2345 20 80这行是必须有,意思是运行级别2345,启动权限20 关闭权限80,如果2345不写的话,
可以用-代替,chkconfig: - 85 15表示不在任何运行级别启动
chkconfig --add servicename 在chkconfig工具服务列表中增加此服务,此时服务会被在/etc/rc.d/rcN.d中赋予K/S入口了;
ps:初次写博客,望大神勿喷!