Centos7系統(tǒng)下Nginx+PHP7+MySQL5.7(LNMP)環(huán)境配置

前提:

在本文中,主要介紹從源代碼安裝Nginx、PHP、Mysql,這篇教程是基于CentOS7 64bit系統(tǒng)來(lái)安裝的,非Centos系統(tǒng)不適用?,F(xiàn)在我們就開(kāi)始吧!

目錄:

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

1、Nginx安裝配置

1.1 安裝前工作

首先更新系統(tǒng)軟件源,使用以下命令更新系統(tǒng)

[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安裝源文件

源碼下載,可官網(wǎng)下載地址:http://nginx.org/en/download.html 下載并上傳到服務(wù)器(這里推薦選擇最新穩(wěn)定版)
或直接在服務(wù)上執(zhí)行以下命令下載

[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

在編譯之前還要做一些前期的準(zhǔn)備工作,如:依懶包安裝,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用戶無(wú)法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 編譯配置、編譯、安裝

下面我們進(jìn)入解壓的nginx源碼目錄:/usr/local/src/ 執(zhí)行以下命令

[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

注意:上面的反斜杠 \ 表示換行繼續(xù)。
--prefix=/usr/local/nginx指定安裝到 /usr/local/nginx 目錄下。

上面配置完成后,接下來(lái)執(zhí)行編譯

[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默認(rèn)端口(可選):
[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 。(這里推薦不去修改,默認(rèn)80即可)
修改配置后驗(yàn)證配置是否合法:

[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
啟動(dòng)Nginx程序、查看進(jìn)程
[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 管理的幾種方式 -
# 啟動(dòng)Nginx 
/usr/local/nginx/sbin/nginx 
# 從容停止Nginx:
kill -QUIT 主進(jìn)程號(hào) # 如上一步中的 ps 命令輸出的 29151,就是 Nginx的主進(jìn)程號(hào)
# 快速停止Nginx:
kill -TERM 主進(jìn)程號(hào)
# 強(qiáng)制停止Nginx:
pkill -9 nginx
# 平滑重啟nginx(推薦)
/usr/nginx/sbin/nginx -s reload

現(xiàn)在我們來(lái)看看安裝的Nginx的運(yùn)行結(jié)果,可以簡(jiǎn)單地使用curl命令訪問(wèn)localhost測(cè)試或者打開(kāi)瀏覽訪問(wèn)目標(biāo)服務(wù)器的IP

[root@VM-0-11-centos nginx-1.18.0]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a >nginx.org</a>.<br/>
Commercial support is available at
<a >nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@VM-0-11-centos nginx-1.18.0]#
image.png

提示: 如果沒(méi)有看到以上界面,在確保Nginx啟動(dòng)的前提下,檢查SeLinux和防火墻是否已關(guān)閉。關(guān)閉防火墻命令: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

:由于訪問(wèn)外網(wǎng),壓縮包下載速度過(guò)慢,這里我們可以使用mwget下載壓縮包,這里不具體闡述,請(qǐng)自行安裝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

或者常見(jiàn)大部分依懶包安裝

[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 源碼編譯、安裝

通過(guò) ./configure –help 查看支持的編譯配置參數(shù),如下所示

[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

# 執(zhí)行完成后的結(jié)果:
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

注:這里很多人第一次安裝的時(shí)候可能會(huì)提示
configure: error: mcrypt.h not found. Please reinstall libmcrypt
意思是,沒(méi)有查找到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
然后重新執(zhí)行編譯

編譯 + 安裝,編譯源碼, 如下所示

[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'.

## 對(duì)編譯結(jié)果進(jìn)行測(cè)試:
[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 ~]# 

復(fù)制PHP的配置文件,使用以下命令:

[root@VM-0-11-centos ~]# cp /usr/local/src/php-7.1.3/php.ini-production /usr/local/php7/etc/php.ini
## 根據(jù)需要對(duì)`php.ini`配置進(jìn)行配置修改,請(qǐng)自行參考官方文檔配置 。
[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服務(wù)

上面我們?cè)诰幾g php7 的時(shí)候,已經(jīng)將fpm模塊編譯了,那么接下來(lái),我們要啟用 php-fpm。但是默認(rèn)情況下它的配置文件和服務(wù)都沒(méi)有啟用,所以要我們自己來(lái)配置,先重命名并移動(dòng)以下兩個(gè)文件

[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 的具體配置這里不做深入去詳解,因?yàn)樵诰幾g之前 ./configure 的時(shí)候,我們都已經(jīng)確定了一些配置,比如運(yùn)行fpm的用戶和用戶組之類的,所以默認(rèn)配置應(yīng)該不會(huì)存在路徑問(wèn)題和權(quán)限問(wèn)題。

配置php-fpm的服務(wù)載入:

就像上面的nginx一樣,我們希望使用 service php-fpm start|stop|restart 這些操作來(lái)實(shí)現(xiàn)服務(wù)的重啟,但沒(méi)有像nginx那么復(fù)雜,php編譯好之后,給我們提供了一個(gè) php-fpm 的程序。這個(gè)文件放在php編譯源碼目錄中:

[root@VM-0-11-centos ~]#  cd /usr/local/src/php-7.1.3/sapi/fpm/
## 或直接使用可執(zhí)行文件: /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

通過(guò)上面這個(gè)操作,我們就可以使用 service php-fpm start 來(lái)啟用 php-fpm 了。用 ps -ef | grep php-fpm 看看進(jìn)程吧。

[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環(huán)境就安裝完成了,接下來(lái)我們通過(guò)Nginx代理集成PHP7,來(lái)實(shí)現(xiàn)Nginx+PHP服務(wù)。

3. Nginx代理集成PHP7配置

通過(guò)上面的操作,nginxphp-fpm 服務(wù)都已經(jīng)正常運(yùn)行起來(lái)了,但是 php-fpm 只是在 127.0.0.1:9000 上提供服務(wù),外網(wǎng)是無(wú)法訪問(wèn)的,而且也不可能直接通過(guò) php-fpm 給外網(wǎng)提供服務(wù),因此需要使用nginx去代理 9000 端口執(zhí)行 php。實(shí)際上這個(gè)過(guò)程只需要對(duì) nginx 進(jìn)行配置即可,php-fpm 已經(jīng)在后臺(tái)運(yùn)行了,我們需要在 nginx 的配置文件中增加代理的規(guī)則,即可讓用戶在訪問(wèn) 80 端口,請(qǐng)求php的時(shí)候,交由后端的 php-fpm 去執(zhí)行,并返回結(jié)果?,F(xiàn)在編輯Nginx的配置文件

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

如果你大致了解過(guò) nginx 的配置,應(yīng)該能夠很快分辨出這個(gè)配置文件里面的結(jié)構(gòu),并且知道 server 塊代表一個(gè)虛擬主機(jī),要增加虛擬主機(jī)就再增加一個(gè)server塊,而且這個(gè) 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;
#}

把前面的 # 注釋符號(hào)去掉,把 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 去寫一個(gè) php 文件:index.php 進(jìn)行測(cè)試,文件:index.php的代碼如下:

<?php
    phpinfo();
?>

現(xiàn)在訪問(wèn)目錄IP,應(yīng)該能看到結(jié)果如下:


phpinfo.png

附完整的Nginx配置( /usr/local/nginx/conf/nginx.conf )文件內(nèi)容:


#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

如果新的系統(tǒng)還沒(méi)有wget命令的話可以先:yum install wget,一般都會(huì)有安裝了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]#

安裝完成之后會(huì)自動(dòng)在log中生成連接的密碼

3.3啟動(dòng)Mysql:

啟動(dòng)Mysql
systemctl start mysqld
查看狀態(tài)
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中,沒(méi)有了service命令,都是使用systemctl命令。
啟動(dòng)的時(shí)候是start mysqld而不是mysql

3.4 登錄數(shù)據(jù)庫(kù),修改數(shù)據(jù)庫(kù)密碼

mysql5.7的新特性之一就是在初始化的時(shí)候會(huì)生成一個(gè)自定義的密碼,然后你需要找到這個(gè)密碼,登錄的時(shí)候輸入。注意,輸入密碼的時(shí)候是不顯示

[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。現(xiàn)在我們使用上面的密碼連接到MySQL數(shù)據(jù)。

3.4.1登錄數(shù)據(jù)庫(kù):

這里-p之后不用輸入密碼,回車后再輸入。改過(guò)密碼之后登錄則是直接在-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 修改密碼:

注意,修改的密碼太簡(jiǎn)單會(huì)不給修改,把大小寫字母和數(shù)字加上就肯定可以了。然后切記切記,mysql里面的命令要加分號(hào)!分號(hào)!分號(hào)!

mysql> SET PASSWORD = PASSWORD('Admin123456');
3.5 設(shè)置遠(yuǎn)程可以登錄:

現(xiàn)在這樣是無(wú)法在本地用工具登錄訪問(wèn)的,現(xiàn)在要做兩件事,一件事是將云服務(wù)器上的3306端口開(kāi)放;另一件事是配置遠(yuǎn)程可以訪問(wèn)。

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Admin123456' WITH GRANT OPTION;
mysql> flush privileges;
3.6 修改一些簡(jiǎn)單的配置

3.6.1 設(shè)置編碼

vim /etc/my.cnf

[mysql]
default-character-set=utf8mb4

[client]
default-character-set=utf8mb4

[mysqld]
character-set-server=utf8mb4

3.6.2 設(shè)置時(shí)區(qū)為東八區(qū)

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設(shè)置sql支持group by語(yǔ)句

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 設(shè)置開(kāi)機(jī)啟動(dòng)
systemctl enable mysqld
systemctl daemon-reload

最后重啟數(shù)據(jù)庫(kù),使配置生效:
systemctl restart mysqld

到此,PHP+Nginx+MySQL安裝完成。如有問(wèn)題,請(qǐng)及時(shí)提出。

參考文獻(xiàn):
https://www.yiibai.com/nginx/nginx-php7-source-config.html
http://www.lxweimin.com/p/531cc35b15e7

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。