CentOS7.3(1611) 安装 nginx+php7.2+mysql5.7

-1. 准备工作

-1.1 selinux

# 查看
getenforce
# 临时关闭
# 设置SELinux 成为permissive模式
# setenforce 1 设置SELinux 成为enforcing模式
setenforce 0
# 永久关闭
vi /etc/selinux/config

-1.2 firewalld

centos 7 默认开启的是firewalld防火墙

#停止firewall
systemctl stop firewalld.service 
#禁止firewall开机启动
systemctl disable firewalld.service 
#查看默认防火墙状态(关闭后显示notrunning,开启后显示running)
firewall-cmd --state 

0. yum 源配置(国内服务器)

//must update yum after os installed
yum update yum

//install wget
yum install -y wget

//备份
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
//CentOS 5
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo
//CentOS 6
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
//CentOS 7
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

// 是上述设置生效
yum makecache

1. nginx

  添加nginx yum仓库

vi /etc/yum.repos.d/nginx.repo
//输入内容
[nginx]  
name=nginx repo  
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/  
gpgcheck=0  
enabled=1
// 查看可安装的nginx版本
yum list | grep nginx
CentOS7.3(1611) 安装 nginx+php7.2+mysql5.7_第1张图片
查看nginx版本
#安装nginx
yum install -y nginx

#启动
service nginx start
#停止
service nginx stop
#重启
service nginx restart
# Nginx配置文件位置
/etc/nginx/nginx.conf  

# 添加开机启动
chkconfig nginx on

  nginx到此安装完成,在浏览器输入服务器IP可以访问到nginx的页面,如果无法访问,请检查防火墙配置和nginx进程是否启动。

2. php、php-fpm

2.1 编译源码安装

2.1.1 下载源码

// local src 目录存放需要源码文件
cd /usr/local/src/
// 下载最新版php 7.2
wget http://php.net/distributions/php-7.2.0.tar.gz
tar -xvf php-7.2.0.tar.gz

2.1.2 安装依赖

# configure需要gcc编译
yum install -y gcc \
    zlib  zlib-devel \
    openssl openssl-devel \
    perl perl-devel

mcrypt:

wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
tar zxf libmcrypt-2.5.7.tar.gz
cd libmcrypt-2.5.7
./configure
make && make install

curl:

//先卸载
yum remove curl curl-devel
//https://curl.haxx.se/download/
wget https://curl.haxx.se/download/curl-7.56.0.tar.gz
tar -zxvf curl-7.56.0.tar.gz
cd curl-7.56.0
./configure --prefix=/usr/local/curl --with-ssl --with-zlib
make && make install

//建议安装两次
./configure --prefix=/usr/local
make clean 
make && make install

必备的依赖包安装:

yum -y install \
    gd-devel \
    libjpeg \
    libjpeg-devel \
    libpng \
    libpng-devel \
    libiconv-devel \
    freetype \
    freetype-devel \
    libmcrypt \
    libmcrypt-devel \
    libxml2 \
    libxml2-devel \
    libxslt-devel \
    mhash 

2.1.3 编译php

cd php-7.2.0
//
./configure --prefix=/usr/local/php7 \
    --with-config-file-path=/usr/local/php7/etc \
    --with-curl=/usr/local/curl \
    --disable-ipv6 \
    --with-pdo-mysql \
    --with-openssl \
    --with-openssl-dir \
    --with-pcre-regex \
    --with-kerberos \
    --with-libdir=lib \
    --with-libxml-dir \
    --with-mysqli=shared,mysqlnd \
    --with-pdo-mysql=shared,mysqlnd \
    --with-pdo-sqlite \
    --with-gd \
    --with-iconv \
    --with-zlib \
    --with-xmlrpc \
    --with-xsl \
    --with-pear \
    --with-gettext \
    --with-png-dir \
    --with-jpeg-dir \
    --with-freetype-dir \
    --with-mcrypt \
    --with-mhash \
    --enable-json \
    --enable-mbstring \
    --enable-pdo \
    --enable-mysqlnd \
    --enable-zip \
    --enable-inline-optimization \
    --enable-shared \
    --enable-libxml \
    --enable-xml \
    --enable-bcmath \
    --enable-shmop \
    --enable-sysvsem \
    --enable-mbregex \
    --enable-ftp \
    --enable-gd-native-ttf \
    --enable-pcntl \
    --enable-sockets \
    --enable-soap \
    --enable-session \
    --enable-opcache \
    --enable-fpm \
    --enable-maintainer-zts \
    --enable-fileinfo \
    --enable-gd-jis-conv \
    --with-zlib-dir 
