CentOS下安裝和配置MySQL-JDK-Tomcat-Nginx(我的官網環境搭建手冊)

今天,從新弄個人我的雲主機的環境,準備運營本身用Java寫的我的官網等網站。

服務器環境:阿里雲CentOS 6.4位
包括如下腳本在內的絕大部分命令和腳本,都是我親自執行過,靠譜的。
完整的「運營運維」經驗,請參考個人CSDN博客-運營運維 分類: http://blog.csdn.net/FansUnion/article/category/1714547

1.mysql
1.1 安裝mysql
yum install mysql-server

1.2 啓動mysql,服務名字是「mysqld」而不是「mysql」
service mysqld start
service mysqld stop


1.3 設置密碼,刪除匿名用戶,是否容許遠程登陸,刪除test數據庫,從新加載權限表以確保剛剛的設置生效
/usr/bin/mysql_secure_installation

1.4容許root用戶遠程鏈接數據庫
 mysql -uroot -p;
 use mysql;
 select host,user,password from user;
 update user set host = '%' where user = 'root';
 
 #若是root用戶已經有了"%",會提示下面的錯誤
 " Duplicate entry '%-root' for key 'PRIMARY'"
 
 
 grant all privileges  on *.* to root@'%' identified by "root";

 flush privileges; 
 
--執行上面的命令時,不知道到怎麼把密碼給改了,root沒法登陸。 


#1.5 MySQL 忘記口令的解決辦法
若是 MySQL 正在運行,首先殺之: killall -TERM mysqld。 
啓動 MySQL :/usr/bin/mysqld_safe --skip-grant-tables & 
就能夠不須要密碼就進入 MySQL 了。 
而後就是 
>use mysql
>update user set password=password("lw198962") where user="root";
>flush privileges;
從新殺 MySQL ,用正常方法啓動 MySQL 。


---------------------------
1.6 安裝mysql時,給的提示,很是有幫助
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

#設置密碼
/usr/bin/mysqladmin -u root password 'lw198962'
/usr/bin/mysqladmin -u root -h AY1304131823374920ac password 'lw198962'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!

2.java

2.1下載解壓版JDK7,上傳到服務器,而後解壓
 untar -xvf jdk7.tar.gz
 unzip jdk7.zip

2.2配置環境變量
export JAVA_HOME=/home/fans/Fans/jdk1.6.0_31
export CLASSPATH=$JAVA_HOME/lib
export PATH=$JAVA_HOME/bin:$PATH


2.3從新載入
source /etc/profile


3.tomcat

3.1下載解壓版Tomcat7,上傳到服務器,而後解壓
參考 解壓jdk7

3.2 增長可執行權限
chmod a+x *.sh
提示:不要給.bat文件增長x權限
執行tomcat命令的時候,只有x權限的文件,linux才能自動提示,好比輸入 ./start 按Tab 系統自動提示到startup.sh,
若是startup.bat也有權限,須要更詳細的輸入。

3.3 JDK環境變量
Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
At least one of these environment variable is needed to run this program

多是尚未配置環境變量,也多是配置了,尚未生效。
記得執行 source /etc/profile

3.4 啓動Tomcat失敗怎麼辦
./startup.sh這種啓動方式,錯誤提示不夠明顯。
經過 ps -ef|grep tomcat 查看Tomcat是否已經啓動。
若是沒有,經過 ./catalina.sh run 啓動Tomcat,這種方式能夠看到完整的啓動信息。
Windows環境,也是這樣。

這種啓動方式很差的地方是,退出當前會話,Tomcat就中止了。


4.Nginx
  下載地址  http://nginx.org/download/,都是源碼包,沒有二進制安裝包
 
 安裝過程
 yum install nginx-release...
 yum install nginx
 
 
 配置Nginx:核心部分
 
  server {
        listen 80;
        server_name fansunion.cn www.fansunion.cn;
#將www.fansunon.cn永久重定向到fansunion.cn,在創業作ITFriend網站的過程當中發現,帶www和不帶www的Cookie可能不是同一個
#不要www是爲了簡化輸入,讓url更短更容易輸入和記憶
        if ($host !=  'fansunion.cn'){
           rewrite ^/(.*)$  http://fansunion.cn/$1 permanent;
        }

        charset utf-8;
        access_log off;
     
        ssi on;
        ssi_silent_errors on;
           
       location / {
           proxy_pass    http://localhost:8080;
       }
    }   
  後臺Tomcat監聽8080端口,把經過域名「fansunion.cn」過來的全部請求(html、js,靜態和動態的)都轉發到Tomcat解析。
  
  若是靜態資源比較多的狀況下,也可讓Nginx處理js、css、image,只讓Tomcat處理動態的請求。
  我這麼作,是簡化配置,方便維護。
  
  
  
5.參考資料
  Linux環境運維等更多相關資料,請參考個人CSDN博客

  Ubuntu10.04下配置和使用JDK-Mysql-Tomcat-SVN  http://blog.csdn.net/fansunion/article/details/8532104
  Web系統自動化部署腳本  http://blog.csdn.net/fansunion/article/details/40617419
  Ubuntu下SVN服務器安裝和配置   http://blog.csdn.net/fansunion/article/details/16917259
  立博客網站FansUnion.cn運營2年的經驗和教訓以及將來規劃  http://blog.csdn.net/fansunion/article/details/40635731
  
  
  個人我的官方網站(  http://FansUnion.cn/ )正在逐步完善中,歡迎訪問,有建議和問題,我們能夠交流下~多謝

  
  
  小雷FansUnion-博學的互聯網技術工做者
  2014年11月1日
  湖北武漢
相關文章
相關標籤/搜索