1. 配置LNMP,并部署php应用。
1)、下载php-fpm源码包
[root@localhost ~]# wget -c https://www.php.net/distributions/php-7.4.27.tar.xz
2)、安装编译时的依赖包
yum install -y gcc gcc-c++ make zlib zlib-devel pcre pcre-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers
3)、解压
[root@localhost ~]# tar xf php-7.4.27.tar.xz -C /usr/local/src/
[root@localhost ~]# cd /usr/local/src/php-7.4.27/
4)、配置:
[root@localhost ~]# ./configure --prefix=/usr/local/php --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl --with-zlib --with-bz2 --with-mhash --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-gd-native-ttf --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm --enable-xml --enable-zip --enable-mysqlnd
报错1:
configure: error: Package requirements (sqlite3 > 3.7.4) were not met:
解决:
[root@localhost php-7.4.27]# yum install sqlite-devel -y
报错2:
configure: error: Package requirements (oniguruma) were not met:
解决:
[root@localhost php-7.4.27]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@localhost php-7.4.27]# yum install oniguruma-devel -y
报错3:
configure: error: Package requirements (libxslt >= 1.1.0) were not met:
解决:
[root@localhost php-7.4.27]# yum install libxslt-devel -y
4)、编译
[root@localhost php-7.4.27]# make
[root@localhost php-7.4.27]# make install
5)、配置文件
[root@localhost php-7.4.27]# cp php.ini-development /usr/local/php/lib/php.ini
[root@localhost php-7.4.27]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@localhost php-7.4.27]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
[root@localhost php-7.4.27]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-7.4.27]# vim /etc/init.d/php-fpm
#chkconfig: 35 80 20
[root@localhost php-7.4.27]# chkconfig --add php-fpm
[root@localhost php-7.4.27]# chkconfig php-fpm on
[root@localhost php-7.4.27]# vim /usr/local/php/lib/php.ini //在data模块下设置时区
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone =Asia/Shanghai
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
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;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
6)、安装mariadb数据库并初始化
[root@localhost ~]# yum install -y mariadb mariadb-server
[root@localhost ~]# systemctl start mariadb
[root@localhost ~]# mysql_secure_installation //初始化数据库并设置密码
[root@localhost ~]# mysql -uroot -p123456
7)、 测试mysql和php协同
[root@localhost ~]# vim /usr/local/nginx/html/mysql.php
[root@localhost ~]# more /usr/local/nginx/html/mysql.php
<?php
$con = mysqli_connect("127.0.0.1","root","123456");
if (!$con) {
die("could not connect to the db:\n" . mysql_error());
}
else { echo "success"; }
mysqli_close($con);
?>
[root@localhost ~]# vim /usr/local/nginx/html/phpinfo.php
[root@localhost ~]# more /usr/local/nginx/html/phpinfo.php
<?php
phpinfo();
?>
8)、重启服务
[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# systemctl restart php-fpm
部署PHP应用
停止之前的源码安装的服务脚本
[root@localhost ~]# systemctl stop php-fpm
[root@localhost ~]# chkconfig --del php-fpm
1)、下载php
[root@localhost ~]# yum install php php-fpm php-gd php-mysql -y
[root@localhost ~]# vim /etc/php.ini
[Date]
date.timezone =Asia/Shanghai
[root@localhost ~]# systemctl restart nginx php-fpm
2)、上传论坛包
[root@localhost ~]# ls Discuz_X3.4_SC_UTF8_0101.zip
Discuz_X3.4_SC_UTF8_0101.zip
[root@localhost ~]# unzip Discuz_X3.4_SC_UTF8_0101.zip
[root@localhost ~]# cd dir_SC_UTF8/
[root@localhost dir_SC_UTF8]# mv upload/ /usr/local/nginx/html/bbs
3)、访问
4)、修改权限
[root@localhost ~]# chmod -R 777 /usr/local/nginx/html/bbs
5)、下一步
6)、下一步
7)、指定用户密码与管理员密码
8)、登录论坛
2. 配置nginx代理局域网上网。
1)、代理服务器端添加一块仅主机模式的网卡
2)、配置
[root@localhost ~]# cd /usr/local/nginx/conf.d
[root@localhost conf.d]# vim proxy1.conf
server {
resolver 114.114.114.114; #指定DNS服务器IP地址
listen 8080;
location / {
proxy_pass http://$http_host$request_uri; #设定代理服务器的协议和地址
}
}
[root@localhost conf.d]# systemctl restart nginx
3. 配置nginx反向代理。
环境准备
主负载:外网地址:192.168.159.133 内网地址:192.168.184.135
副负载:内网:192.168.159.184.136
测试:192.168.159.136
1)、副负载网卡设置为仅主机模式模拟内网服务器,网关为主负载的内网地址
nmcli con mod ens33 ipv4.address 192.168.184.136/24 ipv4.gateway 192.168.184.135
nmcli con up ens33
[root@localhost ~]# echo "反向代理测试页面" > /var/www/html/index.html
[root@localhost ~]# systemctl start httpd
2)、主负载配置
[root@localhost conf.d]# vim proxy2.conf
server {
listen 888;
server_name localhost;
location / {
proxy_pass http://192.168.184.136;
}
}
[root@localhost conf.d]# systemctl restart nginx
3)、另外台虚拟机测试
[root@rs2 ~]# curl 192.168.159.133:888
反向代理测试页面
4. 配置nginx负载均衡。
环境准备
准备四台虚拟机
主负载:外网地址:192.168.159.133 内网地址:192.168.184.135
web1:192.168.159.136
web2:192.168.159.138
测试:192.168.159.140
1)、web1 ,web2安装配置nginx
[root@rs1 ~]# yum install nginx -y
#配置虚拟主机
[root@rs1 conf.d]# vim vhost.conf
server {
listen 80;
server_name bbs.yunjisuan.com;
location / {
root /usr/share/nginx/html/bbs;
index index.html index.htm;
proxy_set_header Host $host;
}
access_log /usr/share/nginx/html/bbs/logs/access_bbs.log main;
}
server {
listen 80;
server_name www.yunjisuan.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
proxy_set_header Host $host;
}
access_log /usr/share/nginx/html/www/logs/access_www.log main;
}
[root@rs1 conf.d]# mkdir /usr/share/nginx/html/{bbs,www}/logs -p
[root@rs2 ~]# yum install nginx -y
#配置虚拟主机
[root@rs2 conf.d]# vim vhost.conf
server {
listen 80;
server_name bbs.yunjisuan.com;
location / {
root /usr/share/nginx/html/bbs;
index index.html index.htm;
proxy_set_header Host $host;
}
access_log /usr/share/nginx/html/bbs/logs/access_bbs.log main;
}
server {
listen 80;
server_name www.yunjisuan.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
proxy_set_header Host $host;
}
access_log /usr/share/nginx/html/www/logs/access_www.log main;
}
[root@rs2 conf.d]# mkdir /usr/share/nginx/html/{bbs,www}/logs -p
2)、在两台web服务端设置主页
#web1
[root@rs1 conf.d]# echo "`hostname -I `www" > /usr/share/nginx/html/www/index.html
[root@rs1 conf.d]# echo "`hostname -I `bbs" > /usr/share/nginx/html/bbs/index.html
#web2
[root@rs2 conf.d]# echo "`hostname -I `www" > /usr/share/nginx/html/www/index.html
[root@rs2 conf.d]# echo "`hostname -I `bbs" > /usr/share/nginx/html/bbs/index.html
#启动服务
[root@rs1 conf.d]# systemctl start nginx
[root@rs2 conf.d]# systemctl start nginx
3)、测试
[root@localhost ~]# curl -H host:bbs.yunjisuan.com 192.168.159.136
192.168.159.136 bbs
[root@localhost ~]# curl -H host:bbs.yunjisuan.com 192.168.159.138
192.168.159.138 bbs
[root@localhost ~]# curl -H host:www.yunjisuan.com 192.168.159.136
192.168.159.136 www
[root@localhost ~]# curl -H host:www.yunjisuan.com 192.168.159.138
192.168.159.138 www
4)、主负载配置
[root@localhost conf.d]# vim lb.conf
upstream www_server_pools {
server 192.168.159.136:80 weight=1;
server 192.168.159.138:80 weight=1;
}
server {
listen 80;
#server_name localhost;
server_name www.yunjisuan.com;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
# root /usr/share/nginx/html;
# index index.html index.htm;
# 访问www.yunjisuan.com,请求发送给www_server_pools里面的节点
proxy_pass http://www_server_pools;
proxy_set_header Host $host;
}
}
[root@localhost conf.d]# systemctl restart nginx
5)、测试段端配置hosts解析
[root@localhost ~]# vim /etc/hosts
192.168.159.133 www.yunjisuan.com bbs.yunjisuan.com
测试:
[root@localhost ~]# for ((i=1;i<=4;i++)); do curl http://bbs.yunjisuan.com; done
192.168.159.136 172.17.0.1 bbs
192.168.159.138 172.17.0.1 bbs
192.168.159.136 172.17.0.1 bbs
192.168.159.138 172.17.0.1 bbs
[root@localhost ~]# for ((i=1;i<=4;i++)); do curl http://www.yunjisuan.com; done
192.168.159.136 172.17.0.1 www
192.168.159.138 172.17.0.1 www
192.168.159.136 172.17.0.1 www
192.168.159.138 172.17.0.1 www