make && make install
// 等待编译安装完成
# 源码目录提供了两个版本的php.ini 
# 
cp /usr/local/src/php-7.2.0/php.ini-production /usr/local/php7/etc/php.ini

// 配置php-fpm
cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
vi /usr/local/php7/etc/php-fpm.conf
# 找到如下几行,确保如下几行前没有”;” 
pid = run/php-fpm.pid 
error_log = log/php-fpm.log 
log_level = notice 
// php7 新增 php-fpm.d 的子配置文件目录
cd /usr/local/php7/etc/php-fpm.d
cp www.conf.default www.conf
vi www.conf
# 找到以下几行,修改成如下所示的数值
listen = 127.0.0.1:9000 
pm = dynamic 
pm.max_children = 50 
pm.start_servers = 20 
pm.min_spare_servers = 5 
pm.max_spare_servers = 35 
pm.max_requests = 500

## pm表示使用哪种方式,有两个值可以选择,
## 就是static(静态)或者dynamic(动态)。
## 在更老一些的版本中,dynamic被称作apache-like。这个要注意看配置文件的说明。
## 下面4个参数的意思分别为:
## pm.max_children:静态方式下开启的php-fpm进程数量。
## pm.start_servers:动态方式下的起始php-fpm进程数量。
## pm.min_spare_servers:动态方式下的最小php-fpm进程数量。
## pm.max_spare_servers:动态方式下的最大php-fpm进程数量。

利用php自带的php-fpm管理工具,可以很方便的start,stop,restart
把管理工具从源码包里放到php7/sbin文件夹里,方便使用

#添加php-fpm到启动项目录
cp /usr/local/src/php-7.2.0/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
#设置php-fpm权限
chmod 755 /etc/init.d/php-fpm
#设置自启动
chkconfig php-fpm on

添加环境变量

