一.Nginx 安装
http://blog.csdn.net/vwadev/article/details/50052369
1.nginx的安装
前提:完整的编译环境 gcc g++ make 开发库等
apt-get install build-essential
apt-get install libtool
1.选定源码目录
可以是任何目录,本文选定的是/usr/local/src
2.安装PCRE库
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包:
cd /usr/local/src
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.37.tar.gz
tar -zxvf pcre-8.37.tar.gz
cd pcre-8.37
./configure
make
make install
3.安装zlib库
http://zlib.net/zlib-1.2.8.tar.gz 下载最新的 zlib 源码包,使用下面命令下载编译和安装 zlib包:
cd /usr/local/src
wget http://zlib.net/zlib-1.2.8.tar.gz
tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make
make install
4.安装ssl(某些vps默认没装ssl)
cd /usr/local/src
wget http://www.openssl.org/source/openssl-1.0.2d.tar.gz
tar -zxvf openssl-1.0.2d.tar.gz
cd openssl-1.0.2d
./config --prefix=/usr/local --openssldir=/usr/local/ssl
make && make install
./config shared --prefix=/usr/local --openssldir=/usr/local/ssl
make clean
make && make install
5.安装nginx
Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,
下面是把 Nginx 安装到 /usr/local/nginx 目录下的详细步骤:
cd /usr/local/src
wget http://nginx.org/download/nginx-1.9.6.tar.gz
tar -zxvf nginx-1.9.6.tar.gz
cd nginx-1.9.6
./configure --prefix=/usr/local/nginx2 \
--with-http_ssl_module \
--with-stream \
--with-pcre=/usr/local/src/pcre-8.37 \
--with-zlib=/usr/local/src/zlib-1.2.8 \
--with-openssl=/usr/local/src/openssl-1.0.2d
make
make install
--with-stream 用于激活支持TCP
--with-pcre=/usr/src/pcre-8.37 指的是pcre-8.37 的源码路径。
--with-zlib=/usr/src/zlib-1.2.8 指的是zlib-1.2.8 的源码路径。
配置说明
http://nginx.org/en/docs/stream/ngx_stream_core_module.html
2.nginx 启动、停止、平滑启动、平滑升级
Nginx 启动
确保系统的 80 端口没被其他程序占用
netstat -ano|grep 80
如果查不到结果后执行,有结果则忽略此步骤(ubuntu下必须用sudo启动,不然只能在前台运行)
sudo /usr/local/nginx/nginx
打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经 安装并运行成功。
启动nginx,可以执行一下命令(默认安装位置):
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
参数“-c”指定了配置文件的路径,如果不加,则Nginx会默认加载其安装目录的conf子目录中的ngin.conf
Nginx 停止
nginx的停止方法有很多种,一般是发送系统信号给nginx主进程来停止nginx。我们通过ps命令来查找nginx的主进程号
ps -ef |grep nginx
备注信息为“master process”它表示主进程。为“worker”的是子进程。
如果在nginx.conf中指定了pid文件存放路径,则该文件存放的就是nginx的主进程号。
如果没有指定,则默认存放在Nginx安装目录的log目录下。所以我们还可以这样做:
kill -信号类型 '/usr/local/nginx/logs/nginx.pid'
nginx支持以下几种信号:
TERM,INT :快速关闭
QUIT:从容关闭
HUP:平滑启动
USR1:重新打开日志文件
USR2:平滑升级可执行程序
EINCH:从容关闭工作进程
(1)从容停止nginx
kill -QUIT Nginx主进程号
(2)快速停止Nginx
kill -TERM Nginx主进程号
(3)强制停止所有nginx进程
pkill -9 nginx
Nginx 平滑升级
当需要将正在运行的nginx升级、添加/删除服务器模块时,可以在不中断的情况下使用新版本、重编译的nginx可执行程序替换旧版本的可执行程序。步骤如下:
(1)备份旧的可执行程序
(2)发送以下指令
kill -USR2 旧的版本nginx主进程号
(3)旧版本的nginx的主进程将重命名他的pid文件为.oldbin。然后执行新版本的nginx可执行程序。依次启动新的主进程和新的工作进程。
(4)此时新旧版本的nginx会同时运行,共同处理输入请求。要逐步停止旧版本的nginx实例,需要发送WINCH信号给旧的主进程,然后他的工作进程就从容关闭:
kill -WINCH 旧版本的主进程号
(5)一段时间后,旧的工作进程处理完所有的已连接请求后退出,仅有新的工作进程来处理输入请求。
(6)这时候我们可以决定是使用新的版本还是恢复到旧版本。
Nginx 平滑启动 / Nginx 重加载
如果改变了配置文件,想重启让其生效,同样可以通过发送系统信号给Nginx主进程。
不过,在重启之前,要确认配置文件的语法是正确的,否则将不会加载新的配置项。
通过以下语句测试配置文件语法是否正确:
/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf 其中-t表示测试,并不真正执行。
然后,通过以下命令重加载Nginx配置:
kill -HUP master进程号
执行上面命令之后,Nginx运行新的工作进程,旧工作进程继续为已有的连接服务,等所有旧的连接成功后,旧的工作进程才被关闭。
Nginx 启动脚本
#!/bin/sh # chkconfig: 2345 85 15 # description:Nginx Server
NGINX_HOME=/usr/local/nginx NGINX_SBIN=$NGINX_HOME/sbin/nginx NGINX_CONF=$NGINX_HOME/conf/nginx.conf NGINX_PID=$NGINX_HOME/logs/nginx.pid
NGINX_NAME="Nginx"
. /etc/rc.d/init.d/functions
if [ ! -f $NGINX_SBIN ] then echo "$NGINX_NAME startup: $NGINX_SBIN not exists! " exit fi
start() { $NGINX_SBIN -c $NGINX_CONF ret=$? if [ $ret -eq 0 ]; then action $"Starting $NGINX_NAME: " /bin/true else action $"Starting $NGINX_NAME: " /bin/false fi }
stop() { kill `cat $NGINX_PID` ret=$? if [ $ret -eq 0 ]; then action $"Stopping $NGINX_NAME: " /bin/true else action $"Stopping $NGINX_NAME: " /bin/false fi }
restart() { stop start }
check() { $NGINX_SBIN -c $NGINX_CONF -t }
reload() { kill -HUP `cat $NGINX_PID` && echo "reload success!" }
relog() { kill -USR1 `cat $NGINX_PID` && echo "relog success!" }
case "$1" in start) start ;; stop) stop ;; restart) restart ;; check|chk) check ;; status) status -p $NGINX_PID ;; reload) reload ;; relog) relog ;; *) echo $"Usage: $0 {start|stop|restart|reload|status|check|relog}" exit 1 esac |
上面是nginx的启动脚本,只要把它拷贝至/etc/init.d目录下,就可以通过service nginx start等目录操作nginx。
3.nginx卸载
############### NGINX 卸载 ##################
apt卸载nginx方法
卸载方法1.
# 删除nginx,保留配置文件
sudo apt-get remove nginx
#删除配置文件
rm -rf /etc/nginx
卸载方法2.
#删除nginx连带配置文件
sudo apt-get purge nginx # Removes everything.
#卸载不再需要的nginx依赖程序
sudo apt-get autoremove
二 .Nginx http 下载文件访问配置
Nginx.conf文件的配置如下:
user root root;
#nginx进程数,建议按照cpu数目来指定,一般为它的倍数。 worker_processes 4;
#为每个进程分配cpu,上例中将8个进程分配到8个cpu,当然可以写多个,或者将一个进程分配到多个cpu。 worker_cpu_affinity 00010000 00100000 01000000 10000000;
error_log /usr/local/nginx/logs/error.log; error_log /usr/local/nginx/logs/error.log notice; error_log /usr/local/nginx/logs/error.log info; #pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 204800;
#use epoll 使用epoll的I/O模型。 #worker_connections 每个进程允许的最多连接数 events { use epoll; worker_connections 204800; }
http { include mime.types; default_type application/octet-stream;
sendfile on; keepalive_timeout 65;
server { listen 12380; server_name localhost;
location / { root html;#根目录,在/usr/local/nginx/html下 index index.html ; } } }
|
由此可访问:http://192.168.90.5:12380/a.c; http://192.168.90.5:12380/a.html; 来获取文件。
三.Nginx http 实现上传功能
一.加入上传模块
参考网站:
http://www.open-open.com/lib/view/open1435198673794.html
http://blog.csdn.net/waden/article/details/7040123 ;
一. 相关模块安装
查看Nginx是否安装了这两个模块(nginx_upload_module:上传 和nginx_uploadprogress_module:用来查看上传进度的),
使用命令:
$ nginx -V (注意是大写),可以
查看Nginx当时编译时候的参数,如果发现有上述两个模块,说明Nginx已经安装了这两个模块。
如果没有的话,就需要安装这两个Nginx模块。
1. 下载nginx_upload_module
https://github.com/vkholodkov/nginx-upload-module/tree/2.2
二、Nginx添加编译选项
由于这两个模块不在Nginx源代码中,需要重新编译Nginx,在编译选项中加上:
如下:
./configure --prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-stream \
--with-pcre=/usr/local/src/pcre-8.37 \
--with-zlib=/usr/local/src/zlib-1.2.8 \
--with-openssl=/usr/local/src/openssl-1.0.2d \
--add-module=/usr/local/src/nginx_upload_module-2.2
$ make
$ make install
二.安装php
http://www.cnblogs.com/fly1988happy/archive/2011/12/14/2288096.html;
1.获取安装文件: http://www.php.net/downloads.php php-7.0.2.tar.gz
2. 获取安装php需要的支持文件:http://download.csdn.net/download/netlong339/1351852 libxml2-2.6.32.tar.gz
2.安装libxml2
tar zxvf libxml2-2.6.32.tar.gz
cd libxml2-2.6.32
./configure --prefix=/usr/local/libxml2
make
make install
如果安装成功以后,在/usr/local/libxml2/目录下将生成bin、include、lib、man和share五个目录。在后面安装PHP源代码包的配置时,会通过在configure命令的选项中加上"--with-libxml-dir=/usr/ local/libxml2"选项,用于指定安装libxml2库文件的位置。
3.安装php
#tar zvxf php-7.0.2.tar.gz
#cd php-7.0.2
#./configure \
--prefix=/usr/local/php \
--with-libxml-dir=/usr/local/libxml2
#make
#make install
4.启动php
./php-cgi -b 127.0.0.1:9000 & (9000端口与php与ngix中的配置相关)
三.php与nginx的关连配置
在nginx.conf中增加如下配置:
location ~ .*\.php?$ { #include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; }
|
四.上传模块的配置
在nginx.conf中增加如下配置:
server { listen 12380; server_name localhost; client_max_body_size 1024m; client_body_buffer_size 512k; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header REMOTE_ADD $remote_addr;
location / { root html; index index.html ; }
location ~ .*\.php?$ { #include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi.conf; }
location = /mydelete { rewrite ^(.*)$ /mydelete.php last; }
location /upload { # 转到后台处理URL,表示Nginx接收完上传的文件后,然后交给后端处理的地址 upload_pass /myupload.php; # 临时保存路径, 可以使用散列 # 上传模块接收到的文件临时存放的路径, # 1 表示方式,该方式是需要在/tmp/nginx_upload下创建以0到9为目录名称的目录, # 上传时候会进行一个散列处理。 upload_store /tmp/filetemp; # 上传文件的权限,rw表示读写 r只读 upload_store_access user:rw group:rw all:rw; #set $upload_field_name "file"; # upload_resumable on; # 这里写入http报头,pass到后台页面后能获取这里set的报头字段 upload_set_form_field "file_content_type" $upload_content_type; upload_set_form_field "file_path" $upload_tmp_path; upload_set_form_field "file_name" $upload_file_name; upload_set_form_field "relative_path" $upload_field_name;
# Upload模块自动生成的一些信息,如文件大小与文件md5值 upload_aggregate_form_field "${upload_field_name}_md5" $upload_file_md5; upload_aggregate_form_field "${upload_field_name}_size" $upload_file_size; # 允许的字段,允许全部可以 "^.*$" upload_pass_form_field "^.*$"; # upload_pass_form_field "^submit$|^description$"; # 每秒字节速度控制,0表示不受控制,默认0, 128K upload_limit_rate 0; # 如果pass页面是以下状态码,就删除此次上传的临时文件 upload_cleanup 400 404 499 500-505; # 打开开关,意思就是把前端脚本请求的参数会传给后端的脚本语言, #比如:http://192.168.1.251:9000/upload/?k=23,后台可以通过POST['k']来访问。 upload_pass_args on; } } |
五.upload.php脚本
<?php //print_r($_POST); $tp = "file"; $time = date("m_d_").$tt; $name = $_POST[$tp."_name"]; $name =".". pathinfo($name, PATHINFO_EXTENSION); $name = uuid().$name; $type = $time.$_POST[$tp."_content_type"]; $temppath = $_POST[$tp."_path"]; $relative_path = $_POST["relative_path"]; $relative_path_name = $relative_path.$name; $final_file_path = "/fileserver/".$relative_path_name; $result = rename($temppath,$final_file_path); if($result == true) { print_r("200".$relative_path_name); }else { print_r("false"); }
function uuid() { if (function_exists ( 'com_create_guid' )) { return com_create_guid (); } else { mt_srand ( ( double ) microtime () * 10000 ); //optional for php 4.2.0 and up.随便数播种,4.2.0以后不需要了。 $charid = strtoupper ( md5 ( uniqid ( rand (), true ) ) ); //根据当前时间(微秒计)生成唯一id. $hyphen = chr ( 45 ); // "-" $uuid = '' . //chr(123)// "{" substr ( $charid, 0, 8 ) . $hyphen . substr ( $charid, 8, 4 ) . $hyphen . substr ( $charid, 12, 4 ) . $hyphen . substr ( $charid, 16, 4 ) . $hyphen . substr ( $charid, 20, 12 ); //.chr(125);// "}" return $uuid; } }
?> |
六.上传模块的nginx.conf整体配置
user root root;
#nginx进程数,建议按照cpu数目来指定,一般为它的倍数。
worker_processes 2;
#为每个进程分配cpu,上例中将8个进程分配到8个cpu,当然可以写多个,或者将一个进程分配到多个cpu。
worker_cpu_affinity 00000001 00000010;
error_log /usr/local/nginx/logs/error.log;
error_log /usr/local/nginx/logs/error.log notice;
error_log /usr/local/nginx/logs/error.log info;
#pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 65536;
#use epoll 使用epoll的I/O模型。
#worker_connections 每个进程允许的最多连接数
events {
use epoll;
worker_connections 65536;
}
http
{
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server
{
listen 12390;
location /
{
root /alidata/fileserver;
if ($request_method = DELETE)
{
return 666;
}
}
}
server
{
listen 12380;
server_name localhost;
client_max_body_size 1024m;
client_body_buffer_size 512k;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE_ADD $remote_addr;
location /
{
root html;
index index.html ;
}
location ~ .*\.php?$
{
#include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location = /mydelete
{
rewrite ^(.*)$ /mydelete.php last;
}
location /upload
{
# 转到后台处理URL,表示Nginx接收完上传的文件后,然后交给后端处理的地址
upload_pass /myupload.php;
# 临时保存路径, 可以使用散列
# 上传模块接收到的文件临时存放的路径, 1 表示方式,该方式是需要在/tmp/nginx_upload下创建以0到9为目录名称的目录,上传时候会进行一个散列处理。
upload_store /tmp/filetemp;
# 上传文件的权限,rw表示读写 r只读
upload_store_access user:rw group:rw all:rw;
#set $upload_field_name "file";
# upload_resumable on;
# 这里写入http报头,pass到后台页面后能获取这里set的报头字段
upload_set_form_field "file_content_type" $upload_content_type;
upload_set_form_field "file_path" $upload_tmp_path;
upload_set_form_field "file_name" $upload_file_name;
upload_set_form_field "relative_path" $upload_field_name;
# Upload模块自动生成的一些信息,如文件大小与文件md5值
upload_aggregate_form_field "${upload_field_name}_md5" $upload_file_md5;
upload_aggregate_form_field "${upload_field_name}_size" $upload_file_size;
# 允许的字段,允许全部可以 "^.*$"
upload_pass_form_field "^.*$";
# upload_pass_form_field "^submit$|^description$";
# 每秒字节速度控制,0表示不受控制,默认0, 128K
upload_limit_rate 0;
# 如果pass页面是以下状态码,就删除此次上传的临时文件
upload_cleanup 400 404 499 500-505;
# 打开开关,意思就是把前端脚本请求的参数会传给后端的脚本语言,比如:http://192.168.1.251:9000/upload/?k=23,后台可以通过POST['k']来访问。
upload_pass_args on;
}
}
}