Linux 安装nginx
1、检测所需扩展是否都已安装:
1 rpm -qa | grep "pcre" 2 rpm -qa | grep "openssl" 3 rpm -qa | grep "wget" 4 rpm -qa | grep "zlib" 5 6 # 如果缺少某一扩展 用yum安装即可 7 yum install pcre* -y 8 yum install openssl* -y 9 yum install zlib -y 10 yum install zlib-devel -y
2、下载nginx安装包:
wget http://nginx.org/download/nginx-1.8.0.tar.gz
3、解压:
tar -zxvf nginx-1.8.0.tar.gz
4、安装:
1 cd nginx-1.8.0 2 ./configure --prefix=/usr/local/nginx --error-log-path=/var/log/nginx/error.log
--pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx --user=www --group=www
--without-select_module --without-poll_module --with-file-aio --with-http_ssl_module
--with-http_realip_module --with-http_stub_status_module --without-http_ssi_module
--without-http_userid_module --without-http_uwsgi_module --without-http_scgi_module
--without-http_memcached_module --without-http_limit_conn_module
--without-http_limit_req_module --http-log-path=/var/log/nginx/access.log
--http-client-body-temp-path=/tmp/client-body-temp --http-proxy-temp-path=/tmp/proxy-temp
--http-fastcgi-temp-path=/tmp/fastcgi-temp --without-mail_pop3_module --without-mail_imap_module
--without-mail_smtp_module --add-module=../agentzh-headers-more-nginx-module-*/
3 make && make install
5、启动:
1 cd /usr/local/nginx/sbin 2 ./nginx 3 4 # 重新载入配置文件 5 ./nginx -s reload
6、添加到service 服务中:
1 # 首先在/usr/bin 目录下创建nginx 软连 2 cd /usr/bin 3 ln -s /usr/local/nginx/sbin/nginx nginx 4 5 # 编写nginx 启动shell 脚本 6 cd /etc/init.d 7 vim nginx 8 9 # 代码在下方
nginx 启动代码:
1 #!/bin/bash 2 ###################################################### 3 ## Modify by adam.li@ismole.com at 2012-07-20 13:52 ## 4 ## Compatible with RedHat/CentOS Debian/Ubuntu SUSE ## 5 ###################################################### 6 7 # Support chkconfig on RedHat/CentOS 8 # chkconfig: 2345 90 60 9 # description: nginx 10 # processname: nginx 11 # config: /usr/local/nginx/conf/nginx.conf 12 # pidfile: /var/run/nginx.pid 13 14 # Support Linux Standard Base Core Specification 3.2 15 ### BEGIN INIT INFO 16 # Provides: nginx 17 # Required-Start: $local_fs $syslog $network 18 # Required-Stop: 19 # Default-Start: 2 3 4 5 20 # Default-Stop: 0 S 1 6 21 # Short-Description: start/stop nginx daemon 22 # Description: nginx is a web server, http and mail proxy server 23 ### END INIT INFO 24 25 #debug 26 #set -x 27 #check unbound variables 28 #set -u 29 # Scripts PATH enviroment 30 export PATH="/usr/bin:/bin:/usr/sbin:/sbin" 31 # disable core dump 32 ulimit -c 0 33 # return value 34 RETVAL=0 35 # nginx general config 36 PROG="nginx" 37 SYMLINK=$(readlink -f /usr/local/nginx) 38 BASEDIR=${SYMLINK:-/usr/local/nginx} 39 DAEMON="${BASEDIR}/sbin/nginx" 40 CONF="${BASEDIR}/conf/nginx.conf" 41 PIDFILE="/var/run/${PROG}.pid" 42 LOCKFILE="/var/lock/$PROG" 43 # nginx start timeout milliscond 44 STARTTIME=10000 45 # nginx stop timeout milliscond 46 STOPTIME=10000 47 48 #Common Function 49 color_msg(){ 50 local COLOR=$1 51 local MSG=$2 52 OFFSET="\033[60G" 53 NORMAL="\033[0m" 54 case $COLOR in 55 red) 56 COLOR="\033[1;40;31m" 57 ;; 58 green) 59 COLOR="\033[1;40;32m" 60 ;; 61 yellow) 62 COLOR="\033[1;40;33m" 63 ;; 64 *) 65 COLOR="\033[0m" 66 ;; 67 esac 68 echo -en "$OFFSET [$COLOR $MSG $NORMAL" 69 echo "]" 70 } 71 72 configtest() { 73 echo -n "Configtest $PROG : " 74 $DAEMON -tq -c $CONF >/dev/null 2>&1 75 if [ $? -eq 0 ] ;then 76 color_msg green SUCCESS 77 else 78 color_msg red FAILED && exit 1 79 fi 80 } 81 82 start() { 83 echo -n "Starting $PROG : " 84 PROC_PID=$(pgrep -P 1 -u root ^$PROG) 85 if [ -n "$PROC_PID" ]; then 86 echo -n "is already running." 87 color_msg yellow WARNING 88 else 89 $DAEMON -c $CONF >/dev/null 2>&1 90 if [ $? -eq 0 ]; then 91 color_msg green SUCCESS && touch $LOCKFILE 92 else 93 color_msg red FAILED && exit 1 94 fi 95 fi 96 } 97 98 stop() { 99 echo -n "Stopping $PROG : " 100 PROC_PID=$(pgrep -P 1 -u root ^$PROG) 101 if [ -z "$PROC_PID" ]; then 102 echo -n "is not running." 103 color_msg yellow WARNING 104 else 105 kill -TERM ${PROC_PID} >/dev/null 2>&1 106 while [ "$STOPTIME" -gt 0 ]; do 107 kill -0 ${PROC_PID} >/dev/null 2>&1 || break 108 STOPTIME=$(($STOPTIME-1)) 109 echo -n "." && sleep 0.001s 110 done 111 if [ "$STOPTIME" -le 0 ]; then 112 color_msg red TIMEOUT && exit 1 113 else 114 color_msg green SUCCESS 115 rm -f $PIDFILE $LOCKFILE 116 fi 117 fi 118 } 119 120 restart() { 121 echo -n "Restart $PROG : " 122 echo 123 echo -en "\t" && stop 124 echo -en "\t" && start 125 } 126 127 reload() { 128 echo -n "Reloading $PROG : " 129 PROC_PID=$(pgrep -P 1 -u root ^$PROG) 130 if [ -n "$PROC_PID" ]; then 131 kill -HUP ${PROC_PID} >/dev/null 2>&1 132 if [ $? -eq 0 ]; then 133 color_msg green SUCCESS 134 else 135 color_msg red FAILED && exit 1 136 fi 137 else 138 echo -n "is not running." 139 color_msg yellow WARNING 140 fi 141 } 142 143 status() { 144 PROC_PID=$(pgrep -P 1 -u root ^$PROG) 145 if [ -z "$PROC_PID" ];then 146 echo "$PROG is stopped" 147 exit 3 148 else 149 echo "$PROG (pid $PROC_PID) is running..." 150 exit 0 151 fi 152 } 153 154 quit() { 155 echo -n "Quit $PROG : " 156 PROC_PID=$(pgrep -P 1 -u root ^$PROG) 157 if [ -z "$PROC_PID" ]; then 158 echo -n "is not running." 159 color_msg yellow WARNING 160 else 161 kill -QUIT ${PROC_PID} >/dev/null 2>&1 162 while [ "$STOPTIME" -gt 0 ]; do 163 kill -0 ${PROC_PID} >/dev/null 2>&1 || break 164 STOPTIME=$(($STOPTIME-1)) 165 echo -n "." && sleep 0.001s 166 done 167 if [ "$STOPTIME" -le 0 ]; then 168 color_msg red TIMEOUT && exit 1 169 else 170 color_msg green SUCCESS 171 rm -f $PIDFILE $LOCKFILE 172 fi 173 fi 174 } 175 176 logrotate() { 177 echo -n "Re-opening $PROG log file : " 178 PROC_PID=$(pgrep -P 1 -u root ^$PROG) 179 if [ -z "$PROC_PID" ]; then 180 echo -n "is not running." 181 color_msg yellow WARNING 182 else 183 kill -USR1 ${PROC_PID} >/dev/null 2>&1 184 if [ $? -eq 0 ]; then 185 color_msg green SUCCESS 186 else 187 color_msg red FAILED && exit 1 188 fi 189 fi 190 } 191 192 case "$1" in 193 configtest) 194 configtest 195 ;; 196 start) 197 configtest 198 start 199 ;; 200 stop) 201 stop 202 ;; 203 restart|try-restart) 204 configtest 205 restart 206 ;; 207 reload|force-reload) 208 configtest 209 reload 210 ;; 211 status) 212 status 213 ;; 214 quit) 215 quit 216 ;; 217 logrotate) 218 logrotate 219 ;; 220 *) 221 echo $"Usage: $0 {configtest|start|stop|restart|reload|status|quit|logrotate}" 222 exit 1 223 ;; 224 esac
最后启动:
# 添加可执行权限
chmod +x nginx
service nginx start