淑昊柠 发表于 2017-12-23 12:28:14

Linux 安装及配置 Nginx + ftp 服务器

  Nginx 安装及配置
  一、Nginx 简介:

  Nginx("engine x") 是一款是由俄罗斯的程序设计师 Igor Sysoev 所开发高性能的 Web和 反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器。在高连接并发的情况下,Nginx 是 Apache 服务器不错的替代品。是 C 语言编写的,建议在 Linux 运行。
  二、环境软件版本准备:

  系统平台:CentOS>  Nginx:nginx-1.10.3.tar.gz   下载地址: http://nginx.org/download/nginx-1.10.3.tar.gz
  PCRE:pcre-8.35.tar.gz   下载地址: http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
  三、安装编译工具及库文件:

# yum -y install make zlib zlib-devel gcc-c++ libtoolopenssl openssl-devel  gcc:安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境。
  zlib:zlib 库提供了很多种压缩和解压缩的方式,nginx 使用 zlib 对 http 包的内容进行 gzip。
  openssl:OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。nginx 不仅支持http 协议,还支持 https(即在 ssl 协议上传输 http),所以需要在 linux 安装 openssl 库。
  PCRE:PCRE(Perl Compatible Regular Expressions) 是一个 Perl 库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre库。
  安装 PCRE:
  1.解压安装包

# tar -zxvfpcre-8.35.tar.gz -C ../softwares/  2.进入安装目录:

# cd /opt/softwares/pcre-8.35/  3.编译安装:

# ./configure
# make && make install  4.查看 pcre 版本:

# pcre-config ––version  四、Nginx 安装:
  1.解压安装包:

# tar -zxvf nginx-1.10.3.tar.gz -C ../softwares/  2.进入安装目录:

# cd /opt/softwares/nginx-1.10.3/  3.编译安装:注意:编译时将临时文件目录指定为 /var/temp/nginx ,需要在/var 目录下递归创建 /temp 和 /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

# make && make install  4.启动报错修复:

# cd /usr/local/nginx/sbin/# ./nginx
  ./nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
  从错误看是缺少 lib 包导致的,进一步查看一下:
# ldd $(which /usr/local/nginx/sbin/nginx)
  linux-vdso.so.1 =>(0x00007fff87dff000)
  libdl.so.2 => /lib64/libdl.so.2 (0x000000378a000000)
  libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003789c00000)
  libcrypt.so.1 => /lib64/libcrypt.so.1 (0x000000378d400000)
  libpcre.so.1 => not found
  libcrypto.so.10 => /usr/lib64/libcrypto.so.10 (0x0000003bb6c00000)
  libz.so.1 => /lib64/libz.so.1 (0x0000003789400000)
  libc.so.6 => /lib64/libc.so.6 (0x0000003789800000)
  /lib64/ld-linux-x86-64.so.2 (0x0000003789000000)
  libfreebl3.so => /lib64/libfreebl3.so (0x000000378d000000)
  从上面的信息可以看出 libpcre.so.1 => not found ,也就是没有找到libpcre.so.1, 我们进入 /lib64 自己手动链接下。

# cd /lib64/
# ln -s libpcre.so.0.0.1 libpcre.so.1  5.查看 nginx 版本:
# cd /usr/local/nginx/sbin/
# ./nginx -v
  nginx version: nginx/1.10.3
  6.nginx 常用命令:
  

## 启动 nginx  
# ./nginx
  
## 停止 nginx
  
## -s都是采用向 Nginx 发送信号的方式
  
# ./nginx -s stop
  
# ./nginx -s quit
  
## Nginx 重载配置

  
# ./nginx -s>  

  7.设置防火墙:
  CentOS 默认是不开放 80 端口的,这样导致了配置完 Nginx 只能在本机访问(127.0.0.1) 局域网内访问不了 。
  ① 查看防火墙状态:
