環境:centos6.4 min
#安裝編譯庫及依賴模塊?
yum -y install gcc gcc-c++ autoconf automake make?
yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel?
#上傳需要的軟件
#安裝JDK與Tomcat?
查看當系統JDK版本 java –version?
#修改權限?
chmod 777? jdk-6u41-linux-i586-rpm.bin?
./jdk-6u41-linux-i586-rpm.bin?
#可以不加?
vi /etc/profile?
#加入?
JAVA_HOME=/usr/java/jdk1.6.0_41?
CLASSPATH=.:
J
A
V
A
H
O
M
E
/
l
i
b
P
A
T
H
=
JAVA_HOME/bin:$PATH?
export JAVA_HOME CLASSPATH PATH?
#保存退出?
:wq?
#解壓Tomcat?
tar zxvf apache-tomcat-6.0.36.tar.gz
#安裝Mysql?
#是否已經安裝了mysql數據庫?
rpm -qa | grep mysql?
#卸載Mysql?
rpm -e mysql?
rpm -e --nodeps mysql?
#可下載的版本?
yum list | grep mysql?
#安裝Mysql?
yum install -y mysql-server mysql mysql-deve?
#查看?
rpm -qi mysql-server?
#啟動mysql服務? 首先會初始化配置?
service mysqld start?
#重啟Mysql的服務?
service mysqld restart?
#開機自動啟動?
chkconfig --list | grep mysqld?
chkconfig mysqld on?
#為root賬號設置密碼?
mysqladmin -u root password '123'?
#登錄Mysql?
mysql -u root -p?
show databases;?
#查看配置?
cat /etc/my.cnf?
#數據庫存放位置?
cd /var/lib/mysql/?
#mysql數據庫日志?
cat /var/log/mysqld.log?
#查看端口?
netstat –anp
#Mysql編碼問題?
#MySQL數據庫默認的編碼?
character set :latin1?
collation : latin1_swedish_ci?
#查看當前編碼?
SHOW VARIABLES LIKE 'character_set%';?
+--------------------------+----------------------------+?
| Variable_name??????????? | Value????????????????????? |?
+--------------------------+----------------------------+?
| character_set_client???? | latin1???????????????????? |?
| character_set_connection | latin1???????????????????? |?
| character_set_database?? | latin1???????????????????? |?
| character_set_filesystem | binary???????????????????? |?
| character_set_results??? | latin1???????????????????? |?
| character_set_server???? | latin1???????????????????? |?
| character_set_system???? | utf8?????????????????????? |?
| character_sets_dir?????? | /usr/share/mysql/charsets/ |?
+--------------------------+----------------------------+?
#查看MySQL支持的編碼?
mysql> show character set;?
#修改編碼?
vi /etc/my.cnf?
[client]?
default-character-set=utf8?
[mysql]?
default-character-set=utf8?
[mysqld]?
default-character-set = utf8????
collation-server = utf8_unicode_ci?
init-connect='SET NAMES utf8'?
character-set-server = utf8
找到[mysqld]下增加(未測試)?
CentOS 5之前版本?
character-set-server=utf8?
CentOS 6以上的版本?
character-set-server=utf8?
#修改一個已有數據庫的編碼?
ALTER DATABASE linuxcast CHARACTER SET utf8 COLLATE utf8_general_ci;?
#創建數據庫的時候指定編碼?
CREATE DATABASE testDB?
? DEFAULT CHARACTER SET utf8?
? DEFAULT COLLATE utf8_general_ci;
#查看編碼
#重啟MySQL服務?
service mysqld restart?
#防火墻端口?
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT?
service iptables save?
service iptables restart?
service iptables stop?
#設置mysql遠程訪問?
mysql -u root -p?
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'passwd' WITH GRANT OPTION;?
flush privileges;
#安裝Nginx?
yum install wget?
cd /opt?
wget?
http://nginx.org/download/nginx-1.2.8.tar.gz
?
tar zxvf nginx-1.2.8.tar.gz??
cd nginx-1.2.8?
./configure --prefix=/opt/nginx --with-http_stub_status_module --with-http_gzip_static_module?
--with-http_ssl_module
?
make && make install
配置參數:?
--prefix=path?? 定義服務器保存文件的目錄,默認為/usr/local/nginx?
--sbin-path=path?? nginx執行文件名,默認為prefix/sbin/nginx?
--conf-path=path?? 設置nginx.conf配置文件名,默認為prefix/conf/nginx.conf?
--pid-path=path??? 設置nginx.pid文件名,它用來保存nginx主進程的進程ID,默認為prefix/logs/nginx.pid?
--error-log-path=path?? 設置錯誤日志文件名,默認為prefix/logs/error.log?
--http-log-path=path???? 設置HTTP請求日志文件名,默認為prefix/logs/access.log?
--user-name=path???? 設置未授權用戶名,默認為nobody?
--group=name???? 設置組名,默認為未授權用戶名?
--with-select_module??? 編譯或取消編譯利用select()方法的模塊?
--with-poll_module???? 編譯或取消編譯利用poll()方法的模塊?
--without-http_gzip_module??? 取消編譯HTTP服務器壓縮響應的模塊,需要zlip庫?
--without-http_rewrite_module?? 取消編譯HTTP服務器重定向請求或修改請求URI地址的模塊,需要PCRE庫?
--without-http_proxy_module??? 取消編譯HTTP服務器代理模塊?
--with-http_ssl_module??? 編譯添加對HTTPS協議的支持,需要OpenSSL庫?
--with-pcre=path??? 設置PCRE庫的源代碼路徑,下載PCRE源碼后解壓縮到指定的path即可,剩下的交給nginx的./configure和make命令完成?
--with-pcre-jit??? 編譯PCRE庫支持及時編譯?
--with-zlib=path??? 設置zlib庫源代碼的路徑,同樣下載zlib源碼后解壓到指定的path即可?
--with-cc-opt=parameters??? 設置CFLAGS變量的額外參數?
--with-ld-opt=parameters??? 設置鏈接時的額外參數--with-openssl=/usr/include #啟用ssl?
??????? --with-pcre=/usr/include/pcre/ #啟用正規表達式?
??????? --with-http_stub_status_module #安裝可以查看nginx狀態的程序?
??????? --with-http_memcached_module?? #啟用memcache緩存?
??????? --with-http_rewrite_module???? #啟用支持url重寫===================================================================================
./configure \?
??? --user=nginx \?
??? --group=nginx \?
??? --prefix=/usr/share \?
??? --sbin-path=/usr/sbin/nginx \?
??? --conf-path=/etc/nginx/nginx.conf \?
??? --error-log-path=/var/log/nginx/error.log \?
??? --http-log-path=/var/log/nginx/access.log \?
??? --pid-path=/var/log/run/nginx.pid \?
??? --lock-path=/var/log/lock/subsys/nginx \?
??? --with-http_ssl_module \?
??? --with-http_realip_module \?
??? --with-http_addition_module \?
??? --with-http_sub_module \?
??? --with-http_dav_module \?
??? --with-http_flv_module \?
??? --with-http_gzip_static_module \?
??? --with-http_stub_status_module \?
??? --with-http_perl_module \?
??? --with-mail \?
??? --with-mail_ssl_module?
make?
make install?
編譯選項參考: http://wiki.nginx.org/NginxInstallOptions
#iptables配置?
vi /etc/sysconfig/iptables?
#添加配置項?
-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 8080 -j ACCEPT?
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT?
特別提示:正確的應該是添加到默認的22端口這條規則的下面?
#重啟防火墻?
/etc/rc.d/init.d/iptables restart 或 service iptables restart?
#查看現在的防火墻設置?
iptables -L -n 或/etc/init.d/iptables status?
#關閉防火墻?
/etc/init.d/iptables stop
#停止關閉?
#查詢nginx主進程號?
ps -ef | grep nginx?
#停止進程?
kill -QUIT 主進程號?
#快速停止?
kill -TERM 主進程號?
#強制停止?
kill -9 nginx
#Nginx
#端口查看?
netstat –na|grep 80?
#測試配置文件是否有錯誤?
/opt/nginx/sbin/nginx –t?
#重新加載配置?
/opt/nginx/sbin/nginx -s reload?
#啟動nginx?
/opt/nginx/sbin/nginx?
#停止nginx?
/opt/nginx/sbin/nginx –s stop
#Nginx配置
#默認配置
#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; # server_name localhost; # ssl on; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_timeout 5m; # ssl_protocols SSLv2 SSLv3 TLSv1; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
#Tomcat部署springmvc測試項目?
上傳項目到/opt目錄下,新建ROOT.XML放在Catalina/localhost目錄下
<?xml version='1.0' encoding='utf-8'?> <Context crossContext="true" docBase="/opt/springmvc/" path="" reloadable="true"> </Context>
#啟動項目?
cd /opt/apache-tomcat-6.0.36/bin?
./start.sh?
#訪問?
http://192.168.0.108:8080
#修改NG配置
#user nobody; worker_processes2
; error_log logs/error.log; events { use epoll; 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"'; #tcp_nodelay on; sendfile on; keepalive_timeout 65; #啟用gzip gzip on; gzip_min_length 1000; gzip_buffers 4 8k; gzip_http_version 1.1; gzip_types text/plain application/x-javascript text/css application/xml; include proxy.conf; upstream tomcat_server { server 127.0.0.1:8080; } server { listen 80; server_name localhost; index index.html index.htm index.shtml index.jsp; root /opt/springmvc; location ~ \.(html|shtml|jsp|jspx|do|action)?$ { #所有jsp的頁面均交由tomcat處理 proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://tomcat_server; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ #設定訪問靜態文件直接讀取不經過tomcat { expires 30d; } location ~ .*\.(js|css)?$ { expires 1h; } #error_page 404 /404.html; #404頁面 error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location /nginxstatus { stub_status on; access_log on; auth_basic "nginxstatus"; #auth_basic_user_file conf/passwd; } } }
#添加proxy.conf配置
#允許客戶端請求的最大的單個文件字節數 client_max_body_size 10m; #緩沖區代理緩沖用戶端請求的最大字節數 可以理解為先保存到本地再傳給用戶 client_body_buffer_size 128k; #跟后端服務器連接的超時時間_發起握手等候響應超時時間 proxy_connect_timeout 300; #連接成功后_等候后端服務器響應時間_其實已經進入后端的排隊等候處理 proxy_read_timeout 300; #后端服務器數據回傳時間_就是在規定時間內后端服務器必須傳完所有的數據 proxy_send_timeout 300; #代理請求緩沖區_這個緩沖區間會保存用戶的頭信息以供Nginx進行規則處理_一般只要能夠保存下頭信息即可 proxy_buffer_size 4k; #同上 告訴Nginx保存單個用的幾個Buffer 最大用多大空間 proxy_buffers 4 32k; #如果系統忙的時候可以申請更大的proxy_buffers 官方推薦*2 proxy_busy_buffers_size 64k; #proxy 緩存臨時文件的大小 proxy_temp_file_write_size 64k;
#啟動NG與Tomcat
#啟動NG?
/opt/nginx/sbin/nginx?
#檢查是否正確?
/opt/nginx/sbin/nginx?
#重新加載配置?
/opt/nginx/sbin/nginx -s reload?
#停止?
/opt/nginx/sbin/nginx -s stop
#查看
Tomcat:
http://192.168.0.108:8080
?
Nginx:
http://192.168.0.108
?
Nginxstatus:
http://192.168.0.108/nginxstatus
nginx配置參數?
#使用哪個用戶啟動nginx 前面是用戶,后面是組?
user www www;?
#nginx工作的進程數量,一般認為配置值與機器核數相等為佳?
worker_processes 2;?
# [ debug | info | notice | warn | error | crit ] 錯誤日志的位置?
error_log /var/htdocs/logs/nginx_error.log crit;#進程號保存文件?
pid /usr/local/nginx/nginx.pid;#最大文件描述符 建議設置啟動nginx的shell可以打開的最大文件描述符?
#修改/etc/sysctl.conf,增加fs.file-max=6553560,fs.file-max是指系統所有進程一共可以打開的文件數量?
#可以使用ulimit -Hn/-Sn查看該值,可以修改/etc/security/limits.conf,增加以下兩行,*表示對所有用戶有效?
#* soft nofile 65535?
#* hard nofile 65535#運行/sbin/sysctl -p命令,重新登錄shell生效?
worker_rlimit_nofile 65535;events?
{?
# use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];?
use epoll; #使用epoll(linux2.6的高性能方式,了解epoll相關知識和原理可上網絡搜索)?
worker_connections 51200; #每個進程最大連接數(最大連接=連接數x進程數)?
}http?
{?
#文件擴展名與文件類型映射表,具體查看同目錄下的mime.types?
include mime.types;#默認文件類型?
default_type application/octet-stream;#日志文件格式?
log_format main ' r e m o t e a d d r ? remote_user [ t i m e l o c a l ] request '?
'" s t a t u s " body_bytes_sent " h t t p r e f e r e r " ′′ " http_user_agent" "$http_x_forwarded_for"';log_format download ' r e m o t e a d d r ? remote_user [ t i m e l o c a l ] ′′ " request"? s t a t u s bytes_sent '?
'" h t t p r e f e r e r "" http_user_agent" '?
'" h t t p r a n g e "" sent_http_content_range"';#默認編碼?
charset gb2312,utf-8;server_names_hash_bucket_size 128;?
#開啟高效文件傳輸模式?
sendfile on;?
#以下兩個選項用于防止網絡阻塞 參考http://i.cn.yahoo.com/nesta2001zhang/blog/p_104/?
tcp_nopush on;?
tcp_nodelay on;#長鏈接超時時間?
keepalive_timeout 300;#fastcgi連接超時時間,下面的看字面意思都能理解個大概了,就不解釋了.?
fastcgi_connect_timeout 300;?
fastcgi_send_timeout 300;?
fastcgi_read_timeout 300;?
fastcgi_buffer_size 128k;?
fastcgi_buffers 4 256k;?
fastcgi_busy_buffers_size 256k;?
fastcgi_temp_file_write_size 256k;?
fastcgi_temp_path /dev/shm;#打開gzip壓縮?
gzip on;?
#最小壓縮文件大小,一般大于2k的文件使用gzip能提升效率,否則很小的文件壓縮反倒更消耗服務器性能?
gzip_min_length 2k;?
#壓縮緩沖區?
gzip_buffers 48k;?
#壓縮版本(默認1.1,前端為squid2.5使用1.0)?
gzip_http_version 1.1;?
#壓縮類型,默認就已經包含text/html 所以下面就不用再寫了,當然寫上去的話,也不會有問題,但是會有一個warn?
gzip_types text/plain application/x-javascript text/css text/html text/javascript application/xml;?
#錯誤頁面?
error_page 404 /404.html;?
error_page 403 /404.html;?
#上傳文件大小限制?
client_max_body_size 20m;?
#設定請求頭緩存,如果請求header過大,會使用large_client_header_buffers 來讀取?
client_header_buffer_size 16k;?
large_client_header_buffers 464k;?
#設定負載均衡的服務器列表?
upstream mysvr {?
#weigth參數表示權值,權值越高被分配到的幾率越大?
#本機上的Squid開啟3128端口?
server localhost:8080? weight=5;?
server 127.0.0.1:8080?? weight=1;?
}?
#下面開始虛擬主機的配置?
server?
{?
listen 80;?
server_name localhost;#設定本虛擬主機的訪問日志?
access_log logs/access.log main;#如果訪問 /img/*, /js/*, /css/* 資源,則直接取本地文件,不通過squid?
#如果這些文件較多,不推薦這種方式,因為通過squid的緩存效果更好?
location ~ ^/(images|javascript|js|css|flash|media|static)/? {?
root #應用的根目錄;#刷新時間,根據靜態文件修改的頻度來調整,開發測試階段可以短一些,生產階段可以長一些?
expires 24h;?
}#對 "/" 啟用?
location / {# http://后面跟upstream ?的名字?
proxy_pass? http://my server?;?
proxy_redirect off;?
proxy_set_header Host? h o s t ; p r o x y s e t h e a d e r X ? R e a l ? I P remote_addr;?
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;?
client_max_body_size 10m
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
