nginx和ftp搭建圖片服務器


一、需要的組件

圖片服務器兩個服務:

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並立即生效的命令:

  1. #切換root管理員用戶  
  2. [root@localhost ~]# su  
  3. password   
  4. #設置本機IP並立即生效     
  5. [root@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庫。

  1. [root@localhost ~]#yum install -y pcre pcre-devel  

注:pcre-devel是使用pcre開發的一個二次開發庫。nginx也需要此庫。

n  zlib

         zlib庫提供了很多種壓縮和解壓縮的方式,nginx使用zlib對http包的內容進行gzip,所以需要在linux上安裝zlib庫。

 

  1. [root@localhost ~]#yum install -y zlib zlib-devel  

n  openssl

         OpenSSL是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及SSL協議,並提供豐富的應用程序供測試或其它目的使用。

         nginx不僅支持http協議,還支持https(即在ssl協議上傳輸http),所以需要在linux安裝openssl庫。

  1. [root@localhost ~]#yum install -y openssl openssl-devel  

 

1.2、把nginx安裝包nginx-1.8.0.tar.gz上傳到服務器。


在secureCRT打開sftp會話框,上傳文件

使用put/get命令 或者直接拖拽文件

1.3、解壓縮(在安裝包所在目錄執行)

  1. [root@localhost ~]# tar -zxvf nginx-1.8.0.tar.gz  

執行下面的命令創建makefile

  1. ./configure \  
  2.   
  3. --prefix=/usr/local/nginx \  
  4.   
  5. --pid-path=/var/run/nginx/nginx.pid \  
  6.   
  7. --lock-path=/var/lock/nginx.lock \  
  8.   
  9. --error-log-path=/var/log/nginx/error.log \  
  10.   
  11. --http-log-path=/var/log/nginx/access.log \  
  12.   
  13. --with-http_gzip_static_module \  
  14.   
  15. --http-client-body-temp-path=/var/temp/nginx/client \  
  16.   
  17. --http-proxy-temp-path=/var/temp/nginx/proxy \  
  18.   
  19. --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \  
  20.   
  21. --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \  
  22.   
  23. --http-scgi-temp-path=/var/temp/nginx/scgi  

注意:上邊將臨時文件目錄指定為/var/temp/nginx,需要在/var下創建temp及nginx目錄

  1. [root@bogon nginx-1.8.0]# mkdir /var/temp/nginx -p  


1.5、編譯安裝

編譯:

  1. [root@localhost nginx-1.8.0]# make  

安裝:

  1. [root@localhost nginx-1.8.0]# make  install  

安裝成功以后進入安裝目錄(創建makedir時指定的”--prefix=/usr/local/nginx \“)

  1. [root@localhost nginx-1.8.0]# cd /usr/local/nginx/  


2、nginx運行

2.1、啟動nginx

  1. [root@localhost nginx]# cd sbin  
  2. [root@localhost sbin]# ./nginx  


2.2、關閉

  1. [root@localhost sbin]# ./nginx -s stop  

2.3、重新加載配置文件

  1. [root@localhost sbin]# ./nginx -s reload  


2.4、關閉防火牆

1)關閉

  1. [root@localhost sbin]# service iptables stop  
  2.   
  3. iptables: Flushing firewall rules:                         [  OK  ]  
  4.   
  5. iptables: Setting chains to policy ACCEPT: filter          [  OK  ]  
  6.   
  7. iptables: Unloading modules:                               [  OK  ]  

2)也可以修改防火牆配置文件:

  1. [root@localhost sbin]# vim /etc/sysconfig/iptables  
  1. //在倒數第二行加入80端口    
  2. -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT   

修改后需要重啟防火牆:

  1. [root@localhost sbin]# service iptables restart  

3)另外一種解決辦法

  1. [root@localhost ]# /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT    
  2. [root@localhost ]# /etc/init.d/iptables save    
  3. [root@localhost ]# /etc/init.d/iptables restart   

2.5、訪問nginx服務


3、關於圖片服務器配置

進入配置文件目錄

  1. cd /usr/local/nginx/conf/  

