码迷,mamicode.com
首页 > Web开发 > 详细

nginx-php配置动静分离

时间:2018-04-22 14:01:24      阅读:583      评论:0      收藏:0      [点我收藏+]

标签:nginx搭配php完成解析动态页面

实验目的:nginx-php配置动静分离

实验环境:
主机 192.168.88.100 NGINX服务器
主机 192.168.88.102 PHP和MYSQL服务器
注意:想要手工编译必须安装gcc gcc-c++ make 这三个插件
并且关闭防火墙
service firewalld stop
systemctl disable firewalld

http://nginx.org/en/download.html 这里是nginx最新版的下载地址

1.在88.100上手工编译安装nginx服务器

yum -y install \
pcre-devel \
zlib-devel

useradd -M -s /sbin/nologin nginx

将 准备好的nginx源码包上传到linux中并解压编译
tar xzvf nginx-1.13.7.tar.gz -C /usr/src/
cd /usr/src/nginx-1.13.7

./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

make && make install

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ //将管理nginx的命令做个软连接到系统中

nginx -t //配置文件语法检查
nginx //启动服务
killall -1 nginx //安全重启
killall -3 nginx //停止服务

-------制作管理角本-------- //让linux系统的systemctl服务能够识别start restart stop等命令
vi /etc/init.d/nginx
#!/bin/bash

chkconfig: 35 99 20

description: Nginx Service Control Script

PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0

chmod +x /etc/init.d/nginx
chkconfig --add nginx

vi /usr/local/nginx/conf/nginx.conf

user nginx nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html/webphp;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ .php$ {
root /var/www/html/webphp;
fastcgi_pass 192.168.88.102:9000; //将页面中的以php结尾的动态页面交给88.102服务器的9000端口,也就是php-fpm模块来处理
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/webphp$fastcgi_script_name;
include fastcgi_params;
}
}
}

service nginx restart

2.在88.102上手工编译mysql后编译PHP
安装编译所需要的插件
yum -y install \
ncurses \
ncurses-devel \
bison \
cmake

向系统添加mysql用户
useradd -s /sbin/nologin mysql

把上传到服务器上的mysql安装包解压并编译
tar xf mysql-boost-5.7.20.tar.gz
cd mysql-5.7.20/

cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1

make && make install

chown -R mysql.mysql /usr/local/mysql/

在/etc中修改mysql主配置文件
vi /etc/my.cnf

[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock

[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

chown mysql:mysql /etc/my.cnf

设置mysql所需要的环境变量
echo ‘PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH‘ >> /etc/profile
echo ‘export PATH‘ >> /etc/profile
source /etc/profile

cd /usr/local/mysql/

bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data

cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
systemctl daemon-reload
systemctl start mysqld
netstat -anpt | grep 3306

systemctl enable mysqld

mysqladmin -u root -p password "abc123" //给root账号设置密码

mysql -u root -p


3.同样在88.102上手工编译php
以下都是手工编译php所需要的插件
yum -y install \
libjpeg \
libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 \
libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel

将上传到服务器的php源代码包解压并编译安装
tar xjvf php-7.1.10.tar.bz2
cd php-7.1.10
./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--enable-fpm \ //打开php的fpm模块,也是nginx和php的动静分离的核心
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip

make && make install

cp php.ini-development /usr/local/php/lib/php.ini
vi /usr/local/php/lib/php.ini

mysqli.default_socket = /usr/local/mysql/mysql.sock //修改1020行,指定mysql的sock文件
date.timezone = Asia/Shanghai //修改939行,指定时区

/usr/local/php/bin/php -m //验证安装的模块,发现没有显示安装fpm模块,但是fpm模块其实已经安装

-----------配置及优化FPM模块--------
cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf //将模板文件变为配置文件
cd /usr/local/php/etc/php-fpm.d/
cp www.conf.default www.conf //将模板文件变为配置文件
vi www.conf // 修改36行
192.168.88.102:9000

cd /usr/local/php/etc/
vi php-fpm.conf

pid = run/php-fpm.pid //去除注释


/usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini
netstat -anpt | grep 9000

ln -s /usr/local/php/bin/* /usr/local/bin/

ps aux | grep -c "php-fpm" //结果

4 //结果为4

mkdir -p /var/www/html/webphp //这里创建的是在nginx的配置文件上定义的.php文件的根目录
vi /var/www/html/webphp/index.php //编辑测试页面

<?php
phpinfo();
?>
在浏览器里输入nginx服务器的IP即192.168.88.100/index.php 可以访问动态PHP页面

技术分享图片

nginx-php配置动静分离

标签:nginx搭配php完成解析动态页面

原文地址:http://blog.51cto.com/13714942/2106447

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!