配置LNMP并部署php应用;配置nginx代理局域网上网、反向代理、复制均衡
1. 配置LNMP,并部署php应用。
安装LNMP环境所需安装包
[[email protected] ~]# yum install httpd mairadb-server php php-mysql php-gd php-fpm
数据库操作
启动服务
[[email protected] ~]# systemctl enable --now mariadb
设置密码(默认为空密码)
[[email protected] ~]# mysqladmin -uroot password "123456"
编辑数据库配置文件
[[email protected] ~]# vim /etc/my.cnf
添加:character-set-server=utf8
重启服务
[[email protected] ~]# systemctl restart mariadb
编辑nginx配置文件(php-fpm)
[[email protected] ~]# rpm -qc php-fpm
/etc/logrotate.d/php-fpm
/etc/php-fpm.conf
/etc/php-fpm.d/www.conf
/etc/sysconfig/php-fpm
[[email protected] ~]# vim /etc/nginx/conf.d/vhost.conf
添加以下内容:
server {
listen 80;
server_name www.test1.com;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
root /usr/share/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;
}
}
php:修改内容如下
[[email protected] ~]# vim /etc/php.ini
date.timezone = Asia/shanghai
cgi.fix_pathinfo=0
重启nginx服务
[[email protected] ~]# systemctl restart nginx
启动php-fpm服务
[[email protected] ~]# systemctl enable --now php-fpm
定义php主页内容
[[email protected] ~]# echo "<?php phpinfo(); ?>" >/usr/share/nginx/html/test.php
在浏览器访问http://www.test1.com/test.php
部署php应用
[[email protected] ~]# cd /usr/share/nginx/html/
删除测试页面
[[email protected] html]# rm test.php
上传要使用的应用包
[[email protected] html]# ll farm*
-rw-r--r-- 1 root root 22212558 8月 6 2020 farm-ucenter1.5.zip
解压
[[email protected] html]# yum install unzip
[[email protected] html]# unzip farm-ucenter1.5.zip
将应用包中的wwwroot移动到当前目录下
[[email protected] html]# mv upload/ farm
编辑配置文件
[[email protected] html]# vim /etc/nginx/conf.d/vhost.conf
添加index.php的访问权限
index index.php index.html index.htm;
浏览器中访问地址http://www.test1.com/farm/
访问前修改php.ini文件
[[email protected] html]# vim /etc/php.ini
short_open_tag = On
[[email protected] html]# systemctl restart nginx php-fpm
按照步骤进行安装
根据状态更改目录权限
[[email protected] html]# cd farm/
[[email protected] farm]# chmod -R a+w ucenter/ home/ bbs/
[[email protected] farm]# mysql -uroot -p123456
MariaDB [(none)]> create database test1;
MariaDB [(none)]> grant all on test1.* to [email protected] identified by '123';
点击确定,开始安装,成功!
2. 配置nginx代理局域网上网。
修改配置文件
[[email protected] ~]# vim /etc/nginx/conf.d/vhost.conf
添加:
server {
resolver 8.8.8.8;
listen 8080;
location / {
proxy_pass http://$http_host$request_uri;
}
}
重启服务
[[email protected] ~]# systemctl restart nginx
[[email protected] ~]# netstat -lnupt | grep 8080
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 13164/nginx: master
开启路由转发功能
[[email protected] ~]# echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
[[email protected] ~]# sysctl -p
net.ipv4.ip_forward = 1
设置浏览器的局域网设置
3. 配置nginx反向代理。
修改配置文件
[[email protected] ~]# vim /etc/nginx/conf.d/vhost.conf
server {
listen 80;
server_name www.test1.com;
location / {
root /usr/share/nginx/html/test1/;
index index.html index.htm;
}
}
server {
listen 80;
server_name www.test2.com;
location / {
root /usr/share/nginx/html/test2/;
index index.html index.htm;
proxy_pass http://www.test1.com;
}
}
重启服务
[[email protected] ~]# systemctl restart nginx
修改本地hosts文件
[[email protected] ~]# vim /etc/hosts
添加:
192.168.150.11 www.test1.com
修改用于测试访问的主机hosts文件
[[email protected] ~]# vim /etc/hosts
添加:
192.168.150.11 www.test1.com www.test2.com
测试:访问test2网页内容,结果返回test1的访问结果
4. 配置nginx复制均衡。
第一台主机(rpm包安装)
修改配置文件
[[email protected] ~]# vim /etc/nginx/conf.d/vhost.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html/test1/;
index index.html index.htm;
}
}
server {
listen 81;
server_name localhost;
location / {
root /usr/share/nginx/html/test2/;
index index.html index.htm;
}
}
重启服务[[email protected] ~]# systemctl restart nginx
第二台主机(源码安装)
修改配置文件
[[email protected] ~]# vim /usr/local/nginx/conf/nginx.conf
添加:
upstream www_server_pools {
server 192.168.150.11:80 weight=1;
server 192.168.150.11:81 weight=1;
}
修改:
server {
listen 80;
server_name localhost;
location / {
#root html;
#index index.html index.htm;
proxy_pass http://www_server_pools;
}
重启服务[[email protected] ~]# systemctl restart nginx
测试: