设为首页 收藏本站
查看: 1175|回复: 0

[经验分享] 170116、centos6.4下nginx和ftp搭建图片服务器

[复制链接]

尚未签到

发表于 2017-12-23 18:43:27 | 显示全部楼层 |阅读模式
一、需要的组件
  图片服务器两个服务:

Nginx(图片访问):
  1、http服务:可以使用nginx做静态资源服务器。也可以使用apache。推荐使用nginx,效率更高。
  2、反向代理 实现 负载均衡

ftp服务(图片上传):
  使用Linux做服务器,在linux中有个ftp组件vsftpd。

二、Nginx服务器搭建

1.安装Nginx
  要求安装vmware虚拟机。
  Linux:CentOS6.4(32)
  Nginx:1.8.0
  Vsftpd:需要在线安装。
  虚拟机以及Linux安装很简单此处略。
  Linux的局域网IP为:192.168.1.110
  修改Linux的IP并立即生效的命令:

[java] view plain copy
print?https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

  • #切换root管理员用户
  • [iyunv@localhost ~]# su
  • password
  • #设置本机IP并立即生效
  • [iyunv@localhost ~]# ifconfig eth0 192.168.1.110 netmask 255.255.255.0

1.1、nginx安装环境
  nginx是C语言开发,建议在linux上运行,本教程使用Centos6.5作为安装环境。
  n  gcc
  安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:yum install gcc-c++
  n  PCRE
  PCRE(PerlCompatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。

[java] view plain copy
print?https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

  • [iyunv@localhost ~]#yum install -y pcre pcre-devel
  注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。
  n  zlib
  zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。

[java] view plain copy
print?https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

  • [iyunv@localhost ~]#yum install -y zlib zlib-devel
  n  openssl
  OpenSSL是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
  nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。

[java] view plain copy
print?https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

  • [iyunv@localhost ~]#yum install -y openssl openssl-devel

1.2、把nginx安装包nginx-1.8.0.tar.gz上传到服务器。
DSC0000.jpg

  在secureCRT打开sftp会话框,上传文件
  使用put/get命令 或者直接拖拽文件

1.3、解压缩(在安装包所在目录执行)

[java] view plain copy
print?https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

  • [iyunv@localhost ~]# tar -zxvf nginx-1.8.0.tar.gz
  执行下面的命令创建makefile

[java] view plain copy
print?https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

  • ./configure \

  • --prefix=/usr/local/nginx \

  • --pid-path=/var/run/nginx/nginx.pid \

  • --lock-path=/var/lock/nginx.lock \

  • --error-log-path=/var/log/nginx/error.log \

  • --http-log-path=/var/log/nginx/access.log \

  • --with-http_gzip_static_module \

  • --http-client-body-temp-path=/var/temp/nginx/client \

  • --http-proxy-temp-path=/var/temp/nginx/proxy \

  • --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \

  • --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \

  • --http-scgi-temp-path=/var/temp/nginx/scgi
  注意:上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录

[java] view plain copy
print?https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

  • [iyunv@bogon nginx-1.8.0]# mkdir /var/temp/nginx -p

DSC0001.jpg

1.5、编译安装
  编译:

[java] view plain copy
print?https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

  • [iyunv@localhost nginx-1.8.0]# make
  安装:

[java] view plain copy
print?https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

  • [iyunv@localhost nginx-1.8.0]# make  install
  安装成功以后进入安装目录(创建makedir时指定的”--prefix=/usr/local/nginx \“)

[java] view plain copy
print?https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

  • [iyunv@localhost nginx-1.8.0]# cd /usr/local/nginx/
DSC0002.jpg


2、nginx运行

2.1、启动nginx

[java] view plain copy
  print?https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

  • [iyunv@localhost nginx]# cd sbin
  • [iyunv@localhost sbin]# ./nginx
  
DSC0003.jpg

2.2、关闭

[java] view plain copy
  print?https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

  • [iyunv@localhost sbin]# ./nginx -s stop



2.3、重新加载配置文件

[java] view plain copy
  print?https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

  • [iyunv@localhost sbin]# ./nginx -s reload
  

  2.4、关闭防火墙
  1)关闭

