分布式云盘环境搭建
发布日期:2021-09-28 19:25:39 浏览次数:6 分类:技术文章

本文共 21528 字,大约阅读时间需要 71 分钟。

一.fastDFS的安装与使用

1.fastDFS的安装部署与使用

FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储、文件同步、文件访问(文件上传、文件下载)等,解决了大容量存储和负载均衡的问题。特别适合以文件为载体的在线服务,如相册网站、视频网站等等。老师源码

1.2.1 libevent

老版本的fastDFS是基于libevent写的,但是最新版的fastDFS最终网络IO这部分重新用

epoll实现了。
所以如果是fastDFS是2.0之前,请先安装好libevent环境。 之后就不用了。

1.2.2 libfastcommon

git下载地址:

./make.shsudo ./make.sh install

确认make没有错误后,执行安装,64位系统默认会复制到/usr/lib64下。

这时候需要设置环境变量或者创建软链接

export LD_LIBRARY_PATH=/usr/lib64/ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so

debian独有的第三种添加连接路径的方法:

sudo vim /etc/ld.so.confsudo ldconfig -v

测试一个执行文件是否缺少动态库

ldd [可执行文件路径]

1.2.3 安装fastDFS

下载 FastDFS 源程序,下载地址

也可以用github下载
这里我们下的是V5.05版本。

tar -zxvf fastdfs-5.05.tar.gz./make.shsudo ./make.sh install

确认make没有错误后,执行安装,默认会安装到/usr/bin中,并在/etc/fdfs中添加三个配置文件。

遇到权限问题需要加sudo,或者去root目录下面操作

1.2.4 修改配置文件

首先将三个文件的名字去掉sample,暂时只修改以下几点,先让fastdfs跑起来,其余参数调优的时候再考虑。也可以考虑将三个文件备份一下。

tracker.conf 中修改

base_path=/usr/fastdfs #用于存放日志。bind_addr=192.168.21.139 #不要绑定127.0.0.1

注意 确保/usr/fastdfs 目录存在

storage.conf 中修改

base_path=/usr/fastdfs/storage #用于存放日志。store_path0=/usr/fastdfs/fastdfs0 #存放数据,若不设置默认为前面那个tracker_server=192.168.2.105:22122 #指定tracker服务器地址。不能是127.0.0.1

注意 确保/usr/fastdfs/storage、 /usr/fastdfs/fastdfs0目录存在

client.conf 中同样要修改

base_path=/usr/fastdfs #用于存放日志。tracker_server=192.168.2.105:22122 #指定tracker服务器地址。

1.2.5 启动tracker和storage—–3个中tracker必须最先启动

tracker必须最先启动且绑定IP不能选择127.0.0.1,或者默认,因为默认就是127.0.0.1,client和storage的track_server必须连接到tracker的IP,track的http端口也就是http.server_port=8080,storage上启动的http服务的端口号 http.server_port=8080,client中http.tracker_server_port=8080,主要是为了支持http,还有一个小细节,就是最后一行的##include http.conf 删掉一个#,并且配置中的path可以根据自己的要求设置但也要尽可能便于管理,目录尽量避免放到root目录,涉及权限问题,目录最好在改配置之前就建好,以免遗忘出现错误

sudo /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf

同下

sudo /usr/bin/fdfs_storaged /etc/fdfs/storage.conf

查看端口是否起来

[root@itcast:/etc/fdfs]# netstat -antp | grep storagetcp        0      0 0.0.0.0:23000           0.0.0.0:*               LISTEN      10333/fdfs_storagedtcp        0      0 10.1.20.245:57886       10.1.20.245:22122       ESTABLISHED 10333/fdfs_storaged

也可以以下命令来监控服务器的状态: 可以使用 fdfs_monitor 来查看一下storage的状态,看是否已经成功注册到了tracker

[root@itcast:/etc/fdfs]# fdfs_monitor /etc/fdfs/storage.conf[2016-09-02 17:33:01] DEBUG - base_path=/usr/fastdfs/storage, connect_timeout=30, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0server_count=1, server_index=0tracker server is 192.168.137.128:22122group count: 1Group 1:group name = group1disk total space = 48756 MBdisk free space = 40471 MBtrunk free space = 0 MBstorage server count = 1active server count = 1storage server port = 23000storage HTTP port = 8888store path count = 1subdir count per path = 256current write server index = 0current trunk file id = 0    Storage 1:        id = 192.168.137.128        ip_addr = 192.168.137.128  ACTIVE...        last_heart_beat_time = 2016-09-02 17:32:35        last_source_update = 1970-01-01 08:00:00        last_sync_update = 1970-01-01 08:00:00        last_synced_timestamp = 1970-01-01 08:00:00

