(三)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)


相关内容