(三)Linux環境部署(Centos+Nginx+Tomcat+Mysql)-Nginx環境搭建
以下為本人本地測試實踐中,完整的搭建一個基於Linux系統上Centos+Nginx+Tomcat+Mysql的操作文檔,梳理記錄如下,希望對於那些想要系統地完整搭建Linux環境的程式員們一個系統性的參考。但因大部分都是邊搭建邊記錄下的操作,有所疏漏在所難免,還望見諒;如有問題和意見請幫忙在後面的評論中指出,萬分感謝!另整個搭建的過程中參考了網上許多優秀博主的優秀實踐,在文中末尾或者其他適當的地方都有所標註,如有必要,你可以按參考的連結閱讀參考的原文章。本文檔一共分為6部分,6篇文章,分別是:
1、(一)Linux環境部署(Centos+Nginx+Tomcat+Mysql) - FTP安裝
2、(二)Linux環境部署(Centos+Nginx+Tomcat+Mysql) - 防火牆配置
3、(三)Linux環境部署(Centos+Nginx+Tomcat+Mysql) - Nginx環境搭建
4、(四)Linux環境部署(Centos+Nginx+Tomcat+Mysql) - 安裝Tomcat和JDK 以及 Nginx與Tomcat整合
5、(五)Linux環境部署(Centos+Nginx+Tomcat+Mysql) - Mysql 安裝
6、(六)Linux環境部署(Centos+Nginx+Tomcat+Mysql) - 一些常用命令總結
(三)、Nginx環境搭建
一、 安裝環境包
1、首先使用yum命令安裝、升級所需的程式庫。Nginx的配置及運行需要pcre、zlib、 openssl等軟體包的支援,因此應預先安裝這些軟體的開發包(devel),以便提供相應的庫和標頭檔,確保Nginx的安裝順利完成。在安裝nginx前,需要確保系統安裝了g++、gcc、openssl-devel、pcre-devel和zlib-devel軟體
[root@localhost/]# yum -y install gcc-c++
[root@localhost/]# yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
二、 安裝nginx
Nginx官網:http://nginx.org/
yum方式安裝nginx
1、檢查系統是否安裝的Nginx
[root@localhost/]# find -name nginx
2、安裝nginx的最新穩定版yum源,Nginx最新版yum源在EPEL RPM包中,wget下載安裝即可
[root@localhost~]#cd /root
[root@localhost ~]# wget –Ohttp://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
(如果沒安裝wget小工具,使用如下命令安裝:[root@localhost ~]# yum -y install wget)
[root@localhost~]# rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm //按路徑安裝並顯示進度
3、安裝nginx
[root@localhost ~]#yum installnginx
4、啟動測試nginx
[root@localhost ~]#service nginxrestart
Nginx啟動後有兩個進程,master為主進程,worker為背景工作處理序
在啟動完NGINX後,我們可以在瀏覽器中輸入http://localhost查看welcometo nginx即算成功。
tar方式安裝nginx
上傳或者wgethttp://nginx.org/download/nginx-1.6.3.tar.gz下載一個tar包到/usr/local
先添加執行Nginx的使用者和組(yum不需要這一步)
groupadd nginx
useradd -g nginxnginx -s /bin/false
cd/usr/local/src
tar zxvfnginx-1.6.2.tar.gz
cd nginx-1.6.2
[root@localhostlocal]# tar -zxf nginx-1.6.3.tar.gz
[root@localhostlocal]# cd nginx-1.6.3
[root@localhostnginx-1.6.3]# ./configure--prefix=/usr/local/nginx --user=nginx --group=nginx--with-http_stub_status_module
(相當於:#useradd -s /sbin/nologinnginx
#chown -R nginx: nginx /usr/local/tomcat7)
[root@localhost nginx-1.6.3]#make && make install
[root@localhostsbin]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin建立軟連結
註:配置前可以參考:./configure --help給出說明
--prefix:設定Nginx的安裝目錄
--user和—group:指定Nginx運行使用者和組
--with-http_stub_status_module:啟用http_stub_status_module模組以支援狀態統計
Nginx預設被安裝在/usr/local/nginx
三、nginx啟動、停止
1、檢查設定檔:[root@localhost conf]# nginx -t
與apache的主程式httpd類似,Nginx的主程式也提供了”-t”選項用來對設定檔進行檢查,以便找出不當或錯誤的配置。設定檔nginx.conf預設位於安裝目錄下的conf/子目錄中。若要檢查位於其他位置的設定檔,可使用”-c”選項來指定路徑。
2、啟動、停止nginx:[root@localhost conf]# nginx
直接運行nginx即可啟動Nginx伺服器,這種方式將使用預設的設定檔,若要改用其他設定檔,需添加”-c設定檔路徑”選項來指定路徑。需要注意的是,若伺服器中已安裝有httpd等其他WEB服務軟體,應採取措施避免部突。
[root@localhost conf]# netstat -anpt | grep nginx
[root@localhost ~]# netstat -na | grep 80
如果訪問不到,檢查一下防火牆配置:
配置防火牆
#修改防火牆配置:
[root@uniquenginx]# vi + /etc/sysconfig/iptables
#添加配置項
-AINPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
#重啟防火牆和網路設定
[root@uniquenginx]# service iptables restart
[root@uniquenginx]# /etc/init.d/network restart
這樣nginx的web服務就可以通過80連接埠訪問了
其它:
[root@localhost sbin]# nginx –v (nginx版本號碼)
[root@localhost sbin]# which nginx (看看在哪裡啟動)
/usr/sbin/nginx 啟動
/usr/sbin/nginx -s stop 關掉/停止
[root@unique sbin]# /usr/local/nginx/sbin/nginx -s reload重啟Linux
ps -ef | grep"nginx: master process" | grep -v "grep" | awk -F ' ''{print $2}' 看看主程式號
當Nginx進程運行時,PID號預設存放在logs/目錄下的nginx.pid檔案中,因此停止linux若改用kill命令,也可以根據nginx.pid檔案中的PID號來進行控制。
[root@localhost /]#killall -9 nginx
#查詢nginx主進程號
ps -ef | grepnginx
#停止進程
kill -QUIT 主進程號
#快速停止
kill -TERM 主進程號
#強制停止
pkill -9 nginx
3、配置開機自啟動Nginx
這裡使用的是編寫shell指令碼的方式來處理
vi/etc/init.d/nginx (將建立檔案,輸入下面的代碼):
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# it is v.0.0.2 version.
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog:"
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f/var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog{start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
:wq 儲存並退出
設定檔案的存取權限:
chmod a+x/etc/init.d/nginx (a+x ==> all user can execute 所有使用者可執行)
(chmod 555/etc/init.d/nginx)
這樣在控制台就很容易的操作nginx了:查看Nginx目前狀態、啟動Nginx、停止Nginx、重啟Nginx.
同樣的修改了nginx的設定檔nginx.conf,也可以使用上面的命令重新載入新的設定檔並運行,可以將此命令加入到rc.local檔案中,這樣開機的時候nginx就預設啟動了
vi /etc/rc.local
加入一行 /etc/init.d/nginx start 儲存並退出,下次重啟會生效。
(也可以使用如下方式: chkconfig --addnginx)