一、需要的組件
圖片服務器兩個服務:
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並立即生效的命令:
- #切換root管理員用戶
- [root@localhost ~]# su
- password
- #設置本機IP並立即生效
- [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庫。
- [root@localhost ~]#yum install -y pcre pcre-devel
注:pcre-devel是使用pcre開發的一個二次開發庫。nginx也需要此庫。
n zlib
zlib庫提供了很多種壓縮和解壓縮的方式,nginx使用zlib對http包的內容進行gzip,所以需要在linux上安裝zlib庫。
- [root@localhost ~]#yum install -y zlib zlib-devel
n openssl
OpenSSL是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及SSL協議,並提供豐富的應用程序供測試或其它目的使用。
nginx不僅支持http協議,還支持https(即在ssl協議上傳輸http),所以需要在linux安裝openssl庫。
- [root@localhost ~]#yum install -y openssl openssl-devel
1.2、把nginx安裝包nginx-1.8.0.tar.gz上傳到服務器。
在secureCRT打開sftp會話框,上傳文件
使用put/get命令 或者直接拖拽文件
1.3、解壓縮(在安裝包所在目錄執行)
- [root@localhost ~]# tar -zxvf nginx-1.8.0.tar.gz
在root@bogon nginx-1.8.0目錄下
./configure && make && make install //在這裏吃過虧
1.5、編譯安裝
編譯:
- [root@localhost nginx-1.8.0]# make
- [root@localhost nginx-1.8.0]# make install
- [root@localhost nginx-1.8.0]# cd /usr/local/nginx/
2、nginx運行
2.1、啓動nginx
- [root@localhost nginx]# cd sbin
- [root@localhost sbin]# ./nginx
2.2、關閉
- [root@localhost sbin]# ./nginx -s stop
2.3、重新加載配置文件
- [root@localhost sbin]# ./nginx -s reload
2.4、關閉防火牆
- [root@localhost ]# /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
- [root@localhost ]# /etc/init.d/iptables save
- [root@localhost ]# /etc/init.d/iptables restart
2.5、訪問nginx服務
順便給新手推薦一個無意中發現的Java網站,感覺超級適合入門級的Java開發,
基礎知識寫的挺好,練手項目也很豐富很,基本都是文檔形式指導,簡明易懂,不像視頻一樣費時間。
地址是http://how2j.cn?p=24192
3、關於圖片服務器配置
進入配置文件目錄
- cd /usr/local/nginx/conf/
- #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 /{} 修改配置:
- #默認請求
- location / {
- root /home/ftpuser/www;#定義服務器的默認網站根目錄位置
- index index.html index.php index.htm;#定義首頁索引文件的名稱
- }
其中:/home/ftpuser/www;爲創建FTP服務賬戶ftpuser的根目錄下的www目錄
方法二、在http{}內配置新服務
- 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端口
- [root@localhost ]# /sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
- [root@localhost ]# /etc/init.d/iptables save
- [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服務器用的。
- [root@localhost ~]# useradd ftpuser
這樣一個用戶建完,可以用這個登錄,記得用普通登錄不要用匿名了。登錄後默認的路徑爲 /home/ftpuser.
爲這個ftp賬戶添加密碼
- [root@localhost ~]# passwd ftpuser
輸入兩次密碼後修改密碼。
3、 防火牆開啓21端口
因爲ftp默認的端口爲21,而centos默認是沒有開啓的,所以要修改iptables文件
[root@localhost ~]# vim /etc/sysconfig/iptables
在行上面有22 -j ACCEPT 下面另起一行輸入跟那行差不多的,只是把22換成21,然後:wq保存。
還要運行下,重啓iptables
- [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
樓主搭建過程遇到的問題:
1:ftp上傳的文件,不在指定目錄內?
原因:可能指定的目錄文件夾權限不夠,通過chmod 進行權限的分配
2:通過nginx訪問不到圖片?
需要修改nginx.conf 的第一行 user nobody 修改爲root或者ftp的用戶名;
文章爲轉載,通過這篇文章,成功搭建起來,java後臺測試通過,如有哪裏不對,請指正!
Java測試代碼:
public class TextFtpIamge {
private final static Log logger = LogFactory.getLog(TextFtpIamge.class);
public static void main(String[] args) throws Exception {
String ftpHost= "192.168.81.53"; //ftp服務器地址
int ftpPort = 21;//ftp服務員器端口號
String ftpUserName = "ftpuser";//anonymous匿名用戶登錄,不需要密碼。administrator指定用戶登錄
String ftpPassword = "ftpuser";//指定用戶密碼
String ftpPath = "/home/ftpuser/images/aa"; //ftp文件存放物理路徑
String filePath=""; //文件路徑
String fileName="";//文件名稱
// filePath="E:\\";
// fileName="1.jpg";
// downloadFile(ftpHost, ftpUserName, ftpPassword, ftpPort,
// ftpPath, filePath, fileName);
filePath="C:\\Users\\admin\\Desktop";
fileName="1.jpg";
FileInputStream input=new FileInputStream(new File(filePath+File.separatorChar+fileName));
uploadFile(ftpHost, ftpUserName, ftpPassword, ftpPort, ftpPath, fileName, input);
}
/**
* 獲取FTPClient對象
*
* @param ftpHost
* FTP主機服務器
* @param ftpPassword
* FTP 登錄密碼
* @param ftpUserName
* FTP登錄用戶名
* @param ftpPort
* FTP端口 默認爲21
* @return
*/
public static FTPClient getFTPClient(String ftpHost, String ftpUserName,
String ftpPassword, int ftpPort) {
FTPClient ftpClient = new FTPClient();
try {
ftpClient = new FTPClient();
ftpClient.connect(ftpHost, ftpPort);// 連接FTP服務器
ftpClient.login(ftpUserName, ftpPassword);// 登陸FTP服務器
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
logger.info("未連接到FTP,用戶名或密碼錯誤。");
ftpClient.disconnect();
} else {
logger.info("FTP連接成功。");
}
} catch (SocketException e) {
e.printStackTrace();
logger.info("FTP的IP地址可能錯誤,請正確配置。");
} catch (IOException e) {
e.printStackTrace();
logger.info("FTP的端口錯誤,請正確配置。");
}
return ftpClient;
}
/**
* 下載文件
*
* @param ftpHost ftp服務器地址
* @param ftpUserName anonymous匿名用戶登錄,不需要密碼。administrator指定用戶登錄
* @param ftpPassword 指定用戶密碼
* @param ftpPort ftp服務員器端口號
* @param ftpPath ftp文件存放物理路徑
* @param fileName 文件路徑
* @param input 文件輸入流,即從本地服務器讀取文件的IO輸入流
*/
public static void downloadFile(String ftpHost, String ftpUserName,
String ftpPassword, int ftpPort, String ftpPath, String localPath,
String fileName) {
FTPClient ftpClient = null;
try {
ftpClient = getFTPClient(ftpHost, ftpUserName, ftpPassword, ftpPort);
ftpClient.setControlEncoding("UTF-8"); // 中文支持
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
ftpClient.changeWorkingDirectory(ftpPath);
File localFile = new File(localPath + File.separatorChar + fileName);
OutputStream os = new FileOutputStream(localFile);
ftpClient.retrieveFile(fileName, os);
os.close();
ftpClient.logout();
} catch (FileNotFoundException e) {
logger.error("沒有找到" + ftpPath + "文件");
e.printStackTrace();
} catch (SocketException e) {
logger.error("連接FTP失敗.");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
logger.error("文件讀取錯誤。");
e.printStackTrace();
}
}
/**
* 上傳文件
*
* @param ftpHost ftp服務器地址
* @param ftpUserName anonymous匿名用戶登錄,不需要密碼。administrator指定用戶登錄
* @param ftpPassword 指定用戶密碼
* @param ftpPort ftp服務員器端口號
* @param ftpPath ftp文件存放物理路徑
* @param fileName 文件路徑
* @param input 文件輸入流,即從本地服務器讀取文件的IO輸入流
*/
public static void uploadFile(String ftpHost, String ftpUserName,
String ftpPassword, int ftpPort, String ftpPath,
String fileName,InputStream input){
FTPClient ftp=null;
try {
ftp=getFTPClient(ftpHost, ftpUserName, ftpPassword, ftpPort);
ftp.makeDirectory(ftpPath);//如果路徑不存在,就創建
ftp.changeWorkingDirectory(ftpPath);//轉到指定上傳路徑
//ftp 默認是iso-8859-1編碼,防止中文亂碼
fileName=new String(fileName.getBytes("GBK"),"iso-8859-1");
ftp.setFileType(FTP.BINARY_FILE_TYPE);
//fileName 上傳到服務器文件名稱
ftp.storeFile(fileName, input);
input.close();
ftp.logout();
System.out.println("upload succes!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
一隻行走的小笨猿!