echo 'export PATH=/usr/local/php7/bin:/usr/local/php7/sbin:$PATH' >> /etc/profile
source /etc/profile
# 这样就能看到版本信息
php -v
PHP 7.2.0 (cli) (built: Dec 30 2017 19:15:52) ( ZTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies

2.1.4 配置PHP session

cd /usr/local/php7/etc
vi php.ini
#搜索 session.save_path , 去掉前面的 ;
session.save_path = "/var/lib/php/session"  ; #此处假定session 保存在 /var/lib/php/session 目录下
#建立目录
mkdir -p /var/lib/php/session
chmod -R 777 /var/lib/php/session/  #更改目录权限

3. MySql 5.7

下载mysql提供的yum仓库

cd /usr/local
mkdir yumrepo && cd yumrepo
wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
# 安装yum源
yum localinstall mysql57-community-release-el7-8.noarch.rpm
# 检查mysql源是否安装成功
yum repolist enabled | grep "mysql.*-community.*"
检查mysql yum源安装是否成功
# 开始安装
yum install -y \
    mysql-community-server \
    mysql-community-client \
    mysql-community-dever
# 安装成功之后,输入命令启动mysql
service mysqld start
# mysql安装完成之后,在/var/log/mysqld.log文件中给root生成了一个默认密码。
# 通过下面的方式找到root默认密码,然后登录mysql进行修改
grep 'temporary password' /var/log/mysqld.log 
查看默认mysql密码
# 用上述密码登录
mysql -uroot -p
# 修改密码
set password for 'root'@'localhost'=password('MyNewPass4!'); 
# 注意:
# mysql5.7默认安装了密码安全检查插件(validate_password),
# 默认密码检查策略要求密码必须包含:
#     大小写字母、数字和特殊符号,并且长度不能少于8位。
# 否则会提示ERROR 1819 (HY000): 
#     Your password does not satisfy the current policy requirements错误

修改密码策略:

# 在/etc/my.cnf文件添加validate_password_policy配置,指定密码策略
# 选择0(LOW),1(MEDIUM),2(STRONG)其中一种,选择2需要提供密码字典文件
validate_password_policy=0
# 如果不需要密码策略,添加my.cnf文件中添加如下配置禁用即可:
validate_password = off

开机启动

# 
chkconfig mysqld on

4. 配置nginx支持php

cd /etc/nginx/
vi nginx.conf
#打开gzip
gzip    on;

配置测试

cd /etc/nginx/conf.d
vi test.conf

填入以下内容:

server {
    listen       8090;
    server_name  localhost;

    location / {
        ssi on;
        ssi_silent_errors on;
        ssi_types text/shtml;
        root   /mnt/www/test;
        index  index.html index.htm index.php;
        if ( !-e $request_filename ) {
            rewrite ^(.*)$ /index.php?url=$1 last;
        }

    }
    location ~\.php$ {
        root           /mnt/www/test;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    location ~\.(jpg|jpeg|png|js|css) {
        root /mnt/www/test;
        expires 30d;
    }
}

在/mnt/www/test 目录下新建index.php, 输入以下内容:


然后访问服务器的8090端口。如果能显示php信息。则配置完成

5. phpMyAdmin

5.1 安装php扩展mysqli和pdo_mysql

autoconf:

# autoconf 用于编译php扩展
yum install -y autoconf
CentOS7.3(1611) 安装 nginx+php7.2+mysql5.7_第2张图片
php扩展在源码的ext目录下
# 编译 mysqli
cd /usr/local/src/php-7.2.0/ext/mysqli
# 执行这句命令在mysqli目录下生成configure
/usr/local/php7/bin/phpize
# 
./configure \
    --with-php-config=/usr/local/php7/bin/php-config \
    --enable-embedded-mysqli=shared \
    --enable-shared
#
make && make install
# 编译完成之后会在 /usr/local/php7/lib/php/extensions/no-debug-zts-20170718
# 目录下生成*.so 文件
# 编译 pdo_mysql
cd /usr/local/src/php-7.2.0/ext/pdo_mysql
/usr/local/php7/bin/phpize
# php7 pdo只需要连接mysqlnd驱动即可
./configure \
    --with-php-config=/usr/local/php7/bin/php-config \
    --with-pdo-mysql=mysqlnd
make && make install

编译完成之后需要在php.ini 中载入这些extensions

cd /usr/local/php7/etc
vi php.ini
#添加下面内容
extension_dir = "/usr/local/php7/lib/php/extensions/no-debug-zts-20170718"
extension = mysqli.so 
extension = pdo_mysql.so 

5.2 获取并安装phpMyAdmin

#
cd /usr/local/src
wget https://files.phpmyadmin.net/phpMyAdmin/4.7.7/phpMyAdmin-4.7.7-all-languages.tar.gz
mv ***

修改phpMyAdmin配置文件

cd /mnt/www/phpMyAdmin
cp config.sample.inc.php config.inc.php
vi config.inc.php
#这里的内容随意设置,在cookie访问模式下不能为空
$cfg['blowfish_secret'] = 'www.oschina.com';  
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'cookie';
/* Server parameters */
 #重要,这里设置成localhost有可能会出现
$cfg['Servers'][$i]['host'] = '127.0.0.1'; phpMyAdmin无法访问数据库
$cfg['Servers'][$i]['connect_type'] = 'tcp';
$cfg['Servers'][$i]['compress'] = false;
$cfg['Servers'][$i]['AllowNoPassword'] = false;
$cfg['Servers'][$i]['user'] = 'root';
$cfg['Servers'][$i]['password'] = '******';  #这里设置数据库的密码

配置nginx

cd /etc/nginx/conf.d
vi phpmyadmin.conf
server {
    listen       9090;
    server_name  localhost;

    location / {
        ssi on;
        ssi_silent_errors on;
        ssi_types text/shtml;
        root   /mnt/www/phpMyAdmin;
        index  index.html index.htm index.php;
        if ( !-e $request_filename ) {
            rewrite ^(.*)$ /index.php?url=$1 last;
        }

    }
    location ~\.php$ {
        root           /mnt/www/phpMyAdmin;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    location ~\.(jpg|jpeg|png|js|css) {
        root /mnt/www/phpMyAdmin;
        expires 30d;
    }
}

重启 nginx php-fpm mysqld 服务之后,输入9090端口就可以访问了。

6.composer

wget https://dl.laravel-china.org/composer.phar -O /usr/local/bin/composer
chmod a+x /usr/local/bin/composer

ex

zlib openssl 可以yum install zlib zlib-devel openssl openssl-devel 来安装

zlib:

编译安装:
wget http://www.zlib.net/zlib-1.2.11.tar.gz
tar zxf zlib-1.2.11.tar.gz
cd zlib-1.2.11
./configure --prefix=/usr/local/zlib
make && make install
# 更新动态链接库信息
echo "/usr/local/zlib/lib" >> /etc/ld.so.conf 
ldconfig -v

openssl:

wget https://www.openssl.org/source/openssl-1.0.2o.tar.gz
tar zxf openssl-1.0.2o.tar.gz
./config shared
make && make install

你可能感兴趣的:(CentOS7.3(1611) 安装 nginx+php7.2+mysql5.7)