Centos7系统下Nginx+PHP7+MySQL5.7(LNMP)环境配置

前提:

在本文中,主要介绍从源代码安装Nginx、PHP、Mysql,这篇教程是基于CentOS7 64bit系统来安装的,非Centos系统不适用。现在我们就开始吧!

目录:

1.Nginx安装配置
2.PHP7安装配置
3.MySQL5.7安装配置

1、Nginx安装配置

1.1 安装前工作

首先更新系统软件源,使用以下命令更新系统

[root@VM-0-11-centos ~]# yum update

依赖包安装

[root@VM-0-11-centos ~]# yum -y install gcc gcc-c++ autoconf automake libtool make cmake
[root@VM-0-11-centos ~]# yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
1.2 下载Nginx安装源文件

源码下载,可官网下载地址:http://nginx.org/en/download.html 下载并上传到服务器(这里推荐选择最新稳定版)
或直接在服务上执行以下命令下载

[root@VM-0-11-centos ~]# cd /usr/local/src
[root@VM-0-11-centos src]# wget -c http://nginx.org/download/nginx-1.10.3.tar.gz

解压上面下载的文件

[root@VM-0-11-centos src]# tar zxvf nginx-1.18.0.tar.gz

在编译之前还要做一些前期的准备工作,如:依懒包安装,Nginx用户和用户组等。

1.3 新建nginx用户及用户组
[root@VM-0-11-centos src]# groupadd nginx
[root@VM-0-11-centos src]# useradd -g nginx -M nginx

修改/etc/passwd,使得nginx用户无法bash登陆(nginx用户后面由/bin/bash改为/sbin/nologin)

[root@VM-0-11-centos src]# vi /etc/passwd

然后找到有 nginx 那一行,把它修改为(后面由/bin/bash改为/sbin/nologin):

nginx:x:1002:1003::/home/nginx:/sbin/nologin
1.4 编译配置、编译、安装

下面我们进入解压的nginx源码目录:/usr/local/src/ 执行以下命令

[root@VM-0-11-centos ~]# cd /usr/local/src/nginx-1.18.0.tar.gz
[root@VM-0-11-centos nginx-1.18.0]# pwd
/usr/local/src/nginx-1.18.0
[root@VM-0-11-centos nginx-1.18.0]#
[root@VM-0-11-centos nginx-1.18.0]# ./configure --prefix=/usr/local/nginx \
--pid-path=/usr/local/nginx/run/nginx.pid \
--with-http_ssl_module \
--user=nginx \
 --group=nginx \
--with-pcre \
--without-mail_pop3_module \
--without-mail_imap_module \
--without-mail_smtp_module

注意:上面的反斜杠 \ 表示换行继续。
--prefix=/usr/local/nginx指定安装到 /usr/local/nginx 目录下。

上面配置完成后,接下来执行编译

[root@VM-0-11-centos nginx-1.18.0]# make
[root@VM-0-11-centos nginx-1.18.0]# make install
... ...
cp conf/nginx.conf '/usr/local/nginx/conf/nginx.conf.default'
test -d '/usr/local/nginx/run' \
        || mkdir -p '/usr/local/nginx/run'
test -d '/usr/local/nginx/logs' \
        || mkdir -p '/usr/local/nginx/logs'
test -d '/usr/local/nginx/html' \
        || cp -R html '/usr/local/nginx'
test -d '/usr/local/nginx/logs' \
        || mkdir -p '/usr/local/nginx/logs'
