mrbear 发表于 2017-12-22 19:38:53

nginx + vsftpd 搭建 图片服务器

  环境:
  CentOS7

安装 nginx
  一nginx安装环境
  1. gcc
  安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:
  yum install gcc-c++
  2.PCRE
  PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。
  yum install -y pcre pcre-devel
  注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。
  3. zlib
  zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
  yum install -y zlib zlib-devel
  4.openssl
  OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
  nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。
  yum install -y openssl openssl-devel
  二 编译安装
  1.将nginx-1.8.0.tar.gz拷贝至linux服务器。
  2.解压:
  tar -zxvf nginx-1.8.0.tar.gz
  cd nginx-1.8.0
  3.配置configure
  ./configure --help查询详细参数(参考本教程附录部分:nginx编译参数)
  参数设置如下:
  ./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目录
  三 启动运行测试
  1编译安装
  make
  makeinstall
  安装成功查看安装目录 :whereis nginx
  2 启动nginx
  cd /usr/local/nginx/sbin/
  ./nginx
  注意:执行./nginx启动nginx,这里可以-c指定加载的nginx配置文件,如下:
  ./nginx -c /usr/local/nginx/conf/nginx.conf
  如果不指定-c,nginx在启动时默认加载conf/nginx.conf文件,此文件的地址也可以在编译安装nginx时指定./configure的参数(--conf-path= 指向配置文件(nginx.conf))
  3 停止nginx
  方式1,快速停止:
  cd /usr/local/nginx/sbin
  ./nginx -s stop
  此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
  方式2,完整停止(建议使用):
  cd /usr/local/nginx/sbin
  ./nginx -s quit
  此方式停止步骤是待nginx进程处理任务完毕进行停止。
  4 重启nginx
  方式1,先停止再启动(建议使用):
  对nginx进行重启相当于先停止nginx再启动nginx,即先执行停止命令再执行启动命令。
  如下:
  ./nginx -s quit
  ./nginx
  方式2,重新加载配置文件:
  当nginx的配置文件nginx.conf修改后,要想让配置生效需要重启nginx,使用-s>  ./nginx -s>  5 测试
  nginx安装成功,启动nginx,即可访问虚拟机上的nginx:浏览器输入服务器地址即可

安装ftp组件
  1.安装vsftpd组件
  安装完后,有/etc/vsftpd/vsftpd.conf 文件,是vsftp的配置文件。
  yum -y install vsftpd
  2.添加一个ftp用户
  此用户就是用来登录ftp服务器用的。
  useradd ftpuser
  这样一个用户建完,可以用这个登录,记得用普通登录不要用匿名了。登录后默认的路径为 /home/ftpuser.   
  3.给ftp用户添加密码。
passwd ftpuser
  输入两次密码后修改密码。
  4 .防火墙开启21端口
firewall-cmd --permanent --zone=public --add-port=21/tcp
firewall-cmd --permanent --zone=public --add-port=21/udp
  还要运行下,重启iptables
  firewall-cmd --reload
  5.修改selinux
  外网是可以访问上去了,可是发现没法返回目录(使用ftp的主动模式,被动模式还是无法访问),也上传不了,因为selinux作怪了。
  修改selinux:
  执行以下命令查看状态:
  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,代表,没有开启外网的访问
  setsebool -P allow_ftpd_full_access on
  setsebool -P ftp_home_dir on
  这样应该没问题了(如果,还是不行,看看是不是用了ftp客户端工具用了passive模式访问了,如提示Entering Passive mode,就代表是passive模式,默认是不行的,因为ftp passive模式被iptables挡住了,下面会讲怎么开启,如果懒得开的话,就看看你客户端ftp是否有port模式的选项,或者把passive模式的选项去掉。如果客户端还是不行,看看客户端上的主机的电脑是否开了防火墙,关吧)
  FileZilla的主动、被动模式修改:
  菜单:编辑→设置

  6   关闭匿名访问
  修改/etc/vsftpd/vsftpd.conf文件:
  重启ftp服务:
  service vsftpd restart

nginx配置文件


  

