linux环境搭建php+nginx+fastdfs

提要:由于项目中有文件上传的需求,传统php文件上传是把上传后的文件跟项目放到一起,时间一长项目会越来越笨重。于是采用将文件与项目分离的策略,具体实现使用fastdfs。fastdfs的开源项目地址:https://github.com/happyfish100(由淘宝架构师余庆开源)

安装步骤:

  1. 下载需要安装的软件
    依赖软件:
    pcre安装 yum install pcre
    zlib安装 yum install zlib
    真正用的软件:
    libfastcommon 选用版本V1.0.7
    wget https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz
    fastDFS 选用版本V5.0.5
    wget https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz
    fastdfs-nginx-module 选用版本V1.16
    wget http://jaist.dl.sourceforge.net/project/fastdfs/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gz
    说明:安装包放在/var/download/下,安装位置在/www/server/下。

  1. libfastcommon安装
    tar -zxvf V1.0.7.tar.gz
    cp -r libfastcommon-1.0.7/ /www/server/libfastcommon
    cd /www/server/libfastcommon
    ./make.sh && ./make.sh install

  1. fastDFS安装
    tar -zxvf V5.05.tar.gz
    cp -r fastdfs-5.05/ /www/server/fastdfs
    cd /www/server/fastdfs
    ./make.sh && ./make.sh install
    安装完成之后,配置文件在/etc/fdfs/目录下。
    cd /etc/fdfs
    cp client.conf.sample client.conf
    cp storage.conf.sample storage.conf
    cp tracker.conf.sample tracker.conf
    (1) 配置storage.conf文件 (在此之前先执行:mkdir -p /var/fdfs/storage 和 mkdir -p /var/fdfs/tracker。这两个是跟文件存放相关的目录)
     修改以下几项配置为:
     group_name=group1
     port=23000
     base_path=/var/fdfs/storage
     store_path0=/var/fdfs/storage
     tracker_server=192.168.5.22:22122 (tracker服务的默认端口为22122)
     http.server_port=8001 (该端口在配置nginx时也会用到)
    (2) 配置tracker.conf文件
     修改以下几项配置为:
     port=22122
     http.server_port=8001
     base_path=/var/fdfs/tracker
    (3) 启动fdfs_trackerd, fdfs_storeaged 服务

  1. 安装nginx插件fastdfs-nginx-module
    tar -zxvf fastdfs-nginx-module_v1.16.tar.gz
    cp -r fastdfs-nginx-module/ /www/server/fastdfs-nginx-module
    cd /www/server/fastdfs-nginx-module/src
    修改config配置文件中的CORE_INCS项 如下:
    (1) 由原来的:
    CORE_INCS="$CORE_INCS /usr/local/include/fastdfs /usr/local/include/fastcommon/"
     改成:
    CORE_INCS="$CORE_INCS /usr/include/fastdfs /usr/include/fastcommon/"
    说明:这个改动是很重要的,不然在nginx编译的时候会报错
    cp /www/server/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/
    cd /etc/fdfs/
    vim mod_fastdfs.conf
    (2) 添加或修改以下几项配置为:
     url_have_group_name=true
     tracker_server=192.168.5.22:22122
     [group1]
     group_name=group1
     storage_server_port=23000
     store_path_count=1
     store_path0=/var/fdfs/storage/data
    (3) 配置文件服务器的软连接
    ln -s /var/fdfs/storage/data/ /var/fdfs/storage/data/M00
    (4) 将下面两个文件复制到 /etc/fdfs/
    cp /www/server/fastdfs/conf/http.conf /etc/fdfs/
    cp /www/server/fastdfs/conf/mime.types /etc/fdfs/

  1. 重新编译nginx 添加fastdfs-nginx-module模块
    说明:由于项目运行环境是由宝塔搭建的,所以重新编译nginx需要到/www/server/nginx/src目录下进行。
    cd /www/server/nginx/src/
    ./configure --prefix=/www/server/nginx --with-http_stub_status_module --add-module=/www/server/fastdfs-nginx-module/src
    make && make install
    配置nginx
    cd /www/server/panel/vhost/nginx
    添加fastdfs.conf文件,添加如下配置项:
  listen 8001;  
  server_name 192.168.5.22;     
  location /group1/M00 {
        root /var/fdfs/storage;
         ngx_fastdfs_module;
     }

  1. 重启nginx
    cd /www/server/nginx/sbin
    ./nginx -s reload

  1. 配置/etc/fdfs/client.conf文件
    修改以下几项配置为:
    base_path=/var/fdfs/tracker
    tracker_server=192.168.5.22:22122
    http.tracker_server_port=8001

  1. 测试文件上传及访问
    (1)将一张图片放到/var/fdfs/storage/目录下
    (2)cd /var/fdfs/storage
    (3)/usr/bin/fdfs_upload_file /etc/fdfs/client.conf hzw-1.jpg
    返回结果:group1/M00/00/00/rBCMxl58NtSAaWRmAADqT-z7QHo692.jpg
    访问:http://192.168.5.22:8001/group1/M00/00/00/rBCMxl58NtSAaWRmAADqT-z7QHo692.jpg
    如果能看到图片,说明文件系统搭建成功!

  1. 如果遇到访问404的情况进行如下处理
    查看nginx状态 service nginx status
    返回如下结果: nginx (pid 29643 23193 23192 23191 23190 23189 23188 9694) already running 使用kill 命令 将上面nginx的pid 全部杀死 如 kill 29643
    再执行systemctl start nginx 命令重启nginx,查看nginx状态成功后,再次访问即可!!

    1. 将fastdfs安装为PHP的一个扩展
      (1)cd /www/server/fastdfs/php_client
      (2)执行 /www/server/php/72/bin/phpize
      (3)./configure --with-php-config=/www/server/php/72/bin/php-config
      (4)make && make install
      将fastdfs_client.ini文件的内容写入到对应版本的php.ini文件中:
      (5)cat fastdfs_client.ini >> /www/server/php/72/etc/php.ini
      (6)执行php -m|grep fastdfs_client 查看是否有fastdfs-client扩展
原文地址:https://www.cnblogs.com/lty-fly/p/12581715.html