linux Nginx发布基于PHP的WEB

实验环境约定

  • 系统:centos7.7
  • 防⽕墙:关闭
  • Selinux:关闭
  • 服务器地址:192.168.31.199/24

一. 安装Nginx

1. 安装依赖工具

yum -y install epel-release make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

2. 安装PCRE

# PCRE 作用是让 Nginx 支持 Rewrite 功能。
[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
[root@localhost src]# tar zxvf pcre-8.35.tar.gz
[root@localhost src]# cd pcre-8.35
[root@localhost pcre-8.35]# ./configure
[root@localhost pcre-8.35]# make && make install
[root@localhost pcre-8.35]# pcre-config --version        #查看PCRE版本

3. 安装nginx

[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# wget https://nginx.org/download/nginx-1.16.1.tar.gz
[root@localhost src]# tar -zxvf nginx-1.16.1.tar.gz
[root@localhost src]# cd nginx-1.16.1/
[root@localhost nginx-1.16.1]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35
[root@localhost nginx-1.16.1]# make && make install
[root@localhost nginx-1.16.1]# /usr/local/nginx/sbin/nginx -v  #查看Nginx版本

二. 安装mysql

1. 安装依赖包

yum -y install ncurses-devel gcc-* bzip2-*

2. 升级cmake⼯具

1)获得软件
https://cmake.org/download/

2)安装软件
cd /usr/local/src/
wget https://cmake.org/files/v3.6/cmake-3.6.0-rc1.tar.gz
tar xf cmake-3.6.0-rc1.tar
cd cmake-3.6.0-rc1
./configure
make
make install

3. 升级boost库⽂件

1) 软件获得
https://www.boost.org/  请选择1.5.9版本
下载链接如下:
wget https://jaist.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz

2)软件安装
mkdir /usr/local/boost
tar -zxvf boost_1_59_0.tar.gz -C /usr/local/boost/
# 该软件为绿色版本,直接解压到/usr/local/boost/目录下即可
# boost 1.5.9版本配合的mysql是5.7;

4. 安装mysql

1)软件获得
cd /usr/local/src
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.24.tar.gz

2)软件安装
tar -zxvf mysql-5.7.24.tar.gz
cd mysql-5.7.24
useradd -s /sbin/nologin -r mysql

cmake . \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_DEBUG=0 \
-DWITH_EMBEDDED_SERVER=1 \
-DDOWNLOAD_BOOST=1 \
-DENABLE_DOWNLOADS=1 \
-DWITH_BOOST=/usr/local/boost



# 以上cmake参数解释
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql	#指定安装路径
-DMYSQL_DATADIR=/usr/local/mysql/data	#指定存放数据⽂件的⽬录位置
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock	#指定mysql.sock的路径
-DWITH_INNOBASE_STORAGE_ENGINE=1 #安装innodb存储引擎
-DWITH_MYISAM_STORAGE_ENGINE=1 #安装myisam存储引擎
-DENABLED_LOCAL_INFILE=1 #允许客户端使⽤local data local 导⼊本地数据⽂件
-DEXTRA_CHARSETS=all #安装所有字符集
-DDEFAULT_CHARSET=utf8 #默认字符集utf-8
-DDEFAULT_COLLATION=utf8_general_ci #校验字符
-DWITH_DEBUG=0	#关闭debug
-DWITH_EMBEDDED_SERVER=1 #⽣成⼀个libmysqld.a(.so)的库,这个库同时集成了mysql服务与客户端API
-DDOWNLOAD_BOOST=1 #允许boost,
-DENABLE_DOWNLOADS=1 #允许下载boost库文件
-DWITH_BOOST=/usr/local/boost #指定boost文件库位置

# 如果需要重新make需要删除/usr/local/src/mysql-5.7.24目录下的CMakeCache.txt,然后再重新cmake;

make -j4  #使用4个cpu内核一起进行编译
make install

