目录
文章目录
一、环境准备
1.1 机器准备
ip | 主机名 | 服务 | 作用 |
---|---|---|---|
192.168.2.41 | myhost41 | nginx-1.16.1.tar.gz | 反向代理 |
192.168.2.56 | myhost56 | mysql-5.7.13 | 后端数据库 |
1.2 两台服务器都关闭selinux
1.2.1 myhost41
[root@myhost41 ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@myhost41 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.2.41 netmask 255.255.255.0 broadcast 10.199.41.255
inet6 fe80::215:5dff:fe89:cd43 prefixlen 64 scopeid 0x20<link>
ether 00:15:5d:89:cd:43 txqueuelen 1000 (Ethernet)
RX packets 1365008 bytes 289565779 (276.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 213902 bytes 207232091 (197.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 7772 bytes 435190 (424.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7772 bytes 435190 (424.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@myhost41 ~]# getenforce
Enforcing
[root@myhost41 ~]# setenforce 0
[root@myhost41 ~]# getenforce
Permissive
[root@myhost41 ~]# sed -i "s#enforcing#disabled#g" /etc/sysconfig/selinux
[root@myhost41 ~]# ping www.baidu.com
PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.
64 bytes from 14.215.177.38: icmp_seq=1 ttl=54 time=31.6 ms
64 bytes from 14.215.177.38: icmp_seq=2 ttl=54 time=32.0 ms
64 bytes from 14.215.177.38: icmp_seq=3 ttl=54 time=31.6 ms
64 bytes from 14.215.177.38: icmp_seq=4 ttl=54 time=31.0 ms
1.2.2 myhost56
[root@myhost56 ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@myhost56 ~]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.2.56 netmask 255.255.255.0 broadcast 10.199.41.255
inet6 fe80::215:5dff:fe89:cd43 prefixlen 64 scopeid 0x20<link>
ether 00:15:5d:89:cd:43 txqueuelen 1000 (Ethernet)
RX packets 1365008 bytes 289565779 (276.1 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 213902 bytes 207232091 (197.6 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 7772 bytes 435190 (424.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7772 bytes 435190 (424.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@myhost56 ~]# getenforce
Enforcing
[root@myhost56 ~]# setenforce 0
[root@myhost56 ~]# getenforce
Permissive
[root@myhost56 ~]# sed -i "s#enforcing#disabled#g" /etc/sysconfig/selinux
[root@myhost56 ~]# ping www.baidu.com
PING www.a.shifen.com (14.215.177.38) 56(84) bytes of data.
64 bytes from 14.215.177.38: icmp_seq=1 ttl=54 time=31.6 ms
64 bytes from 14.215.177.38: icmp_seq=2 ttl=54 time=32.0 ms
64 bytes from 14.215.177.38: icmp_seq=3 ttl=54 time=31.6 ms
64 bytes from 14.215.177.38: icmp_seq=4 ttl=54 time=31.0 ms
二、部署
2.1 mysql 安装部署
2.1.1 软件安装约定
安装包存放目录:/home/tools
Mysql安装目录:/usr/local/mysql
数据库保存位置:/usr/local/mysql/data #需要自行建立,并修改属主和属组为mysql:mysql
日志保存位置:/usr/local/mysql/log #需要自行建立,并修改属主和属组为mysql:mysql
2.1.2 下载源码安装包
- MySQL的官网下载地址:http://www.mysql.com/downloads
- MySQL的官网下载地址:https://dev.mysql.com/downloads/mysql
- MySQL版本:MySQL-5.7.13
[root@myhost56 ~]#cd /home/tools
[root@myhost56 tools]# wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz
[root@myhost56 tools]# ll
total 835560
-rw-r--r--. 1 root root 639864682 May 25 2016 mysql-5.7.13-linux-glibc2.5-x86_64.tar.gz
2.1.3 解压安装部署
解压安装部署过程省略,参考之前写两篇,注意安装路径防火墙开放端口等灵活处理。
2.2 nginx 安装部署
2.2.1 安装nginx的依赖库
[root@myhost41 ~]# yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel
2.2.2 创建nginx用户
[root@myhost41 ~]# useradd nginx -s /sbin/nologin -M
[root@myhost41 ~]# id nginx
uid=1008(nginx) gid=1008(nginx) 组=1008(nginx)
2.2.3 解压nginx安装包
[root@myhost41 ~]# cd /home/tools/
[root@myhost41 tools]# ll
-rw-r--r--. 1 root root 1032630 6月 8 2020 nginx-1.16.1.tar.gz
[root@myhost41 tools]# tar xf nginx-1.16.1.tar.gz
[root@myhost41 tools]# ll
drwxr-xr-x. 8 test 1001 4096 8月 13 2019 nginx-1.16.1
-rw-r--r--. 1 root root 1032630 6月 8 2020 nginx-1.16.1.tar.gz
[root@myhost41 tools]# cd nginx-1.16.1
[root@myhost41 nginx-1.16.1]# ll
总用量 756
drwxr-xr-x. 6 test 1001 4096 1月 8 11:02 auto
-rw-r--r--. 1 test 1001 296463 8月 13 2019 CHANGES
-rw-r--r--. 1 test 1001 452171 8月 13 2019 CHANGES.ru
drwxr-xr-x. 2 test 1001 4096 1月 8 11:02 conf
-rwxr-xr-x. 1 test 1001 2502 8月 13 2019 configure
drwxr-xr-x. 4 test 1001 68 1月 8 11:02 contrib
drwxr-xr-x. 2 test 1001 38 1月 8 11:02 html
-rw-r--r--. 1 test 1001 1397 8月 13 2019 LICENSE
drwxr-xr-x. 2 test 1001 20 1月 8 11:02 man
-rw-r--r--. 1 test 1001 49 8月 13 2019 README
drwxr-xr-x. 9 test 1001 84 1月 8 11:02 src
2.2.4 编译安装nginx
注意: --with-stream --with-stream_ssl_module
[root@myhost41 nginx-1.16.1]# cd /usr/local/nginx/conf/
[root@myhost41 conf]# ll
总用量 72
-rw-r--r--. 1 root root 1077 1月 8 11:10 fastcgi.conf
-rw-r--r--. 1 root root 1077 1月 8 11:10 fastcgi.conf.default
-rw-r--r--. 1 root root 1007 1月 8 11:10 fastcgi_params
-rw-r--r--. 1 root root 1007 1月 8 11:10 fastcgi_params.default
-rw-r--r--. 1 root root 2837 1月 8 11:10 koi-utf
-rw-r--r--. 1 root root 2223 1月 8 11:10 koi-win
-rw-r--r--. 1 root root 5231 1月 8 11:10 mime.types
-rw-r--r--. 1 root root 5231 1月 8 11:10 mime.types.default
-rw-r--r--. 1 root root 1156 1月 8 11:16 nginx.conf
-rw-r--r--. 1 root root 515 1月 8 11:13 nginx.conf_bak20210108
-rw-r--r--. 1 root root 2656 1月 8 11:10 nginx.conf.default
-rw-r--r--. 1 root root 636 1月 8 11:10 scgi_params
-rw-r--r--. 1 root root 636 1月 8 11:10 scgi_params.default
-rw-r--r--. 1 root root 664 1月 8 11:10 uwsgi_params
-rw-r--r--. 1 root root 664 1月 8 11:10 uwsgi_params.default
-rw-r--r--. 1 root root 3610 1月 8 11:10 win-utf
[root@myhost41 conf]# vi nginx.conf
[root@myhost41 nginx-1.16.1]# ./configure
--user=nginx --group=nginx \
--prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gzip_static_module \
--with-stream \
--with-stream_ssl_module
[root@myhost nginx-1.16.1]# make && make install
[root@myhost nginx-1.16.1]# echo $?
0
2.2.5 编辑修改nginx配置文件
user nginx;
worker_processes 1;
error_log logs/error.log warn;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
#-----------------往下--------------------
#四层负载,四层的负载不在http模块里面,他是和http模块同级别的
stream {
upstream mysql {
server 192.168.2.56:3306; #后端数据库的ip和端口,如果进行了域名解析,直接写域名就好
}
server {
listen 3306; #如果监听3306,远程登录的时候不用加-p参数
proxy_connect_timeout 10s;
proxy_timeout 30s;
proxy_pass mysql;
}
}
#------------------往上--------------------
http {
include ../conf/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;
}
2.2.6 启动或重启nginx
[root@myhost41 conf]# chown -R nginx:nginx /usr/local/nginx/
[root@myhost41 conf]# /usr/local/nginx/sbin/nginx
[root@myhost41 conf]# /usr/local/nginx/sbin/nginx -s reload
[root@myhost41 conf]# netstat -lntup |grep nginx
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 29234/nginx: master ]
三、连接测试
略