看到ACTIVE,就说明已经成功注册到了tracker。

查看端口情况

[root@ubuntu:~]#netstat -lnp --tcp

检查进程

[root@ubuntu:~]# ps -ef |grep fdfsroot 7819 1 0 15:24 ? 00:00:00 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.confroot 8046 1 0 15:36 ? 00:00:01 fdfs_storaged /etc/fdfs/storage.conf start[root@itcast:/]# ps aux | grep fdfsroot      3815  0.0  0.3 144552  3536 ?        Sl   17:03   0:04 /usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restartroot      3825  0.0  6.7 146676 67712 ?        Sl   17:03   0:04 /usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

表示启动ok了,若有错误,可以在/usr/fastdfs目录下检查日志。

1.2.6 上传、删除测试

  1. 上传文件:
    使用自带的fdfs_test来测试,使用格式如下:
fdfs_test /etc/fdfs/client.conf upload abcd.txt(在测试目录下)

出现example file url:

表示添加成功

  1. 删除文件
fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/wKgCaVeVnY2AcveAAAAAFDGtH6s805_big.txt

可以看到,上传ok了,这里会生成两个文件,这是fastdfs的主/从文件特性,以后再介绍。

example file url是不能在浏览器中直接打开的,除非配合nginx使用,这里我不需要了。
删除文件需要完整的group_name和remote_filename。

一些其他的指令均在fastdfs/client目录中。包括指令:

fdfs_appender_testfdfs_append_filefdfs_delete_filefdfs_file_infofdfs_storagedfdfs_test1fdfs_upload_appenderfdfs_appender_test1fdfs_crc32fdfs_download_filefdfs_monitorfdfs_testfdfs_trackerdfdfs_upload_file

1.2.7 重新启动程序

server进程退出运行

直接kill即可让server进程正常退出,可以使用killall命令,例如:

killall fdfs_trackerdkillall fdfs_storaged

也可以使用如下命令:

/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf stop/usr/bin/fdfs_storaged /etc/fdfs/storage.conf stop

千万不要使用-9参数强杀,否则可能会导致binlog数据丢失的问题。

重启server进程

直接使用:

/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart/usr/bin/fdfs_storaged /etc/fdfs/storage.conf restart

删除storage

/usr/local/bin/fdfs_monitor /etc/fdfs/storage.conf delete group2 20.12.1.73(删除group2中ip为20.12.1.73的storage)

注意:在配置过程,要注意防火墙的设置。关闭防火墙,或者设置对相关端口例外。

1、关闭ubuntu的防火墙

ufw disable

2、卸载了iptables

apt-get remove iptables

3、关闭ubuntu中的防火墙的其余命令

iptables -P INPUT ACCEPT

iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT
iptables -F

二.memcache的安装与配置

早期内存数据库memcache安装与使用

libevent-2.0.22-stable.gz

memcached-1.4.29.tar.gz
libmemcached-1.0.18.tar.gz

1.先解压libevent-2.0.22-stable.gz,因为memcached的网络部分依赖于libevent

tar zxvf libevent-2.0.22-stable.gz

进入解压目录

./configure   -------->检查配置是否满足------>成功会生成makefilemake   -------->没有err出现sudo make install   ----------->安装,也就时将文件转移目录

2.再解压memcache

tar zxvf memcached-1.4.29.tar.gz

进入解压目录

./configure   -------->检查配置是否满足------>成功会生成makefilemake   -------->没有err出现sudo make install   ----------->安装,也就时将文件转移目录

测试是否成功,成功就能启动

itcast@itcast:~$ memcached -p 11211 -vvv -m 64mslab class   1: chunk size        96 perslab   10922slab class   2: chunk size       120 perslab    8738slab class   3: chunk size       152 perslab    6898slab class   4: chunk size       192 perslab    5461slab class   5: chunk size       240 perslab    4369slab class   6: chunk size       304 perslab    3449slab class   7: chunk size       384 perslab    2730slab class   8: chunk size       480 perslab    2184slab class   9: chunk size       600 perslab    1747slab class  10: chunk size       752 perslab    1394slab class  11: chunk size       944 perslab    1110slab class  12: chunk size      1184 perslab     885slab class  13: chunk size      1480 perslab     708slab class  14: chunk size      1856 perslab     564slab class  15: chunk size      2320 perslab     451slab class  16: chunk size      2904 perslab     361slab class  17: chunk size      3632 perslab     288slab class  18: chunk size      4544 perslab     230slab class  19: chunk size      5680 perslab     184slab class  20: chunk size      7104 perslab     147slab class  21: chunk size      8880 perslab     118slab class  22: chunk size     11104 perslab      94slab class  23: chunk size     13880 perslab      75slab class  24: chunk size     17352 perslab      60slab class  25: chunk size     21696 perslab      48slab class  26: chunk size     27120 perslab      38slab class  27: chunk size     33904 perslab      30slab class  28: chunk size     42384 perslab      24slab class  29: chunk size     52984 perslab      19slab class  30: chunk size     66232 perslab      15slab class  31: chunk size     82792 perslab      12slab class  32: chunk size    103496 perslab      10slab class  33: chunk size    129376 perslab       8slab class  34: chunk size    161720 perslab       6slab class  35: chunk size    202152 perslab       5slab class  36: chunk size    252696 perslab       4slab class  37: chunk size    315872 perslab       3slab class  38: chunk size    394840 perslab       2slab class  39: chunk size    493552 perslab       2slab class  40: chunk size    616944 perslab       1slab class  41: chunk size    771184 perslab       1slab class  42: chunk size   1048576 perslab       1<26 server listening (auto-negotiate)<27 server listening (auto-negotiate)<28 send buffer was 212992, now 268435456<32 send buffer was 212992, now 268435456<30 server listening (udp)<34 server listening (udp)<29 server listening (udp)<33 server listening (udp)<31 server listening (udp)<35 server listening (udp)<28 server listening (udp)<32 server listening (udp)<36 new auto-negotiating client connection36: going from conn_new_cmd to conn_waiting36: going from conn_waiting to conn_read36: going from conn_read to conn_parse_cmd

另起shell测试是否能连接

itcast@itcast:~$ telnet localhost 11211Trying 127.0.0.1...Connected to localhost.Escape character is '^]'.set foo 0 0 321STORED   CLIENT_ERROR bad data chunkERRORset foo 0 0 3safSTOREDget fooVALUE foo 0 3safEND

服务器会出现

>36 CLIENT_ERROR bad data chunk<36 set foo 0 0 336: going from conn_parse_cmd to conn_nread> NOT FOUND foo>36 STORED36: going from conn_nread to conn_write36: going from conn_write to conn_new_cmd36: going from conn_new_cmd to conn_waiting36: going from conn_waiting to conn_read36: going from conn_read to conn_parse_cmd<36 get foo> FOUND KEY foo>36 sending key foo>36 END36: going from conn_parse_cmd to conn_mwrite36: going from conn_mwrite to conn_new_cmd36: going from conn_new_cmd to conn_waiting36: going from conn_waiting to conn_read成功

三.redis的安装与配置

3.1 redis的安装部署与使用

Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。从2010年3月15日起,Redis的开发工作由VMware主持。

redis的服务端与客户端的启动

redis-serverredis-cli

默认持久数据库在/home/itcast下面dump.rdb

itcast@itcast:~/protwo/day2/git$ spawn-fcgi -a 127.0.0.1 -p 8020 -f./dataspawn-fcgi: child exited with: 0itcast@itcast:~/protwo/day2/git$ vi dataitcast@itcast:~/protwo/day2/git$ vi data.citcast@itcast:~/protwo/day2/git$ makegcc -c data.c -o data.o -I./include -I/usr/local/include/hiredis/ -Wall gcc data.o cJSON.o make_log.o redis_op.o -o data -lhiredis -lpthread -lfcgi -lmitcast@itcast:~/protwo/day2/git$ spawn-fcgi -a 127.0.0.1 -p 8020 -f./dataspawn-fcgi: child exited with: 1

这边是正常退出的意思,返回值为你返回的数

四.Nginx的安装部署与使用

4.1 Nginx的安装部署与使用

Nginx (“engine x”) 是一个高性能的HTTP和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。

Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大型的入口网站及搜索引擎Rambler(俄文:Рамблер)使用。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、新浪、网易、腾讯等。