make[1]: Leaving directory `/usr/local/src/nginx-1.18.0'
[root@VM-0-11-centos nginx-1.18.0]#

等待片刻

查看安装后的程序版本:
[root@VM-0-11-centos nginx-1.18.0]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.18.0
修改Nginx默认端口(可选):
[root@VM-0-11-centos nginx-1.18.0]# vi /usr/local/nginx/conf/nginx.conf

找到

... ...
    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;
... ...

把上面的 80 修改为你想要的端口,如:8080 。(这里推荐不去修改,默认80即可)
修改配置后验证配置是否合法:

[root@VM-0-11-centos nginx-1.18.0]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
启动Nginx程序、查看进程
[root@VM-0-11-centos nginx-1.18.0]# /usr/local/nginx/sbin/nginx
[root@VM-0-11-centos nginx-1.18.0]# ps -ef | grep nginx
root      29151      1  0 22:01 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     29152  29151  0 22:01 ?        00:00:00 nginx: worker process
root      29154   2302  0 22:01 pts/0    00:00:00 grep --color=auto nginx
[root@VM-0-11-centos nginx-1.18.0]#
nginx停止、重启
#  nginx 管理的几种方式 -
# 启动Nginx 
/usr/local/nginx/sbin/nginx 
# 从容停止Nginx:
kill -QUIT 主进程号 # 如上一步中的 ps 命令输出的 29151,就是 Nginx的主进程号
# 快速停止Nginx:
kill -TERM 主进程号
# 强制停止Nginx:
pkill -9 nginx
# 平滑重启nginx(推荐)
/usr/nginx/sbin/nginx -s reload

现在我们来看看安装的Nginx的运行结果,可以简单地使用curl命令访问localhost测试或者打开浏览访问目标服务器的IP

[root@VM-0-11-centos nginx-1.18.0]# curl localhost



Welcome to nginx!



Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

[root@VM-0-11-centos nginx-1.18.0]#
Centos7系统下Nginx+PHP7+MySQL5.7(LNMP)环境配置_第1张图片
image.png

提示: 如果没有看到以上界面,在确保Nginx启动的前提下,检查SeLinux和防火墙是否已关闭。关闭防火墙命令:sudo systemctl stop firewalld。

2. PHP7安装配置

2.1 源码下载

压缩包下载

[root@VM-0-11-centos ~]# cd /usr/local/src
[root@VM-0-11-centos src]# wget -c http://cn2.php.net/distributions/php-7.1.3.tar.gz

:由于访问外网,压缩包下载速度过慢,这里我们可以使用mwget下载压缩包,这里不具体阐述,请自行安装mwget

解压压缩包:

[root@VM-0-11-centos src]# tar -xzvf php-7.1.3.tar.gz
2.2 安装编译所需依赖包
[root@VM-0-11-centos src]# cd php-7.1.3
[root@VM-0-11-centos php-7.1.3]# yum -y install libxml2 libxml2-devel openssl openssl-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel

或者常见大部分依懒包安装

[root@VM-0-11-centos php-7.1.3]# yum install -y wget gcc gcc-c++ autoconf libjpeg libjpeg-devel perl perl* perl-CPAN libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers png jpeg autoconf gcc cmake make gcc-c++ gcc ladp ldap* ncurses ncurses-devel zlib zlib-devel zlib-static pcre pcre-devel pcre-static openssl openssl-devel perl libtoolt openldap-devel libxml2-devel ntpdate cmake gd* gd2 ImageMagick-devel jpeg jpeg* pcre-dev* fontconfig libpng libxml2 zip unzip gzip
2.3 源码编译、安装

通过 ./configure –help 查看支持的编译配置参数,如下所示

[root@VM-0-11-centos php-7.1.3]# ./configure --help
`configure' configures this package to adapt to many kinds of systems.

Usage: ./configure [OPTION]... [VAR=VALUE]...

To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE.  See below for descriptions of some of the useful variables.

Defaults for the options are specified in brackets.