[java] view plain copy
  print?https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

  • [iyunv@localhost sbin]# service iptables stop

  • iptables: Flushing firewall rules:                         [  OK  ]

  • iptables: Setting chains to policy ACCEPT: filter          [  OK  ]

  • iptables: Unloading modules:                               [  OK  ]
  
2)也可以修改防火墙配置文件:

[java] view plain copy
  print?https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

  • [iyunv@localhost sbin]# vim /etc/sysconfig/iptables

[java] view plain copy
  print?https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

  • //在倒数第二行加入80端口
  • -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
  
修改后需要重启防火墙:

[java] view plain copy
  print?https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

  • [iyunv@localhost sbin]# service iptables restart
  
3)另外一种解决办法

[java] view plain copy
  print?https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

  • [iyunv@localhost ]# /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
  • [iyunv@localhost ]# /etc/init.d/iptables save
  • [iyunv@localhost ]# /etc/init.d/iptables restart

2.5、访问nginx服务
DSC0004.jpg


3、关于图片服务器配置
  进入配置文件目录

[java] view plain copy
  print?https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

  • cd /usr/local/nginx/conf/
  
nginx的默认配置文件nginx.config

[java] view plain copy
  print?https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

  • #user  nobody;
  • worker_processes  1;

  • #error_log  logs/error.log;
  • #error_log  logs/error.log  notice;
  • #error_log  logs/error.log  info;

  • #pid        logs/nginx.pid;


  • 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;

  •     server {
  •         listen       80;
  •         server_name  localhost;

  •         #charset koi8-r;

  •         #access_log  logs/host.access.log  main;

  •         location / {
  •             root   html;
  •             index  index.html index.htm;
  •         }

  •         #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;
  •         }

  •         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  •         #
  •         #location ~ \.php$ {
  •         #    proxy_pass   http://127.0.0.1;
  •         #}

  •         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  •         #
  •         #location ~ \.php$ {
  •         #    root           html;
  •         #    fastcgi_pass   127.0.0.1:9000;
  •         #    fastcgi_index  index.php;
  •         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  •         #    include        fastcgi_params;
  •         #}

  •         # deny access to .htaccess files, if Apache's document root
  •         # concurs with nginx's one
  •         #
  •         #location ~ /\.ht {
  •         #    deny  all;
  •         #}
  •     }


  •     # another virtual host using mix of IP-, name-, and port-based configuration
  •     #
  •     #server {
  •     #    listen       8000;
  •     #    listen       somename:8080;
  •     #    server_name  somename  alias  another.alias;

  •     #    location / {
  •     #        root   html;
  •     #        index  index.html index.htm;
  •     #    }
  •     #}


  •     # HTTPS server
  •     #
  •     #server {
  •     #    listen       443 ssl;
  •     #    server_name  localhost;

  •     #    ssl_certificate      cert.pem;
  •     #    ssl_certificate_key  cert.key;

  •     #    ssl_session_cache    shared:SSL:1m;
  •     #    ssl_session_timeout  5m;

  •     #    ssl_ciphers  HIGH:!aNULL:!MD5;
  •     #    ssl_prefer_server_ciphers  on;

  •     #    location / {
  •     #        root   html;
  •     #        index  index.html index.htm;
  •     #    }
  •     #}

  • }
  配置图片服务器
  方法一、在配置文件server{}中location /{} 修改配置:

[java] view plain copy
  print?https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

  • #默认请求
  • location / {
  •    root  /home/ftpuser/www;#定义服务器的默认网站根目录位置
  •    index index.html index.php index.htm;#定义首页索引文件的名称
  • }
  其中:/home/ftpuser/www;为创建FTP服务账户ftpuser的根目录下的www目录
  方法二、在http{}内配置新服务

[java] view plain copy
  print?https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

  • server {
  •         listen       8080;
  •         server_name  localhost;

  •         #charset utf-8;

  •         #access_log  logs/host.access.log  main;

  •         #默认请求
  •         location / {
  •             root  /home/ftpuser/www;#定义服务器的默认网站根目录位置
  •             index index.html index.php index.htm;#定义首页索引文件的名称
  •            }
  •         }

因为需要开始端口号8080,所以要在防火墙中开启8080端口

