作者:bell723_893 | 来源:互联网 | 2017-09-27 08:40
DownloadNginx最新稳定版本可以从这里http://nginx.org/en/download.html下载最新版本,比如:nginx-1.0.6.tar.gz编译安装假设下载文件在/usr/local/src目录下tarzxvfnginx-1.0.6.tar.gzcdnginx-1.0.6./configure—prefix=/usr/nginxma
Download Nginx最新稳定版本
可以从这里http://nginx.org/en/download.html 下载最新版本,比如:nginx-1.0.6.tar.gz
编译安装
假设下载文件在/usr/local/src目录下
tar zxvf nginx-1.0.6.tar.gz
cd nginx-1.0.6
./configure —prefix=/usr/nginx
make
make install
文件会被安装到/usr/nginx目录下
到/usr/nginx/sbin/目录下,执行sudo ./nginx,可以启动nginx了
静态文件服务器搭建
在/usr/nginx/conf/nginx.conf文件中添加下列配置 location ~ ^/(images|Javascript|js|css|flash|media|static)/ { root /opt/resources; expires 1d; }
创建/opt/resources目录,将css,images等文件夹复制到下面。 加入images目录下有logo.gif图片,启动nginx后,现在可以通过下面的方式查看: http://localhost/images/logo.gif
创建系统服务
cd /etc/init.d/
创建nginx脚本,内容如下:
- #!/bin/sh
-
- ### BEGIN INIT INFO
- # Provides: nginx
- # Required-Start:
- # Required-Stop:
- # Default-Start: 2 3 4 5
- # Default-Stop: 0 1 6
- # Short-Description: nginx
- # Description: nginx server
- ### END INIT INFO
-
- . /lib/lsb/init-functions
-
- PROGRAM=/usr/nginx/sbin/nginx
-
-
- test -x $PROGRAM || exit 0
-
- case "$1" in
- start)
- log_begin_msg "Starting Nginx server"
- /usr/nginx/sbin/nginx
- log_end_msg 0
- ;;
- stop)
- PID=`cat /usr/nginx/logs/nginx.pid`
- log_begin_msg "Stopping Nginx server"
- if [ ! -z "$PID" ]; then
- kill -15 $PID
- fi
- log_end_msg 0
- ;;
- status)
- ;;
- *)
- log_success_msg "Usage: service nginx {start|stop|status}"
- exit 1
- esac
-
- exit 0
然后运行下面的命令:
sudo chmod +x ./nginx
sudo update-rc.d nginx defaults
现在可以使用下面的命令了,重新启动nginx会自动启动
sudo service nginx start
sudo service nginx stop