4.1.1gcc g++的安装

ububtu平台编译环境可以使用以下指令

apt-get install build-essentialapt-get install libtool

centos平台编译环境使用如下指令

安装make

yum -y install gcc automake autoconf libtool make

安装g++

yum install gcc gcc-c++

4.1.2 nginx的安装

nginx可以使用各平台的默认包来安装,本文是介绍使用源码编译安装,包括具体的编译参数信息。

2.1正式开始前,编译环境gcc g++ 开发库之类的需要提前装好,这里默认你已经装好。
2.2 正式安装一般我们都需要先装pcre, zlib,前者为了重写rewrite,后者为了gzip压缩。
2.2.1 选定源码目录可以是任何目录,本文选定的是/usr/local/src
2.2.2 安装PCRE库 下载最新的PCRE源码包。
使用下面命令下载编译和安装PCRE包:
cd /usr/local/src
wget
tar -zxvf pcre-8.34.tar.gz
cd pcre-8.34
./configure
make
make install

2.2.3 安装zlib库 下载最新的 zlib 源码包,使用下面命令下载编译和安装 zlib包:

cd /usr/local/src

wget

tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make
make install

2.2.4 安装sslcd /usr/local/src

wget
tar -zxvf openssl-1.0.1c.tar.gz
./config
make
sudo make install

2.2.5 安装nginxcd /usr/local/src

wget
tar -zxvf nginx-1.10.1.tar.gz
cd nginx-1.10.1

./configure –sbin-path=/usr/local/nginx/nginx

–conf-path=/usr/local/nginx/nginx.conf
–pid-path=/usr/local/nginx/nginx.pid
–with-http_ssl_module
–with-pcre=/usr/local/src/pcre-8.39
–with-zlib=/usr/local/src/zlib-1.2.8
–with-openssl=/usr/local/src/openssl-1.0.1t

make

make install

–with-pcre=/usr/src/pcre-8.34 指的是pcre-8.34 的源码路径。 –with-zlib=/usr/src/zlib-1.2.7 指的是zlib-1.2.7 的源码路径。

安装成功后 /usr/local/nginx 目录下如下:
fastcgi.conf
koi-win
nginx.conf.default
fastcgi.conf.default
logs
scgi_params
fastcgi_params
mime.types
scgi_params.default
fastcgi_params.default mime.types.default
uwsgi_params
html
nginx
uwsgi_params.default
koi-utf
nginx.conf
win-utf

2.2.6 启动确保系统的 80 端口没被其他程序占用,运行/usr/local/nginx/nginx 命令来启动 Nginx。

netstat -ano|grep 80

如果查不到结果后执行,有结果则忽略此步骤(ubuntu下必须用sudo启动,不然只能在前台运行)

sudo /usr/local/nginx/nginx

打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。

Nginx会被安装在/usr/local/nginx目录下(也可以使用参数–prefix=指定自己需要的位置), 安装成功后 /usr/local/nginx 目录下有四个子目录分别是:conf、html、logs、sbin 。 其中 Nginx 的配置文件存放于 conf/nginx.conf, bin文件是位于 sbin 目录下的 nginx 文件。

确保系统的 80 端口没被其他程序占用,运行 /usr/local/nginx/nginx 命令来启动 Nginx, 打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功
2.2.7 通用配置

运行用户

user nobody;

启动进程,通常设置成和cpu的数量相等

worker_processes 1;

全局错误日志及PID文件#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;

pid logs/nginx.pid;

工作模式及连接数上限

events {

#epoll是多路复用IO(I/O Multiplexing)中的一种方式,
#仅用于linux2.6以上内核,可以大大提高nginx的性能
use epoll;

#单个后台worker process进程的最大并发链接数   worker_connections  1024;# 并发总数是 worker_processes 和 worker_connections 的乘积# 即 max_clients = worker_processes * worker_connections# 在设置了反向代理的情况下,max_clients = worker_processes * worker_connections / 4  为什么# 为什么上面反向代理要除以4,应该说是一个经验值# 根据以上条件,正常情况下的Nginx Server可以应付的最大连接数为:4 * 8000 = 32000# worker_connections 值的设置跟物理内存大小有关# 因为并发受IO约束,max_clients的值须小于系统可以打开的最大文件数# 而系统可以打开的最大文件数和内存大小成正比,一般1GB内存的机器上可以打开的文件数大约是10万左右# 我们来看看360M内存的VPS可以打开的文件句柄数是多少:# $ cat /proc/sys/fs/file-max# 输出 34336# 32000 < 34336,即并发连接总数小于系统可以打开的文件句柄总数,这样就在操作系统可以承受的范围之内# 所以,worker_connections 的值需根据 worker_processes 进程数目和系统可以打开的最大文件总数进行适当地进行设置# 使得并发总数小于操作系统可以打开的最大文件数目# 其实质也就是根据主机的物理CPU和内存进行配置# 当然,理论上的并发总数可能会和实际有所偏差,因为主机还有其他的工作进程需要消耗系统资源。# ulimit -SHn 65535

}