[java] view plain copy
  print?https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

  • [iyunv@localhost ]# /sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
  • [iyunv@localhost ]# /etc/init.d/iptables save
  • [iyunv@localhost ]# /etc/init.d/iptables restart
  


三、FTP服务的安装与启动

1、安装vsftpd组件
  vsftpd组件为Linux的FTP服务组件,安装方式为在线安装。
  

[iyunv@localhost ~]# yum -y install vsftpd  

  安装完后,有/etc/vsftpd/vsftpd.conf 文件,是vsftp的配置文件。

2、添加一个ftp用户
  此用户就是用来登录ftp服务器用的。

[java] view plain copy
print?https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

  • [iyunv@localhost ~]# useradd ftpuser
  这样一个用户建完,可以用这个登录,记得用普通登录不要用匿名了。登录后默认的路径为 /home/ftpuser.
  为这个ftp账户添加密码

[java] view plain copy
print?https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

  • [iyunv@localhost ~]# passwd ftpuser
  输入两次密码后修改密码。

3、 防火墙开启21端口
  因为ftp默认的端口为21,而centos默认是没有开启的,所以要修改iptables文件
  

[iyunv@localhost ~]# vim /etc/sysconfig/iptables  

  在行上面有22 -j ACCEPT 下面另起一行输入跟那行差不多的,只是把22换成21,然后:wq保存。
  还要运行下,重启iptables

[java] view plain copy
print?https://code.csdn.net/assets/CODE_ico.pnghttps://code.csdn.net/assets/ico_fork.svg

  • [iyunv@localhost ~]# service iptables restart

4、 修改selinux
  外网是可以访问上去了,可是发现没法返回目录(使用ftp的主动模式,被动模式还是无法访问),也上传不了,因为selinux作怪了。
  修改selinux:
  执行以下命令查看状态:
  

[iyunv@localhost ~]# getsebool -a | grep ftp  

  allow_ftpd_anon_write --> off
  allow_ftpd_full_access --> off
  allow_ftpd_use_cifs --> off
  allow_ftpd_use_nfs --> off
  ftp_home_dir --> off
  ftpd_connect_db --> off
  ftpd_use_passive_mode --> off
  httpd_enable_ftp_server --> off
  tftp_anon_write --> off
  执行上面命令,再返回的结果看到两行都是off,代表,没有开启外网的访问
  

[iyunv@localhost ~]# setsebool -P allow_ftpd_full_access on  

  
[iyunv@localhost ~]# setsebool -P ftp_home_dir on
  

  这样应该没问题了(如果,还是不行,看看是不是用了ftp客户端工具用了passive模式访问了,如提示Entering Passive mode,就代表是passive模式,默认是不行的,因为ftp passive模式被iptables挡住了,下面会讲怎么开启,如果懒得开的话,就看看你客户端ftp是否有port模式的选项,或者把passive模式的选项去掉。如果客户端还是不行,看看客户端上的主机的电脑是否开了防火墙,关吧)
  FileZilla的主动、被动模式修改:
  菜单:编辑→设置
DSC0005.jpg


5、关闭匿名访问
  修改/etc/vsftpd/vsftpd.conf文件:
DSC0006.jpg

  重启ftp服务:
  

[iyunv@localhost ~]# service vsftpd restart  

6、 开启被动模式
  默认是开启的,但是要指定一个端口范围,打开vsftpd.conf文件,在后面加上
  

pasv_min_port=30000  
pasv_max_port=30999
  

  表示端口范围为30000~30999,这个可以随意改。改完重启一下vsftpd
  由于指定这段端口范围,iptables也要相应的开启这个范围,所以像上面那样打开iptables文件。
  也是在21上下面另起一行,更那行差不多,只是把21 改为30000:30999,然后:wq保存,重启下iptables。这样就搞定了。

7、设置开机启动vsftpd ftp服务
  

[iyunv@localhost ~]# chkconfig vsftpd on  

四、部署验证
  在www下新建文件夹images,下面放一张图片001.jpg
  测试访问:http://192.168.2.37/images/2017/01/16/1484562270619097.jpg
DSC0007.png

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-427274-1-1.html 上篇帖子: nginx的fastcgi 下篇帖子: Nginx实现集群的负载均衡配置过程详解
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表