3)安装后操作
cp support-files/mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/mysql
chown mysql.mysql /usr/local/mysql/ -R
ln -sf /usr/local/mysql/bin/* /usr/bin/
ln -sf /usr/local/mysql/lib/* /usr/lib/
ln -sf /usr/local/mysql/libexec/* /usr/local/libexec
ln -sf /usr/local/mysql/share/man/man1/* /usr/share/man/man1
ln -sf /usr/local/mysql/share/man/man8/* /usr/share/man/man8

#初始化数据库,并记录临时密码;
/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/

#重要的事情说三遍:
#记录临时密码;root@localhost: ymH?C_fpn8;5
#记录临时密码;root@localhost: ymH?C_fpn8;5
#记录临时密码;root@localhost: ymH?C_fpn8;5
原日志:2020-04-11T17:19:34.310247Z 1 [Note] A temporary password is generated for root@localhost: ymH?C_fpn8;5

#修改配置文件
vim /etc/my.cnf
#确保所有参数的路径正确
egrep -v "^#|^$" /etc/my.cnf
[mysqld]
basedir=/usr/local/mysq
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/mysql.sock
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mysql.log
pid-file=/var/run/mysql.pid
!includedir /etc/my.cnf.d

5. 启动测试

/etc/init.d/mysql start    #启动数据库
mysql_secure_installation    #修改密码
mysql -u root -p    #登录数据库

三. 安装PHP

#依赖包安装
yum install gcc-c++ libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel gd net-snmp-*

#软件包获取
[root@localhost src]# cd /usr/local/src
[root@localhost src]# wget http://jp2.php.net/distributions/php-7.2.11.tar.bz2

#配置
[root@localhost src]# cd php-7.2.11
[root@localhost php-7.2.11]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysqli=mysqlnd --enable-pdo --with-pdo-mysql=mysqlnd --with-iconv-dir=/usr/local/ --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-pcre-regex --with-zlib --with-bz2 --enable-calendar --disable-phar --with-curl --enable-dba --with-libxml-dir --enable-ftp --with-gd --with-jpeg-dir --with-png-dir --with-zlib-dir --with-freetype-dir --enable-gd-jis-conv --with-mhash --enable-mbstring  --enable-opcache=yes --enable-pcntl --enable-xml --disable-rpath --enable-shmop --enable-sockets --enable-zip --enable-bcmath --with-snmp --disable-ipv6 --with-gettext --disable-rpath --disable-debug  --enable-embedded-mysqli --with-mysql-sock=/usr/local/mysql/ 

#编译
[root@localhost php-7.2.11]#  make  ZEND_EXTRA_LIBS='-liconv'

#安装
[root@localhost php-7.2.11]# make install

#拷贝配置文件
[root@web01 php-7.2.11]# cp /usr/local/src/php-7.2.11/php.ini-production /usr/local/php/etc/php.ini
配置参数说明
--prefix=/usr/local/php 
--with-config-file-path=/usr/local/php/etc 
--with-mysqli=mysqlnd --enable-embedded-mysqli --with-mysql-sock=/usr/local/mysql/ 
--enable-pdo --with-pdo-mysql=mysqlnd  
--with-iconv-dir=/usr/local/ 
--enable-fpm --with-fpm-user=www --with-fpm-group=www 
--with-pcre-regex 
--with-zlib --with-bz2  --with-zlib-dir  --enable-zip
--enable-calendar 
--disable-phar 
--with-curl 
--enable-dba 
--with-libxml-dir 
--enable-ftp 
--with-gd --with-jpeg-dir --with-png-dir  
--with-freetype-dir 
--enable-gd-jis-conv 
--with-mhash --enable-mbstring  
--enable-opcache=yes 
--enable-pcntl 
--enable-xml 
--disable-rpath 
--enable-shmop 
--enable-sockets  
--enable-bcmath 
--with-snmp 
--disable-ipv6 
--with-gettext 
--disable-rpath 
--disable-debug  

四. PHP-FPM设置

这个PHP-FPM是将Nginx和PHP的桥梁软件(这个软件使用的是Fast-CGI协议)

#php-fpm服务设置  pool
每个pool可以监听不同的sock、tcp/ip,pool池是支持定义多个pool的。所有的网站使用同一个池,那其中一个网站发生一些故障,比如程序员写的一些程序有问题,就会把php资源耗尽,这样的结果就是其他站点的php也会502。所以有必要把每一个站点隔离开。每个pool的名字要唯一。

cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
egrep -v "^;|^$" /usr/local/php/etc/php-fpm.d/www.conf
[www]
user = www
group = www
listen = 127.0.0.1:9000
listen.owner = www
listen.group = www
listen.mode = 0660
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

注释
# 全局配置
[global]
# PID、可以不填
pid = /usr/local/php/var/run/php-fpm.pid
# 错误日志路径、可以不填
error_log = /usr/local/php/var/log/php-fpm.log
# www虚拟主机配置、可写多个
# pool命名:www
[www]
# 监听socket方式 
# 可以写成listen = 127.0.0.1:9000
listen = /tmp/php-fcgi.sock
# 开启php-fpm的执行用户
user = www
# 开启php-fpm的所属组
group = www
# 监听listen的用户,和后面的nginx的一致
listen.owner = nobody
# 监听listen的组,和后面的nginx的一致
listen.group = nobody
# 怎样的形式启用进程
pm = dynamic
# 最大开启子进程数
pm.max_children = 50
# 一开始启动多少子进程
pm.start_servers = 20
# 空闲时保留多少个子进程
pm.min_spare_servers = 5
# 最多空闲子进程
pm.max_spare_servers = 35
# 进程处理多少个请求之后销毁重建
pm.max_requests = 500
# 限定打开最大的文件数
rlimit_files = 1024

#PHP-FPM的主配置文件
egrep -v "^;|^$" /usr/local/php/etc/php-fpm.conf
[global]
pid = run/php-fpm.pid
error_log = log/php-fpm.log
daemonize = yes
include = /usr/local/php/etc/php-fpm.d/*.conf
useradd -s /sbin/nologin -r www
cp /usr/src/php-7.2.12/sapi/fpm/php-fpm.service /etc/systemd/system/
systemctl daemon-reload
systemctl restart php-fpm.service

五. 配置Nginx并初步测试发布环境

# vim /usr/local/nginx/conf/nginx.conf
user  www;     #修改下运行用户

/usr/local/nginx/sbin/nginx   #启动Nginx
echo "<?php phpinfo(); ?>" > /usr/local/nginx/html/phpinfo.php

#测试
http://192.168.31.199/phpinfo.php

六. 发布WordPress

cd /usr/local/src
wget https://wordpress.org/latest.tar.gz
tar xf latest.tar.gz 
cd /usr/local/src/wordpress
mv * /usr/local/nginx/html/
chown www.www /usr/local/nginx/html/ -R
# vim /usr/local/nginx/conf/nginx.conf
user  www;     #修改下运行用户

#Server部分的关键配置
location / {
            root   html;
            index  index.php index.html index.htm;   #第一个位置增加php页面
        }

location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;  #这里的路径修改下
            include        fastcgi_params;
        }
killall nginx
/usr/local/nginx/sbin/nginx
新建数据库  wordpress
[root@www html]# mysql -u root -p123321
mysql> create database wordpress;
Query OK, 1 row affected (0.01 sec)
mysql> exit
访问192.168.31.199就可以开始配置站点了。

本片博文是学习白树明老师的Nginx课程后根据白老师提供的笔记做实验后进行编写的,感谢白老师。

posted @ 2020-04-15 17:58  IamJet  阅读(587)  评论(0编辑  收藏  举报