#usernobody;  
worker_processes
1;  

  
#error_loglogs
/error.log;  
#error_loglogs/error.lognotice;
  
#error_loglogs/error.loginfo;
  

  
#pid      logs/nginx.pid;
  

  

  
events {
  worker_connections1024;
  
}
  

  

  
http {
  include       mime.types;
  default_typeapplication/octet-stream;
  

  log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
  '$status $body_bytes_sent "$http_referer" '
  '"$http_user_agent" "$http_x_forwarded_for"';
  

  #access_loglogs/access.logmain;
  

  sendfile      on;
  #tcp_nopush   on;
  

  #keepalive_timeout0;
  keepalive_timeout65;
  

  #gzipon;
  

  server {
  listen       80;
  server_namelocalhost;
  

  charset utf-8;
  

  #access_loglogs/host.access.logmain;
  

  

  

  location ~ .*\.(gif|jpg|jpeg|png)$ {
  expires 24h;
  root /home/ftpuser/picture/;#Ö¸¶¨Í¼Æ¬´æ·Å·¾¶
  access_log /home/ftpuser/picture/images.log;#ÈÕÖ¾´æ·Å·¾¶
  proxy_store on;
  proxy_store_access user:rw group:rw all:rw;
  proxy_temp_path   /home/ftpuser/picture/;#ͼƬ·ÃÎÊ·¾¶
  proxy_redirect   off;
  proxy_set_header    Host 127.0.0.1;
  client_max_body_size10m;
  client_body_buffer_size 1280k;
  proxy_connect_timeout900;
  proxy_send_timeout   900;
  proxy_read_timeout   900;
  proxy_buffer_size    40k;
  proxy_buffers      40 320k;
  proxy_busy_buffers_size 640k;
  proxy_temp_file_write_size 640k;
  if ( !-e $request_filename)
  {
  proxy_pass http://127.0.0.1;#ĬÈÏ80¶Ë¿Ú
  
            }
  }
  

  location / {
  root   html;
  indexindex.html index.htm;
  }
  #error_page404            /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_indexindex.php;
  #    fastcgi_paramSCRIPT_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 {
  
      #    denyall;
  #}
  }
  

  

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

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

  

  # HTTPS server
  #
  #server {
  #    listen       443 ssl;
  #    server_namelocalhost;
  

  #    ssl_certificate      cert.pem;
  #    ssl_certificate_keycert.key;
  

  #    ssl_session_cache    shared:SSL:1m;
  #    ssl_session_timeout5m;
  

  #    ssl_ciphersHIGH:!aNULL:!MD5;
  #    ssl_prefer_server_cipherson;
  

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

  
}
  


View Code
vsftpd配置


  

# Example config file /etc/vsftpd/vsftpd.conf  
#
  
# The default compiled in settings are fairly paranoid. This sample file
  
# loosens things up a bit, to make the ftp daemon more usable.
  
# Please see vsftpd.conf.5 for all compiled in defaults.
  
#
  
# READ THIS: This example file is NOT an exhaustive list of vsftpd options.

  
# Please read the vsftpd.conf.5 manual page to get a full>  
# capabilities.
  
#
  
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
  
anonymous_enable=NO
  
#
  
# Uncomment this to allow local users to log in.
  
# When SELinux is enforcing check for SE bool ftp_home_dir
  
local_enable=YES
  
#
  
# Uncomment this to enable any form of FTP write command.
  
write_enable=YES
  
#
  
# Default umask for local users is 077. You may wish to change this to 022,
  
# if your users expect that (022 is used by most other ftpd's)
  
local_umask=022
  
#
  
# Uncomment this to allow the anonymous FTP user to upload files. This only
  
# has an effect if the above global write enable is activated. Also, you will
  
# obviously need to create a directory writable by the FTP user.
  
# When SELinux is enforcing check for SE bool allow_ftpd_anon_write, allow_ftpd_full_access
  
anon_upload_enable=YES
  
#
  
# Uncomment this if you want the anonymous FTP user to be able to create
  
# new directories.
  
anon_mkdir_write_enable=YES
  
#
  
# Activate directory messages - messages given to remote users when they
  
# go into a certain directory.
  
