nginx通常被用作后端服务器的反向代理,这样可以很方便的实现动静分离以及均衡负载,从而大大提高服务器的处理能力。
nginx实现动静分离,其实就是在反向代理的时候,如果是静态资源,就直接从nginx发布的路径去获取,而不需要从后台服务器获取了。
但是要注意,这种情况下需要保证后端跟前端的程序保持一致,可以使用Rsync做服务端自动同步或使用NFS、MFS分布式共享存储。
Http Proxy模块,功能很多,最常用的是proxy_pass和proxy_cache
如果要使用proxy_cache,需要集成第三方的ngx_cache_purge模块,用来清除指定URL缓存。这个集成需要在安装nginx的时候去做,使用–add-module
nginx通过upstream模块来实现简单的均衡负载,upstream需要定义在http段内
在upstream段内,定义一个服务器列表,默认的方式轮询,如果要确定同一个访问者发出的请求总是由同一个后端服务器来处理,可以设置ip_hash:
upstream hanao.xyz {
ip_hash;
server 127.0.0.1:8080 weight=5;
server 127.0.0.1 weight=5;
server 127.0.0.1:1111;
}
注意:这个方法本质还是轮询,而且由于客户端的ip可能是不断变化的,比如动态ip,代理,翻墙等。因此ip_hash并不能完全保证用一个客户端总是由同一个服务器来处理。
定义好upstream后,需要在server段内添加如下内容:
server {
location / {
proxy_pass http://hanao.xyz;
}
}
nginx负载均衡配置实现动静分离
主机名 | ip |
---|---|
nginx | 192.168.31.139 |
httpd | 192168.31.138 |
lnmp | 192.168.31.140 |
部署nginx
创建系统用户nginx
[root@czh ~]# useradd -r -M -s /sbin/nologin nginx
安装相关的依赖包
[root@czh ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ vim wget make
创建日志存放目录
[root@czh ~]# mkdir -p /var/log/nginx
[root@czh ~]# chown -R nginx.nginx /var/log/nginx
下载nginx
wget http://nginx.org/download/nginx-1.12.0.tar.gz
[root@czh ~]# ls
anaconda-ks.cfg nginx-1.20.1.tar.gz
编译安装
[root@czh nginx-1.20.1]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-debug \
> --with-http_ssl_module \
> --with-http_realip_module \
> --with-http_image_filter_module \
> --with-http_gunzip_module \
> --with-http_gzip_static_module \
> --with-http_stub_status_module \
> --http-log-path=/var/log/nginx/access.log \
> --error-log-path=/var/log/nginx/error.log
[root@czh nginx-1.20.1]# make install
设置环境变量
[root@czh nginx-1.20.1]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@czh nginx-1.20.1]# . /etc/profile.d/nginx.sh
启动nginx
nginx
配置apache
编译安装httpd-2.4
安装编译器和所需要的工具等
[root@czh ~]# dnf -y install gcc gcc-c++ pcre-devel zlib-devel openssl
openssl-devel make expat-devel libtool
然后下载,依赖包
https://mirrors.bfsu.edu.cn/apache/httpd/httpd-2.4.46.tar.bz2
https://mirrors.bfsu.edu.cn/apache/apr/apr-1.7.0.tar.bz2
https://mirrors.bfsu.edu.cn/apache/apr/apr-util-1.6.1.tar.bz2
进行编译安装
[root@czh ~]# tar xf apr-1.7.0.tar.bz2
[root@czh ~]# ls
anaconda-ks.cfg apr-1.7.0 apr-1.7.0.tar.bz2 apr-util-1.6.1.tar.bz2 httpd-2.4.43.tar.bz2
[root@czh ~]# cd apr-1.7.0/
[root@czh ~]# tar xf apr-util-1.6.1.tar.bz2
[root@czh apr-1.7.0]# vim configure (打开包里面的 configure 文件,将'$cfgfile'这一行注释掉 )
setopt NO_GLOB_SUBST
fi
cfgfile=${ofile}T
trap "$RM \"$cfgfile\"; exit 1" 1 2 15
# $RM "$cfgfile"
然后使用我们开始安装第一个包
[root@czh apr-1.7.0]# ./configure --prefix=/usr/local/apr
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
Configuring APR library
Platform: x86_64-pc-linux-gnu
checking for working mkdir -p... yes
APR Version: 1.7.0
checking for chosen layout... apr
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for a sed that does not truncate output... /usr/bin/sed
Applying APR hints file rules for x86_64-pc-linux-gnu
setting CPPFLAGS to "-DLINUX -D_REENTRANT -D_GNU_SOURCE"
(Default will be unix)
checking whether make sets $(MAKE)... yes
checking how to run the C preprocessor... gcc -E
。。。
#然后make & make install
[root@czh apr-1.7.0]# make & make install
[1] 345325
/bin/sh /root/apr-1.7.0/libtool --silent --mode=compile gcc -g -O2 -pthread -DHAVE_CONFIG_H -DLINUX -D_REENTRANT -D_GNU_SOURCE -I./include -I/root/apr-1.7.0/include/arch/unix -I./include/arch/unix -I/root/apr-1.7.0/include/arch/unix -I/root/apr-1.7.0/include -I/root/apr-1.7.0/include/private -I/root/apr-1.7.0/include/private -o encoding/apr_encode.lo -c encoding/apr_encode.c && touch encoding/apr_encode.lo
make[1]: 进入目录“/root/apr-1.7.0”
/bin/sh /root/apr-1.7.0/libtool --silent --mode=compile gcc -g -O2 -pthread -DHAVE_CONFIG_H -DLINUX -D_REENTRANT -D_GNU_SOURCE -I./include -I/root/apr-1.7.0/include/arch/unix -I./include/arch/unix -I/root/apr-1.7.0/include/arch/unix -I/root/apr-1.7.0/include -I/root/apr-1.7.0/include/private -I/root/apr-1.7.0/include/private -o encoding/apr_encode.lo -c encoding/apr_encode.c && touch encoding/apr_encode.lo
/root/apr-1.7.0/build/mkdir.sh tools
/bin/sh /root/apr-1.7.0/libtool --silent --mode=compile gcc -g -O2 -pthread -DHAVE_CONFIG_H -DLINUX -D_REENTRANT -D_GNU_SOURCE -I./include -I/root/apr-1.7.0/include/arch/unix -I./include/arch/unix -I/root/apr-1.7.0/include/arch/unix -I/root/apr-1.7.0/include -I/root/apr-1.7.0/include/private -I/root/apr-1.7.0/include/private -o tools/gen_test_char.lo -c tools/gen_test_char.c && touch tools/gen_test_char.lo
/root/apr-1.7.0/build/mkdir.sh tools
/bin/sh /root/apr-1.7.0/libtool --silent --mode=compile gcc -g -O2 -pthread -DHAVE_CONFIG_H -DLINUX -D_REENTRANT -D_GNU_SOURCE -I./include -I/root/apr-1.7.0/include/arch/unix -I./include/arch/unix -I/root...
....
#安装第二个
[root@czh ~]# ls
anaconda-ks.cfg apr-1.7.0.tar.bz2 apr-util-1.6.1.tar.bz2
apr-1.7.0 apr-util-1.6.1 httpd-2.4.43.tar.bz2
[root@czh ~]# cd apr-util-1.6.1/
[root@czh apr-util-1.6.1]# ./configure --prefix=/usr/l
lib/ lib64/ libexec/ local/
[root@czh apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
checking for a BSD-compatible install... /usr/bin/install -c
checking for working mkdir -p... yes
APR-util Version: 1.6.1
checking for chosen layout... apr-util
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
Applying apr-util hints file rules for x86_64-pc-linux-gnu
checking for APR... configure: error: the --with-apr parameter is incorrect. It must specify an install prefix, a build directory, or an apr-config file.
make & make install 编译安装
[root@czh apr-util-1.6.1]# make & make install
[1] 64382
/bin/sh /usr/local/apr/build-1/libtool --silent --mode=compile gcc -g -O2 -pthread -DHAVE_CONFIG_H -DLINUX -D_REENTRANT -D_GNU_SOURCE -I/usr/src/apr-util-1.6.1/include -I/usr/src/apr-util-1.6.1/include/private -I/usr/local/apr/include/apr-1 -o buckets/apr_brigade.lo -c buckets/apr_brigade.c && touch buckets/apr_brigade.lo
make[1]: 进入目录“/usr/src/apr-util-1.6.1”
/bin/sh /usr/local/apr/build-1/libtool --silent --mode=compile gcc -g -O2
.........
解压第三个 在进去后
[root@czh src]# tar xf httpd-2.4.43.tar.bz2
[root@czh src]# cd httpd-2.4.43/
[root@czh httpd-2.4.43]# ./configure --prefix=/usr/local/apache \
--sysconfdir=/etc/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork
checking for chosen layout... Apache
checking for working mkdir -p... yes
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
#然后make & make install
[root@czh httpd-2.4.43]# make & make install
[1] 80620
Making all in srclib
Making install in srclib
make[1]: 进入目录“/usr/src/httpd-2.4.43/srclib”
make[1]: 进入目录“/usr/src/httpd-2.4.43/srclib”
make[1]: 离开目录“/usr/src/httpd-2.4.43/srclib”
Making all in os
make[2]: 进入目录“/usr/src/httpd-2.4.43/srclib”
make[1]: 进入目录“/usr/src/httpd-2.4.43/os”
make[2]: 离开目录“/usr/src/httpd-2.4.43/srclib”
make[1]: 离开目录“/usr/src/httpd-2.4.43/srclib”
Making install in os
Making all in unix
make[1]: 进入目录“/usr/src/httpd-2.4.43/os”
....
#启动服务
[root@czh ~]# /usr/local/apache/bin/apachectl start
lnmp的配置
安装nginx
创建系统用户nginx
[root@czh ~]# useradd -r -M -s /sbin/nologin nginx
安装相关的依赖包
[root@czh ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ vim wget make
创建日志存放目录
[root@czh ~]# mkdir -p /var/log/nginx
[root@czh ~]# chown -R nginx.nginx /var/log/nginx
下载nginx
wget http://nginx.org/download/nginx-1.12.0.tar.gz
[root@czh ~]# ls
anaconda-ks.cfg nginx-1.20.1.tar.gz
编译安装
[root@czh nginx-1.20.1]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-debug \
> --with-http_ssl_module \
> --with-http_realip_module \
> --with-http_image_filter_module \
> --with-http_gunzip_module \
> --with-http_gzip_static_module \
> --with-http_stub_status_module \
> --http-log-path=/var/log/nginx/access.log \
> --error-log-path=/var/log/nginx/error.log
[root@czh nginx-1.20.1]# make install
设置环境变量
[root@czh nginx-1.20.1]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@czh nginx-1.20.1]# . /etc/profile.d/nginx.sh
启动
[root@czh ~]# nginx
[root@czh ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
安装mysql
下载依赖
[root@czh ~]# dnf -y install wget make tree ncurses-compat-libs vim ncurses-compat-libs
下载安装包
[root@czh ~]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
--2021-05-05 02:50:16-- https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
正在解析主机 downloads.mysql.com (downloads.mysql.com)... 137.254.60.14
正在连接 downloads.mysql.com (downloads.mysql.com)|137.254.60.14|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 302 Found
位置:https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz [跟随至新的 URL]
--2021-05-05 02:50:17-- https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
正在解析主机 cdn.mysql.com (cdn.mysql.com)... 104.75.165.42
正在连接 cdn.mysql.com (cdn.mysql.com)|104.75.165.42|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:661718255 (631M) [application/x-tar-gz]
正在保存至: “mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz”
mysql-5.7.33-linux- 100%[=================>] 631.06M 3.84MB/s 用时 3m 0s
2021-05-05 02:53:17 (3.51 MB/s) - 已保存 “mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz” [661718255/661718255])
创建新的一个用户
[root@czh ~]# useradd -r -M -s /sbin/nologin mysql
解压到指定的位置上面
[root@czh ~]# tar xf mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
[root@czh ~]# ls
公共 图片 音乐 initial-setup-ks.cfg
模板 文档 桌面 mysql-5.7.33-linux-glibc2.12-x86_64
视频 下载 anaconda-ks.cfg mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
[root@czh ~]# mv mysql-5.7.33-linux-glibc2.12-x86_64 /usr/local/
[root@czh ~]# cd /usr/local/
[root@czh local]# ls
bin games lib libexec sbin src
etc include lib64 mysql-5.7.33-linux-glibc2.12-x86_64 share
修改目录的名字或者添加软连接
[root@czh local]# mv mysql-5.7.33-linux-glibc2.12-x86_64/ mysql
[root@czh local]# ls
bin etc games include lib lib64 libexec mysql sbin share src
修改文件文件的属组和属主
[root@czh local]# ll |grep mysql
drwxr-xr-x. 9 root root 129 5月 5 15:13 mysql
[root@czh local]# chown -R mysql.mysql /usr/local/mysql/
[root@czh local]# ll |grep mysql
drwxr-xr-x. 9 mysql mysql 129 5月 5 15:13 mysql
设置环境变量
[root@czh mysql]# vim /etc/profile.d/mysql.sh
[root@czh mysql]# cat /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
[root@czh mysql]# . /etc/profile.d/mysql.sh
[root@czh mysql]# which mysql
/usr/local/mysql/bin/mysql
创建一个存放数据的目录
[root@czh mysql]# mkdir /opt/mysql_data
[root@czh mysql]# chown -R mysql.mysql /opt/mysql_data/
[root@czh mysql]# ll /opt/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 5月 5 15:25 mysql_data
初始化并会生成初始密码
[root@czh mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/mysql_data/
2021-05-05T07:28:22.085737Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-05-05T07:28:22.383660Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-05-05T07:28:22.441499Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-05-05T07:28:22.502711Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 78d56077-ad73-11eb-8a03-000c29284f0e.
2021-05-05T07:28:22.505086Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-05-05T07:28:23.405193Z 0 [Warning] CA certificate ca.pem is self signed.
2021-05-05T07:28:23.500882Z 1 [Note] A temporary password is generated for root@localhost: (?qFv=aIt1?x
生成配置文件
[root@czh mysql]# cat > /etc/my.cnf <<EOF
> [mysqld]
> basedir = /usr/local/mysql
> datadir = /opt/mysql_data
> socket = /tmp/mysql.sock
> port = 3306
> pid-file = /opt/mysql_data/mysql.pid
> user = mysql
> skip-name-resolve
> EOF
[root@czh ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/mysql_data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/mysql_data/mysql.pid
user = mysql
skip-name-resolve
配置服务启动脚本 ,并且设置开机自动启动,并且启动mysql服务
[root@czh mysql]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@czh mysql]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@czh mysql]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
[root@czh mysql]# head -47 /etc/init.d/mysqld |tail -2
basedir=/usr/local/mysql
datadir=/opt/data
[root@czh mysql]# service mysqld start
Starting MySQL.Logging to '/opt/data/czh.err'.
SUCCESS!
[root@czh mysql]# chkconfig mysqld on
[root@czh mysql]# chkconfig --list
mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@czh mysql]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
安装php
yum -y install php*
service php-fpm start
[root@czh ~]# vim /etc/php-fpm.d/www.conf
//搜索listen = /run/php-fpm/www.sock
进行修改
··········
;listen = /run/php-fpm/www.sock
listen = 0.0.0.0:9000
··········
[root@czh ~]# systemctl restart php-fpm.service
配置nginx
[root@czh html]# vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm index.php; 添加个index.php
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf; //修改这里
}
[root@czh ~]# vim /usr/local/nginx/html/index.php
<?php
phpinfo();
?>
重启服务
[root@czh html]# nginx -s reload
[root@czh html]# systemctl restart php-fpm
配置lnmp的nginx上
[root@lnmp ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /usr/local/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_conf;
}
配置nginx主机的配置
在server上面添加
upstream html {
server 192.168.31.138;
}
upstream php {
server 192.168.31.140;
}
在location下
location / {
proxy_pass http://html; #改成这个
}
把下面的\.php的取消注释
location ~ \.php$ {
proxy_pass http://php; #添加这一行
}
在apache上
[root@httpd ~]# cd /usr/local/apache/htdocs/
[root@httpd htdocs]# echo '1234567' > index.html
添加动态资源
[root@lnmp ~]cd /usr/loacl/nginx/html
[root@lnmp html]# vim index.php
<?php
phpinfo();
?>
再看效果