web集群–配置LNMP并部署PHP应用,配置nginx反向代理
配置LNMP,并部署php应用。
安装epel
安装国内的epel
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
部署LNMP环境
yum install nginx mariadb-server php php-mysql php-gd php-fpm -y
部署LNMP环境
数据库
systemctl enable mariadb --now
mysqladmin -uroot password '123456'
php-fpm
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;
/etc/php.ini增加以下两行。
cgi.fix_pathinfo=0
date.timezone ="Asia/shanghai"
启动服务
systemctl restart nginx mariadb php-fpm
测试协调
测试nginx和php协同
echo "<?php phpinfo(); ?>" >/usr/share/nginx/html/test.php
# 如果 在用浏览器访问ip/phpinfo.php 访问不了下面这个网页
# 请输入下面这个命令,在进行上面的操作
# systemctl restart nginx
测试mysql和php协同
[root@localhost ~]# vim /usr/share/nginx/html/mysql.php
[root@localhost ~]# cat /usr/share/nginx/html/mysql.php
<?php
$con=mysql_connect("localhost","root","123456");
#在这个地方,一定要保证这些地方的设置和你本身的数据库之间的设置要一致
if (!$con) {
die("could not connect to the db:\n" . mysql_error());
}
else { echo "success"; }
mysql_close($con);
删除测试的php文件(极其重要)
rm -f /usr/share/nginx/html/*.php
反向代理配置
安装nginx
yum install nginx-1.10.0-1.el7.ngx.x86_64.rpm
# cd /etc/nginx/conf.d/
# mv default.conf{,.bak}
# cat vhost.conf
server {
listen 80;
server_name bbs.yunjisuan.com;
location / {
root /usr/share/nginx/html/bbs;
index index.html index.htm;
}
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;
}
access_log /usr/share/nginx/html/www/logs/access_www.log main;
}
mkdir -p /usr/share/nginx/html/{www,bbs}/logs
echo "`hostname -I `www" > /usr/share/nginx/html/www/index.html
mkdir -p /usr/share/nginx/html/{www,bbs}/logs
echo "`hostname -I `www" > /usr/share/nginx/html/www/index.html