# service iptables status
  表格:filter
  Chain INPUT (policy ACCEPT)
  numtarget   prot opt source               destination         

  1    ACCEPT   all--0.0.0.0/0            0.0.0.0/0         state>  2    ACCEPT   icmp --0.0.0.0/0            0.0.0.0/0         
  3    ACCEPT   all--0.0.0.0/0            0.0.0.0/0         
  4    ACCEPT   tcp--0.0.0.0/0            0.0.0.0/0         state NEW tcp dpt:22
  5    ACCEPT   tcp--0.0.0.0/0            0.0.0.0/0         state NEW tcp dpt:3306
  6 REJECT   all--0.0.0.0/0            0.0.0.0/0         reject-with icmp-host-prohibited
  Chain FORWARD (policy ACCEPT)
  numtarget   prot opt source               destination         
  1    REJECT   all--0.0.0.0/0            0.0.0.0/0         reject-with icmp-host-prohibited
  Chain OUTPUT (policy ACCEPT)
  numtarget   prot opt source               destination
  ② 开放 80 端口:
# vim /etc/sysconfig/iptables
  # Firewall configuration written by system-config-firewall
  # Manual customization of this file is not recommended.
  *filter
  :INPUT ACCEPT
  :FORWARD ACCEPT
  :OUTPUT ACCEPT
  -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  -A INPUT -p icmp -j ACCEPT
  -A INPUT -i lo -j ACCEPT
  -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
  -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
  -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
  -A INPUT -j REJECT --reject-with icmp-host-prohibited
  -A FORWARD -j REJECT --reject-with icmp-host-prohibited
  COMMIT
  ③ 重启防火墙:
# service iptables restart
  iptables:将链设置为政策 ACCEPT:filter                  [确定]
  iptables:清除防火墙规则:                                 [确定]
  iptables:正在卸载模块:                                 [确定]
  iptables:应用防火墙规则:                                 [确定]
  8.nginx启动成功状态:
  
  FTP 安装及配置
  一、安装vsftpd组件:

# yum -y install vsftpd  二、添加一个 FTP 用户:此用户就是用来登陆 FTP 服务器用的。
  ① 创建用户:

# useradd ftpuser  创建完用户,可以用这个用户登录,记得用普通登陆,最好不要匿名登陆了。
  ② 查看是否创建成功:
# ls
  ftpuserlost+found
  ③ 为账户添加密码:建议 8 位以上密码
# passwd ftpuser
  更改用户 ftpuser 的密码 。
  新的 密码:
  重新输入新的 密码:
  passwd: 所有的身份验证令牌已经成功更新。
  三、设置防火墙:
  FTP 服务器默认端口为 21, 而 CentOS 默认是不开放 21 端口的。
  ① 开放 21 端口:
# vim /etc/sysconfig/iptables
  # Firewall configuration written by system-config-firewall
  # Manual customization of this file is not recommended.
  *filter
  :INPUT ACCEPT
  :FORWARD ACCEPT
  :OUTPUT ACCEPT
  -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  -A INPUT -p icmp -j ACCEPT
  -A INPUT -i lo -j ACCEPT
  -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
  -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
  -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
  -A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT
  -A INPUT -j REJECT --reject-with icmp-host-prohibited
  -A FORWARD -j REJECT --reject-with icmp-host-prohibited
  COMMIT
  ② 重启防火墙:
# service iptables restart
  iptables:将链设置为政策 ACCEPT:filter                  [确定]
  iptables:清除防火墙规则:                                 [确定]
  iptables:正在卸载模块:                                 [确定]
  iptables:应用防火墙规则:                                 [确定]
  四、修改 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_fusefs --> off
  ftpd_use_passive_mode --> off
  httpd_enable_ftp_server --> off
  tftp_anon_write --> off
  tftp_use_cifs --> off
  tftp_use_nfs --> off
  注意: 标注两行为 off,代表没有开启外网的访问。
  ② 开启外网访问:
# setsebool -P allow_ftpd_full_access on
# setsebool -P ftp_home_dir on
# getsebool -a | grep ftp
  allow_ftpd_anon_write --> off
  allow_ftpd_full_access --> on
  allow_ftpd_use_cifs --> off
  allow_ftpd_use_nfs --> off
  ftp_home_dir --> on
  ftpd_connect_db --> off
  ftpd_use_fusefs --> off
  ftpd_use_passive_mode --> off
  httpd_enable_ftp_server --> off
  tftp_anon_write --> off
  tftp_use_cifs --> off
  tftp_use_nfs --> off
  已经开启。
  五、关闭匿名访问:
  将 /etc/vsftpd/vsftpd.conf 文件中 anonymous_enable=YES 改成 NO

