记录下centos下配置nginx与jar开机自启
首先是jar包
#首先在项目所在文件夹或自己定义文件夹新建空白脚本vim /usr/local/demo/start.sh#接下来直接根据自己的地址写入下列四行# 第一行为jdk所在位置,第二行为固定写法export JAVA_HOME=/usr/local/jdkexport PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH#进入到项目所在文件夹cd /usr/local/demo/#设置启动语句java -jar demo-admin.jar > log.out 2>&1 &#给start.sh赋予执行权限chmod +x /usr/local/demo/start.sh#修改系统文件/etc/rc.local,将启动脚本写入vi /etc/rc.local/data/startShell/start.sh#使用root账户给rc.local可运行权限chmod +x /etc/rc.local#最后使用命令重启服务器reboot
然后是nginx
首先,在linux系统的/etc/init.d/目录下创建nginx文件,使用如下命令:
vi /etc/init.d/nginx插入以下内容
-
## 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 0nginx="/mnt/nginx/sbin/nginx"prog=$(basename $nginx)NGINX_CONF_FILE="/mnt/nginx/nginx.conf"[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginxlockfile=/var/lock/subsys/nginxmake_dirs() {# make required directoriesuser=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`if [ -z "`grep $user /etc/passwd`" ]; thenuseradd -M -s /bin/nologin $userfioptions=`$nginx -V 2>&1 | grep 'configure arguments:'`for opt in $options; doif [ `echo $opt | grep '.*-temp-path'` ]; thenvalue=`echo $opt | cut -d "=" -f 2`if [ ! -d "$value" ]; then# echo "creating" $valuemkdir -p $value && chown -R $user $valuefifidone}start() {[ -x $nginx ] || exit 5[ -f $NGINX_CONF_FILE ] || exit 6make_dirsecho -n $"Starting $prog: "daemon $nginx -c $NGINX_CONF_FILEretval=$?echo[ $retval -eq 0 ] && touch $lockfilereturn $retval}stop() {echo -n $"Stopping $prog: "killproc $prog -QUITretval=$?echo[ $retval -eq 0 ] && rm -f $lockfilereturn $retval}restart() {configtest || return $?stopsleep 1start}reload() {configtest || return $?echo -n $"Reloading $prog: "killproc $nginx -HUPRETVAL=$?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" instart)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 需要根据nginx的安装路径修改下面这两项配置:
nginx=”/usr/sbin/nginx” 修改成nginx执行程序的路径。
NGINX_CONF_FILE=”/etc/nginx/nginx.conf” 修改成配置文件的路径。按esc退出编辑 输入:wq保存并退出,然后执行以下命令赋予文件权限
chmod a+x /etc/init.d/ngin使用chkconfig进行管理,先将nginx服务加入chkconfig管理列表:
chkconfig --add /etc/init.d/nginx设置终端模式开机启动:
chkconfig nginx on重启服务器完成
