1.下载linux版安装包
将下载的nginx安装包上传至ubuntu系统。
解压 nginx-1.22.0.tar.gz
包:
tar -zxvf ./nginx-1.22.0.tar.gz -C /home/$USER/soft/
2.安装nginx所需的依赖
sudo apt-get install gcc
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install zlib1g-dev
sudo apt-get install openssl libssl-dev
3.编译和安装nginx
在nginx根目录执行配置文件
./configure
编译和安装
sudo apt-get install make
sudo make && sudo make install
或者
sudo make -j32 && sudo make install
4.测试nginx是否安装成功:
/usr/local/nginx/sbin/nginx -v
5.设置开机启动
编写命令脚本
sudo touch /etc/init.d/nginx
sudo chmod a+x /etc/init.d/nginx
sudo vim /etc/init.d/nginx
#! /bin/sh
# 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
# chkconfig: 2345 55 25
# Description: Nginx startup script for the HTTP Server on Ubuntu. 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
#注意:这里的变量需要根据具体的环境而做修改。
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/usr/local/nginx/logs/$NAME.pid
ulimit -n 8192
case "$1" in
start)
echo -n "Starting $NAME... "
if [ -f $PIDFILE ];then
mPID=`cat $PIDFILE`
isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
if [ "$isStart" != '' ];then
echo "$NAME (pid `pidof $NAME`) already running."
exit 1
fi
fi
$NGINX_BIN -c $CONFIGFILE
if [ "$?" != 0 ] ; then
echo " failed"
exit 1
else
echo " done"
fi
;;
stop)
echo -n "Stoping $NAME... "
if [ -f $PIDFILE ];then
mPID=`cat $PIDFILE`
isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
if [ "$isStart" = '' ];then
echo "$NAME is not running."
exit 1
fi
else
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 [ -f $PIDFILE ];then
mPID=`cat $PIDFILE`
isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
if [ "$isStart" != '' ];then
echo "$NAME (pid `pidof $NAME`) already running."
exit 1
else
echo "$NAME is stopped"
exit 0
fi
else
echo "$NAME is stopped"
exit 0
fi
;;
restart)
$0 stop
sleep 1
$0 start
;;
reload)
echo -n "Reload service $NAME... "
if [ -f $PIDFILE ];then
mPID=`cat $PIDFILE`
isStart=`ps ax | awk '{ print $1 }' | grep -e "^${mPID}$"`
if [ "$isStart" != '' ];then
$NGINX_BIN -s reload
echo " done"
else
echo "$NAME is not running, can't reload."
exit 1
fi
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|restart|reload|status|configtest}"
exit 1
;;
esac
编写系统服务脚本
vim /lib/systemd/system/nginx.service
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
# This unit gets pulled automatically into multi-user.target by
[Unit]
Description=/etc/init.d/nginx
ConditionFileIsExecutable=/etc/init.d/nginx
After=network.target
[Service]
Type=forking
ExecStart=/etc/init.d/nginx start
ExecReload=/etc/init.d/nginx reload
ExecStop=/etc/init.d/nginx stop
ExecStatus=/etc/init.d/nginx status
TimeoutSec=0
RemainAfterExit=yes
#### 添加 ####
[Install]
WantedBy=multi-user.target
Alias=nginx.service
评论 (0)