Tag Archives: nginx

How to install Nginx from source with ‘Debian’ layout

1. 创建相关目录

$ sudo mkdir /etc/nginx
$ sudo mkdir /etc/nginx/conf.d
$ sudo mkdir /etc/nginx/sites-available
$ sudo mkdir /etc/nginx/sites-enabled
$ sudo mkdir /var/log/nginx
$ sudo mkdir -p /var/lib/nginx/body
$ sudo mkdir /var/lib/nginx/proxy
$ sudo mkdir /var/lib/nginx/fastcgi

2. 编译安装

$ ./configure –prefix=/usr \
–sbin-path=/usr/sbin/nginx \
–conf-path=/etc/nginx/nginx.conf \
–lock-path=/var/lock/nginx.lock \
–error-log-path=/var/log/nginx/error.log \
–http-log-path=/var/log/nginx/access.log \
–pid-path=/var/run/nginx.pid \
–user=www-data \
–group=www-data \
–http-client-body-temp-path=/var/lib/nginx/body \
–http-proxy-temp-path=/var/lib/nginx/proxy \
–http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
–with-md5=/usr/lib \
–with-sha1=/usr/lib \
–with-http_ssl_module \
–with-http_gzip_static_module \
–with-pcre \
–without-mail_pop3_module \
–without-mail_imap_module \
–without-mail_smtp_module \
–with-http_stub_status_module

$ make
$ sudo make install
$ sudo mv /usr/html /var/www/nginx-default

3. 修改配置文件
/etc/nginx/nginx.conf

user www-data;
worker_processes 5;

error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
server_tokens off;
include /etc/nginx/mime.types;
default_type application/octet-stream;

access_log /var/log/nginx/access.log;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}

/etc/nginx/sites-available/default

server {
listen 80;
server_name localhost;

location / {
root /var/www/nginx-default;
index index.html index.htm;
}

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /var/www/nginx-default;
}
}

启用default vhost

sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default

4. 创建init脚本
$ sudo vi /etc/init.d/nginx

#! /bin/sh

### 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
DAEMON=/usr/sbin/nginx
NAME=nginx
DESC=nginx

test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
. /etc/default/nginx
fi

set -e

. /lib/lsb/init-functions

case “$1” in
start)
echo -n “Starting $DESC: ”
start-stop-daemon –start –quiet –pidfile /var/run/$NAME.pid \
–exec $DAEMON — $DAEMON_OPTS || true
echo “$NAME.”
;;
stop)
echo -n “Stopping $DESC: ”
start-stop-daemon –stop –quiet –pidfile /var/run/$NAME.pid \
–exec $DAEMON || true
echo “$NAME.”
;;
restart|force-reload)
echo -n “Restarting $DESC: ”
start-stop-daemon –stop –quiet –pidfile \
/var/run/$NAME.pid –exec $DAEMON || true
sleep 1
start-stop-daemon –start –quiet –pidfile \
/var/run/$NAME.pid –exec $DAEMON — $DAEMON_OPTS || true
echo “$NAME.”
;;
reload)
echo -n “Reloading $DESC configuration: ”
start-stop-daemon –stop –signal HUP –quiet –pidfile /var/run/$NAME.pid \
–exec $DAEMON || true
echo “$NAME.”
;;
status)
status_of_proc -p /var/run/$NAME.pid “$DAEMON” nginx && exit 0 || exit $?
;;
*)
N=/etc/init.d/$NAME
echo “Usage: $N {start|stop|restart|reload|force-reload|status}” >&2
exit 1
;;
esac

exit 0

$ sudo chmod +x /etc/init.d/nginx
$ sudo /usr/sbin/update-rc.d -f nginx defaults

5. 启动、停止、重启nginx服务

sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx restart