# vim /etc/vsftpd/vsftpd.conf  六、开启被动模式:
  ① 设置端口范围:被动模式默认是开启的,但是我们要设置一个端口范围,在 vsftpd.conf 文件结尾加上端口范围, 如:
  pasv_min_port=30000
  pasv_max_port=30999
  ② 重启 vsftpd:
# service vsftpd restart
  关闭 vsftpd:                                              [失败]
  为 vsftpd 启动 vsftpd:                                    [确定]
  ③ 设置防火墙端口:
# vim /etc/sysconfig/iptables
  # Firewall configuration written by system-config-firewall
  # Manual customization of this file is not recommended.
  *filter
  :INPUT ACCEPT
  :FORWARD ACCEPT
  :OUTPUT ACCEPT
  -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  -A INPUT -p icmp -j ACCEPT
  -A INPUT -i lo -j ACCEPT
  -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
  -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
  -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
  -A INPUT -m state --state NEW -m tcp -p tcp --dport 21 -j ACCEPT
  -A INPUT -m state --state NEW -m tcp -p tcp --dport 30000:30999 -j ACCEPT
  -A INPUT -j REJECT --reject-with icmp-host-prohibited
  -A FORWARD -j REJECT --reject-with icmp-host-prohibited
  COMMIT
  ④ 重启防火墙:
# service iptables restart
  iptables:将链设置为政策 ACCEPT:filter                  [确定]
  iptables:清除防火墙规则:                                 [确定]
  iptables:正在卸载模块:                                 [确定]
  iptables:应用防火墙规则:                                 [确定]
  七、设置开机自启 vsftpd FTP服务:

# chkconfig vsftpd on  配置 Nginx + FTP 服务器
  一、配置Nginx 服务器:
  1. 进入nginx 配置文件目录:
# cd /usr/local/nginx/conf/
# ls
  fastcgi.conf          fastcgi_params.defaultmime.types          nginx.conf.default   uwsgi_params
  fastcgi.conf.defaultkoi-utf               mime.types.defaultscgi_params          uwsgi_params.default
  fastcgi_params      koi-win               nginx.conf          scgi_params.defaultwin-utf
  2. 修改配置文件:有两种方式
  ①方式一:在配置文件server{}中location /{} 修改配置
  

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

  ②方式二:在http{}内配置新服务
  

1 server {  

2         listen       8080;  

3         server_namelocalhost;  

4  
5         #charset utf-8;
  
6   
  
7         #access_loglogs/host.access.logmain;
  
8   
  
9         #默认请求
  
10         location / {
  
11             root/home/ftpuser/www;#定义服务器的默认网站根目录位置
  
12             index index.html index.php index.htm;#定义首页索引文件的名称
  
13            }
  
14         }
  

  部署验证
  
  出现403问题。
  解决方案:
  1.查看配置文件中路径是否配置成功:
  

location / {  
root   
/home/ftpuser/www;  
indexindex.html index.htm;
  
}
  

# cd /home/ftpuser/www/
# pwd
  /home/ftpuser/www
  两个路径完全匹配,说明路径没有问题。
  2.查看路径中是否存在文件:
# ls
  index.html
  存在文件,可以排除是文件问题。
  3.排查权限问题:
  

# cat nginx.conf  

  
#usernobody;
  
worker_processes1;
  

  
#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 koi8-r;
  

  
#access_loglogs/host.access.logmain;
  

  
location / {
  
root   /home/ftpuser/www;
  
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;
  
#    }
  
#}
  

  
}
  

  发现用户权限没有开启。我们添加需要的用户。
# vim nginx.conf
  #user nobody;
  userroot;
  重新加载 nginx 配置:
# cd ../sbin/

# ./nginx -s>  重新验证
  

mayiwen123456 发表于 2017-12-23 14:07:43

本帖最后由 mayiwen123456 于 2017-12-23 14:44 编辑

试试
页: [1]
查看完整版本: Linux 安装及配置 Nginx + ftp 服务器