nginx的默認配置文件nginx.config

  1. #user  nobody;  
  2. worker_processes  1;  
  3.   
  4. #error_log  logs/error.log;  
  5. #error_log  logs/error.log  notice;  
  6. #error_log  logs/error.log  info;  
  7.   
  8. #pid        logs/nginx.pid;  
  9.   
  10.   
  11. events {  
  12.     worker_connections  1024;  
  13. }  
  14.   
  15.   
  16. http {  
  17.     include       mime.types;  
  18.     default_type  application/octet-stream;  
  19.   
  20.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  21.     #                  '$status $body_bytes_sent "$http_referer" '  
  22.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  23.   
  24.     #access_log  logs/access.log  main;  
  25.   
  26.     sendfile        on;  
  27.     #tcp_nopush     on;  
  28.   
  29.     #keepalive_timeout  0;  
  30.     keepalive_timeout  65;  
  31.   
  32.     #gzip  on;  
  33.   
  34.     server {  
  35.         listen       80;  
  36.         server_name  localhost;  
  37.   
  38.         #charset koi8-r;  
  39.   
  40.         #access_log  logs/host.access.log  main;  
  41.   
  42.         location / {  
  43.             root   html;  
  44.             index  index.html index.htm;  
  45.         }  
  46.   
  47.         #error_page  404              /404.html;  
  48.   
  49.         # redirect server error pages to the static page /50x.html  
  50.         #  
  51.         error_page   500 502 503 504  /50x.html;  
  52.         location = /50x.html {  
  53.             root   html;  
  54.         }  
  55.   
  56.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  57.         #  
  58.         #location ~ \.php$ {  
  59.         #    proxy_pass   http://127.0.0.1;  
  60.         #}  
  61.   
  62.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  63.         #  
  64.         #location ~ \.php$ {  
  65.         #    root           html;  
  66.         #    fastcgi_pass   127.0.0.1:9000;  
  67.         #    fastcgi_index  index.php;  
  68.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  69.         #    include        fastcgi_params;  
  70.         #}  
  71.   
  72.         # deny access to .htaccess files, if Apache's document root  
  73.         # concurs with nginx's one  
  74.         #  
  75.         #location ~ /\.ht {  
  76.         #    deny  all;  
  77.         #}  
  78.     }  
  79.   
  80.   
  81.     # another virtual host using mix of IP-, name-, and port-based configuration  
  82.     #  
  83.     #server {  
  84.     #    listen       8000;  
  85.     #    listen       somename:8080;  
  86.     #    server_name  somename  alias  another.alias;  
  87.   
  88.     #    location / {  
  89.     #        root   html;  
  90.     #        index  index.html index.htm;  
  91.     #    }  
  92.     #}  
  93.   
  94.   
  95.     # HTTPS server  
  96.     #  
  97.     #server {  
  98.     #    listen       443 ssl;  
  99.     #    server_name  localhost;  
  100.   
  101.     #    ssl_certificate      cert.pem;  
  102.     #    ssl_certificate_key  cert.key;  
  103.   
  104.     #    ssl_session_cache    shared:SSL:1m;  
  105.     #    ssl_session_timeout  5m;  
  106.   
  107.     #    ssl_ciphers  HIGH:!aNULL:!MD5;  
  108.     #    ssl_prefer_server_ciphers  on;  
  109.   
  110.     #    location / {  
  111.     #        root   html;  
  112.     #        index  index.html index.htm;  
  113.     #    }  
  114.     #}  
  115.   
  116. }  

配置圖片服務器

方法一、在配置文件server{}中location /{} 修改配置:

  1.  #默認請求  
  2. location / {  
  3.    root  /home/ftpuser/www;#定義服務器的默認網站根目錄位置  
  4.    index index.html index.php index.htm;#定義首頁索引文件的名稱  
  5. }  

其中:/home/ftpuser/www;為創建FTP服務賬戶ftpuser的根目錄下的www目錄

方法二、在http{}內配置新服務

  1. server {  
  2.         listen       8080;  
  3.         server_name  localhost;  
  4.   
  5.         #charset utf-8;  
  6.   
  7.         #access_log  logs/host.access.log  main;  
  8.   
  9.         #默認請求  
  10.         location / {  
  11.             root  /home/ftpuser/www;#定義服務器的默認網站根目錄位置  
  12.             index index.html index.php index.htm;#定義首頁索引文件的名稱  
  13.            }  
  14.         }  

因為需要開始端口號8080,所以要在防火牆中開啟8080端口

  1. [root@localhost ]# /sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT    
  2. [root@localhost ]# /etc/init.d/iptables save    
  3. [root@localhost ]# /etc/init.d/iptables restart   


三、FTP服務的安裝與啟動

1、安裝vsftpd組件

vsftpd組件為Linux的FTP服務組件,安裝方式為在線安裝。

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

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

2、添加一個ftp用戶

此用戶就是用來登錄ftp服務器用的。

  1. [root@localhost ~]# useradd ftpuser  

 

這樣一個用戶建完,可以用這個登錄,記得用普通登錄不要用匿名了。登錄后默認的路徑為 /home/ftpuser.

為這個ftp賬戶添加密碼

  1. [root@localhost ~]# passwd ftpuser  

輸入兩次密碼后修改密碼。

3、 防火牆開啟21端口

因為ftp默認的端口為21,而centos默認是沒有開啟的,所以要修改iptables文件

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

在行上面有22 -j ACCEPT 下面另起一行輸入跟那行差不多的,只是把22換成21,然后:wq保存。

還要運行下,重啟iptables

  1. [root@localhost ~]# service iptables restart  

4、 修改selinux

外網是可以訪問上去了,可是發現沒法返回目錄(使用ftp的主動模式,被動模式還是無法訪問),也上傳不了,因為selinux作怪了。

修改selinux:

執行以下命令查看狀態:

[root@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,代表,沒有開啟外網的訪問

[root@localhost ~]# setsebool -P allow_ftpd_full_access on [root@localhost ~]# setsebool -P ftp_home_dir on

這樣應該沒問題了(如果,還是不行,看看是不是用了ftp客戶端工具用了passive模式訪問了,如提示Entering Passive mode,就代表是passive模式,默認是不行的,因為ftp passive模式被iptables擋住了,下面會講怎么開啟,如果懶得開的話,就看看你客戶端ftp是否有port模式的選項,或者把passive模式的選項去掉。如果客戶端還是不行,看看客戶端上的主機的電腦是否開了防火牆,關吧)

FileZilla的主動、被動模式修改:

菜單:編輯→設置


5、關閉匿名訪問

修改/etc/vsftpd/vsftpd.conf文件:

重啟ftp服務:

[root@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服務

[root@localhost ~]# chkconfig vsftpd on

四、部署驗證

在www下新建文件夾images,下面放一張圖片001.jpg

測試訪問:http://192.168.1.110/images/001.jpg




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM