1.安装nginx依赖的库
包括:GCC,PCRE,zlib,OpenSSL,执行下面的命令一次性安装即可
yum -y install gcc gcc-c++ autoconf automake
yum install -y gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
2. 下载nginx安装包,其他版本都可以
wget http://nginx.org/download/nginx-1.21.4.tar.gz
解压文件:
tar -zxvf nginx-1.21.4.tar.gz
3.进入解压的nginx资源文件目录下,配置 configure
cd nginx-1.21.4 ./configure --with-stream make && make install./configure --prefix=/opt/nginx --sbin-path=/opt/nginx/sbin/nginx --conf-path=/opt/nginx/conf/nginx.conf --with-http_stub_status_module --with-http_gzip_static_module --with-stream make && make install
4.在usr/local/nginx/conf配置nginx.conf文件
nginx.conf文件:
在nginx.conf里http节点里放置以下配置,能达到服务负载均衡,nginx本身服务器端口9206能均衡到 192.168.110.138:9206,server 192.168.110.139:9206;可以加上权重和检测机制等,这里不再赘述:
upstream driverServer {
server 192.168.110.138:9206;
#server 192.168.110.139:9206;
#server 192.168.110.140:9206;
}
server {
listen 9206;
location / {
proxy_pass http://driverServer;
}
}
在nginx.conf最外层加上如下配置,可以达到tcp通信均衡,nginx中服务端口3000,可以均衡到192.168.110.138:3000
stream {upstream tcpServer {
server 192.168.110.138:3000 max_fails=5 fail_timeout=30s;
#server 192.168.110.139:8123;
#server 192.168.110.140:8123;
}server {
listen 3000;
proxy_connect_timeout 10s;
proxy_timeout 30s;
proxy_pass tcpServer;
}server {
listen 9777;
proxy_pass udpserver ;
}
upstream udpserver {server 192.168.110.116:8089 weight=1;
server 192.168.110.117:8089 weight=2;
}
5.配置修改完后,进行nginx -t 操作生效,并在/usr/local/nginx/sbin里启动nginx启动命令如下:
./nginx
重启命令:./nginx -s reload
强制关闭:./nginx -s stop
到此nginx安装配置完毕!
6.最后配置开启自启动:
在/etc/init.d中增加nginx文件命令如下:
touch nginx
在nginx中:增加
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid# Source function library.
. /etc/rc.d/init.d/functions# Source networking configuration.
. /etc/sysconfig/network# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0nginx="/usr/local/nginx/sbin/nginx" #改成自己系统路径
prog=$(basename $nginx)NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf" #改成自己系统路径
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=.*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}restart() {
configtest || return $?
stop
sleep 1
start
}reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}force_reload() {
restart
}configtest() {
$nginx -t -c $NGINX_CONF_FILE
}rh_status() {
status $prog
}rh_status_q() {
rh_status >/dev/null 2>&1
}case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
按esc退出键输入:wq!保存
修改/etc/init.d/nginx文件权限:
chmod 77 nginx
设置开机自启(需要重启)
chkconfig --add /etc/init.d/nginx #将nginx服务加入chkconfig管理列表
chkconfig nginx on #设置开机自动启动
想关闭开机自启,使用命令:chkconfig nginx off
至此nginx开机自启动配置完成!
7.nginx常用服务命令
service nginx start #开启服务
service nginx stop #结束服务
service nginx restart #重启服务
service nginx reload #重载配置文件
service nginx status #查看状态
最后,如果centos服务器开启了防火墙,需要放开相应的通信端口!!!
查看防火墙所有开放的端口
firewall-cmd --zone=public --list-ports