Configuration:
  -h, --helpdisplay this help and exit
      --help=short        display options specific to this package
      --help=recursive    display the short help of all the included packages
  -V, --versiondisplayversion information and exit
  -q, --quiet, --silent   do not print `checking ...' messages
      --cache-file=FILE   cache test results inFILE [disabled]
  -C, --config-cache      alias for `--cache-file=config.cache'
  -n, --no-create         do not create output files
      --srcdir=DIR        find the sources inDIR [configure dir or `..']

Installation directories:
  --prefix=PREFIX         install architecture-independent files in PREFIX
                          [/usr/local]
  --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX
                          [PREFIX]

By default, `make install' will install all the files in
`/usr/local/bin', `/usr/local/lib' etc.  You can specify
an installation prefix other than `/usr/local' using `--prefix',
for instance `--prefix=$HOME'.

For better control, use the options below.

PHP+Nginx组合的编译配置命令

[root@VM-0-11-centos php-7.1.3]# ./configure --prefix=/usr/local/php7 \
--with-config-file-path=/usr/local/php7/etc \
--with-config-file-scan-dir=/usr/local/php7/etc/php.d \
--with-mcrypt=/usr/include \
--enable-mysqlnd \
--with-mysqli \
--with-pdo-mysql \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-gd \
--with-iconv \
--with-zlib \
--enable-xml \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--enable-gd-native-ttf \
--with-openssl \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--without-pear \
--with-gettext \
--enable-session \
--with-curl \
--with-jpeg-dir \
--with-freetype-dir \
--enable-opcache

# 执行完成后的结果:
Generating files
configure: creating ./config.status
creating main/internal_functions.c
creating main/internal_functions_cli.c
+--------------------------------------------------------------------+
| License:                                                           |
| This software is subject to the PHP License, available in this     |
| distribution in the file LICENSE.  By continuing this installation |
| process, you are bound by the terms of this license agreement.     |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point.                            |
+--------------------------------------------------------------------+

Thank you for using PHP.

config.status: creating php7.spec
config.status: creating main/build-defs.h
config.status: creating scripts/phpize
config.status: creating scripts/man1/phpize.1
config.status: creating scripts/php-config
config.status: creating scripts/man1/php-config.1
config.status: creating sapi/cli/php.1
config.status: creating sapi/fpm/php-fpm.conf
config.status: creating sapi/fpm/www.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
config.status: creating sapi/cgi/php-cgi.1
config.status: creating ext/phar/phar.1
config.status: creating ext/phar/phar.phar.1
config.status: creating main/php_config.h
config.status: executing default commands

注:这里很多人第一次安装的时候可能会提示
configure: error: mcrypt.h not found. Please reinstall libmcrypt
意思是,没有查找到mcrytp.h,需要安装libcrytp

解决:
[root@VM-0-11-centos src]# wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/attic/libmcrypt/libmcrypt-2.5.7.tar.gz
[root@VM-0-11-centos src]# tar -zxvf libmcrypt-2.5.7.tar.gz
[root@VM-0-11-centos src]# cd libmcrypt-2.5.7
[root@VM-0-11-centos libmcrypt-2.5.7]# ./configure
[root@VM-0-11-centos libmcrypt-2.5.7]# make && make install
然后重新执行编译

编译 + 安装,编译源码, 如下所示

[root@VM-0-11-centos php-7.1.3]# make
Generating phar.php
Generating phar.phar
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
directorytreeiterator.inc
pharcommand.inc
directorygraphiterator.inc
invertedregexiterator.inc
clicommand.inc
phar.inc

Build complete.
Don't forget to run 'make test'.

## 对编译结果进行测试:
[root@VM-0-11-centos php-7.1.3]# make test
...
...

## 安装程序至指定目录:
[root@VM-0-11-centos php-7.1.3]# make install
Installing shared extensions:     /usr/local/php7/lib/php/extensions/no-debug-non-zts-20160303/
Installing PHP CLI binary:        /usr/local/php7/bin/
Installing PHP CLI man page:      /usr/local/php7/php/man/man1/
Installing PHP FPM binary:        /usr/local/php7/sbin/
Installing PHP FPM defconfig:     /usr/local/php7/etc/
Installing PHP FPM man page:      /usr/local/php7/php/man/man8/
Installing PHP FPM status page:   /usr/local/php7/php/php/fpm/
Installing phpdbg binary:         /usr/local/php7/bin/
Installing phpdbg man page:       /usr/local/php7/php/man/man1/
Installing PHP CGI binary:        /usr/local/php7/bin/
Installing PHP CGI man page:      /usr/local/php7/php/man/man1/
Installing build environment:     /usr/local/php7/lib/php/build/
Installing header files:          /usr/local/php7/include/php/
Installing helper programs:       /usr/local/php7/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php7/php/man/man1/
  page: phpize.1
  page: php-config.1
/usr/local/src/php-7.1.3/build/shtool install -c ext/phar/phar.phar /usr/local/php7/bin
ln -s -f phar.phar /usr/local/php7/bin/phar
Installing PDO headers:           /usr/local/php7/include/php/ext/pdo/
[root@VM-0-11-centos php-7.1.3]#

查看安装成功后的版本信息

[root@VM-0-11-centos ~]# /usr/local/php7/bin/php -v
PHP 7.1.3 (cli) (built: Aug 17 2020 13:48:11) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
[root@VM-0-11-centos ~]# 
2.4. 修改配置

修改php配置,查看php加载配置文件路径:

[root@VM-0-11-centos ~]# /usr/local/php7/bin/php -i | grep php.ini
Configuration File (php.ini) Path => /usr/local/php7/etc
[root@VM-0-11-centos ~]#

php-7.1.3源码目录下:

[root@VM-0-11-centos ~]# ll /usr/local/src/php-7.1.3/ | grep ini
-rw-rw-r--  1 nginx nginx   71063 3月  14 2017 php.ini-development
-rw-rw-r--  1 nginx nginx   71095 3月  14 2017 php.ini-production
[root@VM-0-11-centos ~]# 

复制PHP的配置文件,使用以下命令:

[root@VM-0-11-centos ~]# cp /usr/local/src/php-7.1.3/php.ini-production /usr/local/php7/etc/php.ini
## 根据需要对`php.ini`配置进行配置修改,请自行参考官方文档配置 。
[root@VM-0-11-centos ~]# /usr/local/php7/bin/php -i | grep php.ini
Configuration File (php.ini) Path => /usr/local/php7/etc
Loaded Configuration File => /usr/local/php7/etc/php.ini
[root@VM-0-11-centos ~]# 
2.5 启用php-fpm服务

上面我们在编译 php7 的时候,已经将fpm模块编译了,那么接下来,我们要启用 php-fpm。但是默认情况下它的配置文件和服务都没有启用,所以要我们自己来配置,先重命名并移动以下两个文件

[root@VM-0-11-centos ~]# cd /usr/local/php7/etc
[root@VM-0-11-centos etc]# cp php-fpm.conf.default php-fpm.conf
[root@VM-0-11-centos etc]# cp php-fpm.d/www.conf.default php-fpm.d/www.conf

php-fpm 的具体配置这里不做深入去详解,因为在编译之前 ./configure 的时候,我们都已经确定了一些配置,比如运行fpm的用户和用户组之类的,所以默认配置应该不会存在路径问题和权限问题。

配置php-fpm的服务载入:

就像上面的nginx一样,我们希望使用 service php-fpm start|stop|restart 这些操作来实现服务的重启,但没有像nginx那么复杂,php编译好之后,给我们提供了一个 php-fpm 的程序。这个文件放在php编译源码目录中:

[root@VM-0-11-centos ~]#  cd /usr/local/src/php-7.1.3/sapi/fpm/
## 或直接使用可执行文件: /usr/local/php7/sbin/php-fpm
[root@VM-0-11-centos fpm]# ls
[root@VM-0-11-centos fpm]# cp init.d.php-fpm /etc/init.d/php-fpm
[root@VM-0-11-centos fpm]# chmod +x /etc/init.d/php-fpm
[root@VM-0-11-centos fpm]# chkconfig --add php-fpm
[root@VM-0-11-centos fpm]# chkconfig php-fpm on

通过上面这个操作,我们就可以使用 service php-fpm start 来启用 php-fpm 了。用 ps -ef | grep php-fpm 看看进程吧。

[root@VM-0-11-centos fpm]# ps -ef | grep php-fpm
root     108421      1  0 23:19 ?        00:00:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf)
nginx    108422 108421  0 23:19 ?        00:00:00 php-fpm: pool www
nginx    108423 108421  0 23:19 ?        00:00:00 php-fpm: pool www
root     108507   2285  0 23:23 pts/0    00:00:00 grep --color=auto php-fpm
[root@VM-0-11-centos fpm]#

这样,PHP环境就安装完成了,接下来我们通过Nginx代理集成PHP7,来实现Nginx+PHP服务。

3. Nginx代理集成PHP7配置

通过上面的操作,nginxphp-fpm 服务都已经正常运行起来了,但是 php-fpm 只是在 127.0.0.1:9000 上提供服务,外网是无法访问的,而且也不可能直接通过 php-fpm 给外网提供服务,因此需要使用nginx去代理 9000 端口执行 php。实际上这个过程只需要对 nginx 进行配置即可,php-fpm 已经在后台运行了,我们需要在 nginx 的配置文件中增加代理的规则,即可让用户在访问 80 端口,请求php的时候,交由后端的 php-fpm 去执行,并返回结果。现在编辑Nginx的配置文件

[root@VM-0-11-centos ~]# vi /usr/local/nginx/conf/nginx.conf

如果你大致了解过 nginx 的配置,应该能够很快分辨出这个配置文件里面的结构,并且知道 server 块代表一个虚拟主机,要增加虚拟主机就再增加一个server块,而且这个 conf 文件中也给出了例子。那么怎么代理 php-fpm 呢?找到:

#location ~ \.php$ {
#           root           html;
#           fastcgi_pass   127.0.0.1:9000;
#           fastcgi_index  index.php;
#           fastcgi_param  SCRIPT_FILENAME  /script$fastcgi_script_name;
#           include        fastcgi_params;
#}

把前面的 # 注释符号去掉,把 script 改为 $document_root 最终如下:

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;
        }

这样就可以了,重新载入nginx配置即可,使用以下命令

/usr/local/nginx/sbin/nginx -s reload

然后到 /usr/local/nginx/html 去写一个 php 文件:index.php 进行测试,文件:index.php的代码如下:


现在访问目录IP,应该能看到结果如下:


Centos7系统下Nginx+PHP7+MySQL5.7(LNMP)环境配置_第2张图片
phpinfo.png

附完整的Nginx配置( /usr/local/nginx/conf/nginx.conf )文件内容:


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.php index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        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;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
        proxy_connect_timeout    600;
        proxy_read_timeout       600;
        proxy_send_timeout       600;
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

3.MySQL5.7安装配置

3.1下载安装包
wget http://repo.mysql.com//mysql57-community-release-el7-7.noarch.rpm 
# 或者
wget http://cdn.mysql.com//Downloads/MySQL-5.7/mysql-community-server-5.7.18-1.el7.x86_64.rpm

如果新的系统还没有wget命令的话可以先:yum install wget,一般都会有安装了wget命令工具。

添加选择yum源:

[root@VM-0-11-centos src]# yum localinstall mysql57-community-release-el7-7.noarch.rpm 
[root@VM-0-11-centos src]# yum repolist all | grep mysql
mysql-connectors-community/x86_64 MySQL Connectors Community      enabled:    30
mysql-connectors-community-source MySQL Connectors Community - So disabled
mysql-tools-community/x86_64      MySQL Tools Community           enabled:    47
mysql-tools-community-source      MySQL Tools Community - Source  disabled
mysql55-community/x86_64          MySQL 5.5 Community Server      disabled
mysql55-community-source          MySQL 5.5 Community Server - So disabled
mysql56-community/x86_64          MySQL 5.6 Community Server      disabled
mysql56-community-source          MySQL 5.6 Community Server - So disabled
mysql57-community/x86_64          MySQL 5.7 Community Server      enabled:   187
mysql57-community-source          MySQL 5.7 Community Server - So disabled
3.2安装MySQL:
[root@VM-0-11-centos src]# yum install mysql-community-server 
.....
  Installing : mysql-community-server-5.7.18-1.el7.x86_64                   4/6
  Installing : mysql-community-libs-compat-5.7.18-1.el7.x86_64              5/6
  Erasing    : 1:mariadb-libs-5.5.52-1.el7.x86_64                           6/6
  Verifying  : mysql-community-server-5.7.18-1.el7.x86_64                   1/6
  Verifying  : mysql-community-common-5.7.18-1.el7.x86_64                   2/6
  Verifying  : mysql-community-client-5.7.18-1.el7.x86_64                   3/6
  Verifying  : mysql-community-libs-compat-5.7.18-1.el7.x86_64              4/6
  Verifying  : mysql-community-libs-5.7.18-1.el7.x86_64                     5/6
  Verifying  : 1:mariadb-libs-5.5.52-1.el7.x86_64                           6/6

Installed:
  mysql-community-libs.x86_64 0:5.7.18-1.el7
  mysql-community-libs-compat.x86_64 0:5.7.18-1.el7
  mysql-community-server.x86_64 0:5.7.18-1.el7

Dependency Installed:
  mysql-community-client.x86_64 0:5.7.18-1.el7
  mysql-community-common.x86_64 0:5.7.18-1.el7

Replaced:
  mariadb-libs.x86_64 1:5.5.52-1.el7

Complete!
[root@VM-0-11-centos src]#

安装完成之后会自动在log中生成连接的密码

3.3启动Mysql:

启动Mysql
systemctl start mysqld
查看状态
systemctl status mysqld

[root@localhost src]# systemctl start mysqld
[root@localhost src]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 二 2020-08-18 20:33:25 CST; 1 day 21h ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
 Main PID: 17343 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─17343 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/my...

8月 18 20:33:24 VM-0-11-centos systemd[1]: Stopped MySQL Server.
8月 18 20:33:24 VM-0-11-centos systemd[1]: Starting MySQL Server...
8月 18 20:33:25 VM-0-11-centos systemd[1]: Started MySQL Server.
[root@localhost src]#

要知道在centos7中,没有了service命令,都是使用systemctl命令。
启动的时候是start mysqld而不是mysql

3.4 登录数据库,修改数据库密码

mysql5.7的新特性之一就是在初始化的时候会生成一个自定义的密码,然后你需要找到这个密码,登录的时候输入。注意,输入密码的时候是不显示

[root@VM-0-11-centos src]# grep password /var/log/mysqld.log
2020-08-18T09:15:17.046285Z 1 [Note] A temporary password is generated for root@localhost: afWrxaqQi0!M
[root@VM-0-11-centos src]#

如上面所示,root用户的密码为:afWrxaqQi0!M。现在我们使用上面的密码连接到MySQL数据。

3.4.1登录数据库:

这里-p之后不用输入密码,回车后再输入。改过密码之后登录则是直接在-p后加密码了。

[root@VM-0-11-centos src]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1462
Server version: 5.7.31 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

3.4.2 修改密码:

注意,修改的密码太简单会不给修改,把大小写字母和数字加上就肯定可以了。然后切记切记,mysql里面的命令要加分号!分号!分号!

mysql> SET PASSWORD = PASSWORD('Admin123456');
3.5 设置远程可以登录:

现在这样是无法在本地用工具登录访问的,现在要做两件事,一件事是将云服务器上的3306端口开放;另一件事是配置远程可以访问。

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Admin123456' WITH GRANT OPTION;
mysql> flush privileges;
3.6 修改一些简单的配置

3.6.1 设置编码

vim /etc/my.cnf

[mysql]
default-character-set=utf8mb4

[client]
default-character-set=utf8mb4

[mysqld]
character-set-server=utf8mb4

3.6.2 设置时区为东八区

default-time_zone = '+8:00'

附上my.conf配置

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysql]
default-character-set=utf8mb4

[client]
default-character-set=utf8mb4

[mysqld]
character-set-server=utf8mb4

#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
default-time_zone = '+8:00'
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

3.6.3设置sql支持group by语句

mysql> sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION 

3.6.4 设置开机启动
systemctl enable mysqld
systemctl daemon-reload

最后重启数据库,使配置生效:
systemctl restart mysqld

到此,PHP+Nginx+MySQL安装完成。如有问题,请及时提出。

参考文献:
https://www.yiibai.com/nginx/nginx-php7-source-config.html
https://www.jianshu.com/p/531cc35b15e7

你可能感兴趣的:(Centos7系统下Nginx+PHP7+MySQL5.7(LNMP)环境配置)