1. 程式人生 > >nginx-php配置動靜分離

nginx-php配置動靜分離

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配置動靜分離