dirmessage_enable=YES
  
#
  
# Activate logging of uploads/downloads.
  
xferlog_enable=YES
  
#
  
# Make sure PORT transfer connections originate from port 20 (ftp-data).
  
connect_from_port_20=YES
  
#
  
# If you want, you can arrange for uploaded anonymous files to be owned by
  
# a different user. Note! Using "root" for uploaded files is not
  
# recommended!
  
#chown_uploads=YES
  
#chown_username=whoever
  
#
  
# You may override where the log file goes if you like. The default is shown
  
# below.
  
xferlog_file=/var/log/xferlog
  
#
  
# If you want, you can have your log file in standard ftpd xferlog format.
  
# Note that the default log file location is /var/log/xferlog in this case.
  
xferlog_std_format=YES
  
#

  
# You may change the default value for timing out an>  
#idle_session_timeout=600
  
#
  
# You may change the default value for timing out a data connection.
  
#data_connection_timeout=120
  
#
  
# It is recommended that you define on your system a unique user which the
  
# ftp server can use as a totally isolated and unprivileged user.
  
#nopriv_user=ftpsecure
  
#
  
# Enable this and the server will recognise asynchronous ABOR requests. Not
  
# recommended for security (the code is non-trivial). Not enabling it,
  
# however, may confuse older FTP clients.
  
#async_abor_enable=YES
  
#
  
# By default the server will pretend to allow ASCII mode but in fact ignore
  
# the request. Turn on the below options to have the server actually do ASCII
  
# mangling on files when in ASCII mode.
  
# Beware that on some FTP servers, ASCII support allows a denial of service
  
# attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd

  
# predicted this attack and has always been safe, reporting the>  
# raw file.
  
# ASCII mangling is a horrible feature of the protocol.
  
#ascii_upload_enable=YES
  
#ascii_download_enable=YES
  
#
  
# You may fully customise the login banner string:
  
#ftpd_banner=Welcome to blah FTP service.
  
#
  
# You may specify a file of disallowed anonymous e-mail addresses. Apparently
  
# useful for combatting certain DoS attacks.
  
#deny_email_enable=YES
  
# (default follows)
  
#banned_email_file=/etc/vsftpd/banned_emails
  
#
  
# You may specify an explicit list of local users to chroot() to their home
  
# directory. If chroot_local_user is YES, then this list becomes a list of
  
# users to NOT chroot().
  
# (Warning! chroot'ing can be very dangerous. If using chroot, make sure that
  
# the user does not have write access to the top level directory within the
  
# chroot)
  
#chroot_local_user=YES
  
chroot_list_enable=NO
  
# (default follows)
  
#chroot_list_file=/etc/vsftpd/chroot_list
  
#
  
# You may activate the "-R" option to the builtin ls. This is disabled by
  
# default to avoid remote users being able to cause excessive I/O on large
  
# sites. However, some broken FTP clients such as "ncftp" and "mirror" assume
  
# the presence of the "-R" option, so there is a strong case for enabling it.
  
#ls_recurse_enable=YES
  
#
  
# When "listen" directive is enabled, vsftpd runs in standalone mode and
  
# listens on IPv4 sockets. This directive cannot be used in conjunction
  
# with the listen_ipv6 directive.
  
listen=no
  
#
  
# This directive enables listening on IPv6 sockets. By default, listening
  
# on the IPv6 "any" address (::) will accept connections from both IPv6
  
# and IPv4 clients. It is not necessary to listen on *both* IPv4 and IPv6
  
# sockets. If you want that (perhaps because you want to listen on specific
  
# addresses) then you must run two copies of vsftpd with two configuration
  
# files.
  
# Make sure, that one of the listen options is commented !!
  
listen_ipv6=YES
  

  
pam_service_name=vsftpd
  
userlist_enable=YES
  
tcp_wrappers=YES
  

  
#local_root=/home/image/
  
#chroot_local_user=YES
  
#anon_root=/home/image/
  


View Code  最后一步修改ftpuser的文件权限
  chown -R 755 /home/ftpuser/picture/
页: [1]
查看完整版本: nginx + vsftpd 搭建 图片服务器