由於要實現圖片上傳,且服務器都要用阿里雲的,Jboss也在Linux系統上,就採用了Nginx和FTP(也可採用nginx+NFS)。
本篇博客主要介紹搭建Nginx和FTP的基本過程,部分簡單且過細步驟從略,衆親自行百度即可。
1、搭建Nginx
(1)下載安裝包,安裝,Linux下安裝nginx,需要先安裝Gcc編譯器、PCRE庫、zlib庫、OpenSSL開發庫。然後再安裝nginx。我用的Nginx-1.7.4版本,具體安裝過程百度一下吧~
(2)啓動命令:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf。 /usr/local/nginx/sbin/nginx是你nginx的安裝目錄;nginx.conf是配置文件。
注意:要確定系統的80端口是否被佔用,如果被佔用,可用vi命令修改nginx.conf,具體修改方法見下面的配置文件。
檢查是否啓動成功:ps -ef | grep nginx。
如果成功了,看看在瀏覽器中輸入http://127.0.0.1 是否訪問到了Nginx歡迎界面?訪問到就恭喜你安裝成功了!
(3)下面是如何配置nginx.conf,使其成爲圖片服務器。
user root;
worker_processes 1;
worker_rlimit_nofile 65535;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 12040;
}
http {
include mime.types;
default_type application/octet-stream;
#Proxy_cache_path /usr/local/nginx/NginxTestImgLog levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
#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 8088;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
#proxy_pass http://192.168.10.223:1234;
}
#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 7788;
server_name localhost;
# ssl on;
# ssl_certificate /usr/local/nginx/conf/server.crt;
# ssl_certificate_key /usr/local/nginx/conf/server_nopwd.key;
charset utf-8;
#charset koi8-r;
location ~ (\.jsp)|(\.do)/ {
proxy_pass http://127.0.0.1:7001;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_max_temp_file_size 512m;
}
}
server {
listen 8888;
server_name localhost;
charset utf-8;
location ~ .*\.(gif|jpg|jpeg|png){
#allow 127.0.0.1;
#deny all;
#expires 24h;
root /usr/local/nginx/nginxTestImg/;
access_log /usr/local/nginx/NginxTestImgLog/log_test.log;
proxy_store on;
proxy_store_access user:rw group:rw all:rw;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
client_max_body_size 10m;
client_body_buffer_size 1280k;
proxy_connect_timeout 900;
proxy_send_timeout 900;
proxy_read_timeout 900;
proxy_buffer_size 1024k;
proxy_buffers 40 1024k;
proxy_busy_buffers_size 1024k;
proxy_temp_file_write_size 1024k;
proxy_temp_path /usr/local/nginx/nginxTestImg/;
#Proxy_cache_path /nginxTestImg/;
if ( !-e $request_filename)
{
proxy_pass http://127.0.0.1:8888;
}
}
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;
}
}
}
配置完後,執行reload命令重新加載配置文件。然後放入一張圖片(如,cat.jpg)後進行訪問http://127.0.0.1:8888/cat.jpg ,如果能訪問,說明搭建成功。
2、搭建FTP服務器
(1)安裝,使用yum命令在Linux上安裝即可
(2)啓動ftp服務 cd /ect/vsftpd(ftp的安裝目錄) 換行,./vsftpd.conf 或者service vsftpd restart(通用)
(3)之後比較關鍵的是修改配置文件:
首先,新建一個用戶,如apple,併爲其設置密碼
# useradd -d /etc/vsftpd/apple apple //增加用戶apple,並制定test用戶的主目錄爲/etc/vsftpd/apple
# passwd apple//爲test設置密碼
然後,把apple加到/etc/vsftpd/user_list文件中;如果該文件的同級文件vsftpd.chroot_list和ftpusers中含有你註冊的用戶名,要將其在這兩個文件中注掉。
重啓ftp服務,命令見(2),就OK啦。
還有一點要注意的是,如果使用ftp過程總是說無權限,要看看是否你的vsftpd裏的文件有讀寫權限,設置屬性即可。
之後,在你的apple文件夾下放一個cat.jpg文件,在瀏覽器中輸入http://xxx.xxx.xxx.xxx/8888/apple/cat.jpg,看看是否看到了你的圖片?