http {

#设定mime类型,类型由mime.type文件定义
include mime.types;
default_type application/octet-stream;
#设定日志格式
log_format main ‘ remoteaddr remote_user [ timelocal]" request” ’
status body_bytes_sent “ httpreferer http_user_agent" "$http_x_forwarded_for”’;

access_log  logs/access.log  main;#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,#对于普通应用,必须设为 on,#如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,#以平衡磁盘与网络I/O处理速度,降低系统的uptime.sendfile     on;#tcp_nopush     on;#连接超时时间#keepalive_timeout  0;keepalive_timeout  65;tcp_nodelay     on;#开启gzip压缩gzip  on;gzip_disable "MSIE [1-6].";#设定请求缓冲client_header_buffer_size    128k;large_client_header_buffers  4 128k;#设定虚拟主机配置server {    #侦听80端口    listen    80;    #也可以设置为 合法域名    server_name  localhost;    #定义服务器的默认网站根目录位置    root html;    #设定本虚拟主机的访问日志    access_log  logs/nginx.access.log  main;    #默认请求    location / {        #定义如果访问根目录的请求目录        root html;        #定义首页索引文件的名称        index index.php index.html index.htm;       }    # 定义错误提示页面    error_page   500 502 503 504 /50x.html;    location = /50x.html {    }    #静态文件,nginx自己处理    location ~ ^/(images|javascript|js|css|flash|media|static)/ {        #过期30天,静态文件不怎么更新,过期可以设大一点,        #如果频繁更新,则可以设置得小一点。        expires 30d;    }    #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置.    location ~ .php$ {        fastcgi_pass 127.0.0.1:9000;        fastcgi_index index.php;        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;        include fastcgi_params;    }    #禁止访问 .htxxx 文件        location ~ /.ht {        deny all;    }}

}

2.3 启动与关闭2.3.1 重启Nginxsudo /usr/local/sbin/nginx -s reload

2.3.2 关闭Nginx 快速停止服务

sudo /usr/local/sbin/nginx -s stop


优雅停止服务

sudo /usr/local/sbin/nginx -s quit #kill -s SIGQUIT pid_master
kill -s SIGWINCH pid_master

这里如果是虚拟机要记得设为桥接,共享物理地址

Nginx反向代理设置

nginx.conf配置
events {
worker_connections 1024;
}

http {

include mime.types;
default_type application/octet-stream;

#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '#                  '$status $body_bytes_sent "$http_referer" '#                  '"$http_user_agent" "$http_x_forwarded_for"';#access_log  logs/access.log  main;sendfile        on;#tcp_nopush     on;#keepalive_timeout  0;keepalive_timeout  65;#gzip  on;

upstream itcast.com {//这边的itcast可以自己随意写,但不要和已知的网站冲突

server 192.168.21.137:80;//真正的服务器ip和端口
server 192.168.21.97:80; //真正的服务器ip和端口
}

server {    listen       80;    server_name  localhost;    #charset koi8-r;    #access_log  logs/host.access.log  main;    location / {       proxy_pass http://itcast.com; //添加这句主要就是代理作用了,将客户端请求分发给itcast.com里的服务器处理,本机只分发        root   html;

index index1.html index1.htm;

index baidu.html;

index  demo.html;    }    location /demo.html {        root html;        index demo.html;    }

五.Nginx上FastCGI的安装部署与使用

5.Nginx上FastCGI的安装部署与使用

CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序一般运行在网络服务器上。 CGI可以用任何一种语言编写,只要这种语言具有标准输入、输出和环境变量。如php,perl,tcl等。

FastCGI像是一个常驻(long-live)型的CGI,它可以一直执行着,只要激活后,不会每次都要花费时间去fork一次(这是CGI最为人诟病的fork-and-execute 模式)。它还支持分布式的运算, 即 FastCGI 程序可以在网站服务器以外的主机上执行并且接受来自其它网站服务器来的请求。

FastCGI是语言无关的、可伸缩架构的CGI开放扩展,其主要行为是将CGI解释器进程保持在内存中并因此获得较高的性能。众所周知,CGI解释器的反复加载是CGI性能低下的主要原因,如果CGI解释器保持在内存中并接受FastCGI进程管理器调度,则可以提供良好的性能、伸缩性、Fail- Over特性等等。

Nginx安装fastDFS模块,主要是安装在fastDFS,storage存储服务器上,而不是tracker和client上。

目的实际是为了,当输入类似
URL地址,其中192.168.2.108 是一个storage服务器。
+
能够通过nginx的web服务功能,直接返回图片。

6.3 Nginx与FastCGInginx 不能像apache那样直接执行外部可执行程序,但nginx可以作为代理服务器,将请求转发给后端服务器,这也是nginx的主要作用之一。其中nginx就支持FastCGI代理,接收客户端的请求,然后将请求转发给后端fastcgi进程。下面介绍如何使用C/C++编写cgi/fastcgi,并部署到nginx中。

通过前面的介绍知道,fastcgi进程由FastCGI进程管理器管理,而不是nginx。这样就需要一个FastCGI管理,管理我们编写fastcgi程序。本文使用spawn-fcgi作为FastCGI进程管理器。
spawn-fcgispawn-fcgi是一个通用的FastCGI进程管理器,简单小巧,原先是属于lighttpd的一部分,后来由于使用比较广泛,所以就迁移出来作为独立项目了。spawn-fcgi使用pre-fork 模型,功能主要是打开监听端口,绑定地址,然后fork-and-exec创建我们编写的fastcgi应用程序进程,退出完成工作。fastcgi应用程序初始化,然后进入死循环侦听socket的连接请求。
安装
* 获取spawn-fcgi编译安装包,在 上可以获取当前最新的版本。

* 解压缩spawn-fcgi-x.x.x.tar.gz包。* 进入解压缩目录,执行./configure。* make & make install

如果遇到以下错误:

如果遇到以下错误:
./autogen.sh: x: autoreconf: not found

因为没有安装automake 工具,ubuntu用下面的命令安装好就可以了:

sudo apt-get install autoconf automake libtool 。

spawn-fcgi的帮助信息可以通过man spawn-fcgi或spawn-fcgi –h获得,下面是部分常用spawn-fcgi参数信息:

f 指定调用FastCGI的进程的执行程序位置
-a 绑定到地址addr。
-p 绑定到端口port。
-s 绑定到unix domain socket
-C 指定产生的FastCGI的进程数,默认为5。(仅用于PHP)
-P 指定产生的进程的PID文件路径。
-F 指定产生的FastCGI的进程数(C的CGI用这个)
-u和-g FastCGI使用什么身份(-u 用户 -g 用户组)运行,
CentOS下可以使用apache用户,其他的根据情况配置,如nobody、www-data等。

编写fastgci应用程序使用C/C++编写fastcgi应用程序,可以使用FastCGI软件开发套件或者其它开发框架,如fastcgi++。

本文使用FastCGI软件开发套件——fcgi
通过此套件可以轻松编写fastcgi应用程序,安装fcgi:
./configue
make

如果编译出现类似以下错误:

cgio.cpp: In destructor ‘virtual fcgi_streambuf::~fcgi_streambuf()’:
fcgio.cpp:50: error: ‘EOF’ was not declared in this scope

fcgio.cpp: In member function ‘virtual int fcgi_streambuf::overflow(int)’:

fcgio.cpp:70: error: ‘EOF’ was not declared in this scope

fcgio.cpp:75: error: ‘EOF’ was not declared in this scope

fcgio.cpp: In member function ‘virtual int fcgi_streambuf::sync()’:

fcgio.cpp:86: error: ‘EOF’ was not declared in this scope

fcgio.cpp:87: error: ‘EOF’ was not declared in this scope

fcgio.cpp: In member function ‘virtual int fcgi_streambuf::underflow()’:

fcgio.cpp:113: error: ‘EOF’ was not declared in this scope

make[2]: * [fcgio.lo] Error 1

make[2]: Leaving directory `/root/downloads/fcgi-2.4.1-SNAP-0910052249/libfcgi’

make[1]: * [all-recursive] Error 1

make[1]: Leaving directory `/root/downloads/fcgi-2.4.1-SNAP-0910052249’

make: * [all] Error 2

解决办法:在include/fcgio.h文件中加上**

include

,然后再次编译

sudo make install

编写一个fcgi简单的应用程序:

include

include “fcgi_stdio.h”

int main(int argc, char *argv[]){

int count = 0;

while (FCGI_Accept() >= 0) {    printf("Content-type: text/html\r\n");    printf("\r\n");    printf("Fast CGI Hello!");    printf("

Fast CGI Hello!

"); printf("Request number %d running on host %s\n", ++count, getenv("SERVER_NAME"));} return 0;

}

编译:

gcc fcgi_demo.c -o demo -lfcgi

这个就是其中一个针对client一个http请求的业务服务应用程序。 需要在后台跑起来,并且让spawn负责管理。

spawn-fcgi -a 127.0.0.1 -p 8081 -f ./demo
这边要注意看库有没有连接
sudo ln -s /usr/local/lib/libfcgi.so.0 /usr/lib/libfcgi.so.0

有关nginx的fcgi的配置

server {
listen 80;
server_name localhost;

#access_log  logs/host.access.log  main;    location / {        #proxy        #proxy_pass http://backup.com;        root   html;        index  index.html index.htm;    }    #监听用户的demo.cgi请求,通过fastcgi_pass 交给本地8081端口处理    #此时spwan-cgi已经将8081端口交给之前我们写好的demo进程处理    location = /demo.cgi {        fastcgi_pass 127.0.0.1:8081;        fastcgi_index index.cgi;//这边是交代一个入口,可以不写,不写的时候,上面不需要=号        include fastcgi.conf;    }    #error_page  404              /404.html;    # redirect server error pages to the static page /50x.html    #    error_page   500 502 503 504  /50x.html;    location = /50x.html {        root   html;    }

}

这样nginx收到请求时,会匹配到location = /demo.cgi块,将请求传到后端的fastcgi应用程序处理。

记住修改nginx.conf后要重启Nginx服务器

itcast@itcast:/usr/local/nginx/conf sudo/usr/local/nginx/sbin/nginxsreloadFastCGIitcast@itcast: /protwo/day2/git netstat -antp | grep 8012
itcast@itcast:~/protwo/day2/git sudokill914328itcast@itcast: /protwo/day2/git spawn-fcgi -a 127.0.0.1 -p 8012 -f./echo
fastcgi配置文件,里面有从网页端传递过来数据的一些配置
itcast@itcast:/usr/local/nginx/conf$ sudo vi nginx.conf

nginx.conf配置文件配置

server {
listen 80;
server_name localhost;

#charset koi8-r;    #access_log  logs/host.access.log  main;    location / {

proxy_pass ;

root   html;

index index1.html index1.htm;

index baidu.html;

index  demo.html;//默认处理的html页面,如果没有明确交代是静态页面,默认先按动态页面找,比如有动态的demo处理命令了,就不会执行demo.html,除非明确指出    }       location /demo.html { //这边是客户端输入ip:80后的请求页面或动态页面(如果是请求动态页面,则是一个请求处理命令)        root html; //根目录        index demo.html; //请求对应的处理页面    }    //静态页面最好写一下,不然如果有动态处理动作会优先处理动态的,会出现错误    location /demo {这边是客户端输入ip:80后的请求动态页面(如果是请求动态页面,则是一个请求处理命令)        fastcgi_pass 127.0.0.1:8011;//处理该请求的端口,可以不在本地,让远程端口处理

fastcgi_index demo.cgi; //这边都是处理入口,可不写,其实这边我还有疑问

include fastcgi.conf;//这边是应该包含的文件,默认是从当前nginx.conf同级目录找,找不到就不能重启nginx要注意不要写错    }       location /upload/UploadAction { //这边一开始是不可能知道处理动作,可以先去测试一下,用开发者工具跟踪请求,进行处理        fastcgi_pass 127.0.0.1:8012;

fastcgi_index demo.cgi;

include fastcgi.conf;    }

转载地址:https://blog.csdn.net/hackywit/article/details/52431231 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Linux下github的使用
下一篇:欢迎使用CSDN-markdown编辑器

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月21日 05时31分08秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章