LNMP(centos7.6 Nginx1.16 MySQL5.7 PHP7 )

一、源码安装(Linux+Nginx+MySQL+PHP)

MySQL5.7安装   

1、删除残留的mariadb

[root@web01 ~]# rpm -qa|grep mariadb
mariadb-5.5.64-1.el7.x86_64
mariadb-server-5.5.64-1.el7.x86_64
mariadb-libs-5.5.64-1.el7.x86_64
mariadb-devel-5.5.64-1.el7.x86_64
[root@web01 ~]# yum remove mariadb
删除遗留目录
[root@web01 ~]# rm -rf /etc/my.cnf
[root@web01 ~]# rm -rf /var/lib/mysql/

2、下载源码包

[root@web01 ~]# cd /usr/local/src/
[root@web01 src]# wget http://www.sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
或者wget http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
[root@web01 src]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22.tar.gz
[root@web01 src]# tar -zxvf mysql-5.7.22.tar.gz
[root@web01 src]# tar -zxvf boost_1_59_0.tar.gz

常见问题:
[root@web01 src]# tar -zvxf boost_1_59_0.tar.gz #可以是这个包没有完整下载 本身是100多M 现在只用8K
gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now
[root@web01 src]# tar -zxvf boost_1_59_0.tar.gz #重新下载了一个 100多M  #MySQL5.7指定要求boost_1_59
[root@web01 src]# mv boost_1_59_0 /usr/local/

3、编译安装

[root@web01 src]#yum -y install cmake make gcc gcc-c++ openssl-devel pcre-devel zlib-devel ncurses-devel boost boost-doc boost-devel  #安装依赖

[root@web01 src]# groupadd mysql
[root@web01 src]# useradd -g mysql -s /sbin/nologin mysql -M
[root@web01 src]# mkdir -p /usr/local/mysql/conf 
[root@web01 src]# mkdir -p /usr/local/mysql/data
[root@web01 src]# chown -R mysql:mysql /usr/local/mysql/
[root@web01 src]# ll /usr/local/mysql/
总用量 0
drwxr-xr-x 2 mysql mysql 6 10月 31 14:34 conf
drwxr-xr-x 2 mysql mysql 6 10月 31 14:35 data

[root@web01 ~]# cd /usr/local/src/mysql-5.7.22
[root@web01 mysql-5.7.22]#cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DSYSCONFDIR=/usr/local/mysql/conf -DMYSQL_UNIX_ADDR=/usr/local/mysql/conf/mysql.sock -DSYSTEMD_PID_DIR=/usr/local/mysql/conf -DMYSQL_USER=mysql -DWITH_SYSTEMD=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DMYSQL_TCP_PORT=3306 -DENABLED_LOCAL_INFILE=1 -DENABLE_DOWNLOADS=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EMBEDDED_SERVER=1 -DWITH_DEBUG=0 -DMYSQL_MAINTAINER_MODE=0 -DWITH_SSL:STRING=bundled -DWITH_ZLIB:STRING=bundled -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost_1_59_0

[root@web01 mysql-5.7.22]# make -j$(cat /proc/cpuinfo| grep "processor"| wc -l) && make install -j$(cat /proc/cpuinfo| grep "processor"| wc -l)  #
cmake 
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql  #安装目录
-DMYSQL_DATADIR=/usr/local/mysql/data  #数据存放目录
-DSYSCONFDIR=/usr/local/mysql/conf  #配置文件目录
-DMYSQL_UNIX_ADDR=/usr/local/mysql/conf/mysql.sock  #套接字路劲
-DSYSTEMD_PID_DIR=/usr/local/mysql/conf  #PID目录
-DMYSQL_USER=mysql  #运行mysql进行的用户,如果没有需要提前添加用户和用户组
-DWITH_SYSTEMD=1 
-DWITH_MYISAM_STORAGE_ENGINE=1  #激活myisam
-DWITH_INNOBASE_STORAGE_ENGINE=1  #激活innodb 

-DWITH_PARTITION_STORAGE_ENGINE=1  #安装支持数据库分区 储存引擎 PARTITION
-DWITH_ARCHIVE_STORAGE_ENGINE=1  #储存引擎 BLACKHOLE
-DWITH_MEMORY_STORAGE_ENGINE=1     #

-DWITH_READLINE=1 
-DMYSQL_TCP_PORT=3306  #端口
-DENABLED_LOCAL_INFILE=1  #允许导入数据
-DENABLE_DOWNLOADS=1  #是否要下载可选的文件。例如,启用此选项(设置为1),cmake将下载谷歌所使用的测试套件运行单元测试。
-DEXTRA_CHARSETS=all     #安装所有的字符集
-DDEFAULT_CHARSET=utf8  #默认编码
-DDEFAULT_COLLATION=utf8_general_ci  #默认校验规则
-DWITH_EMBEDDED_SERVER=1  #嵌入式服务器
-DWITH_DEBUG=0  #是否包含调试支持
-DMYSQL_MAINTAINER_MODE=0 
-DWITH_SSL:STRING=bundled 
-DWITH_ZLIB:STRING=bundled 
-DDOWNLOAD_BOOST=1 
-DWITH_BOOST=/usr/local/boost_1_59_0 ##增强插件

###################
-DWITH_BOOST=boost #boost库,mysql包自带boost的源码包只需要这样即可,不带boost源码包需要指明boost目录 


存储引擎选项:

MyISAM,MERGE,MEMORY,和CSV引擎是默认编译到服务器中,并不需要明确地安装。

静态编译一个存储引擎到服务器,使用-DWITH_engine_STORAGE_ENGINE= 1

可用的存储引擎值有:ARCHIVE, BLACKHOLE, EXAMPLE, FEDERATED, INNOBASE (InnoDB), PARTITION (partitioning support), 和PERFSCHEMA (Performance Schema)

 
编译参数说明

 # make 
# 该命令中可以通过添加-j参数指定多线程工作,如make -j2 && make install -j2 则使用2个CPU核进行make
# 该步骤执行完毕后,可以到CMAKE_INSTALL_PREFIX参数指定的目录下,即MySQL安装目录下查看到mysql相关目录与文件
make && make install
  注:如果编译出现错误,请先删除CMakeCache.txt后,再重新编译:
rm -rf CMakeCache.txt

 4、配置文件详细

#编辑systemd启动文件

[root@web01 mysql-5.7.22]# cp ./scripts/mysqld.service /usr/lib/systemd/system  
[root@web01 mysql-5.7.22]# vim /usr/lib/systemd/system/mysqld.service
[root@web01 mysql-5.7.22]# cat scripts/mysqld.service 
# Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#
# systemd service file for MySQL forking server
#

[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql

Type=forking

PIDFile=/usr/local/mysql/conf/mysqld.pid

# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0

# Execute pre and post scripts as root
PermissionsStartOnly=true

# Needed to create system tables
ExecStartPre=/usr/local/mysql/bin/mysqld_pre_systemd

# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/conf/mysqld.pid $MYSQLD_OPTS

# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql

# Sets open_files_limit
LimitNOFILE = 5000

Restart=on-failure

RestartPreventExitStatus=1

PrivateTmp=false
mysqld.service

#添加环境变量

[root@web01 mysql-5.7.22]# cat > /etc/profile.d/mysql.sh << EOF 
PATH=/usr/local/mysql/bin:$PATH
export PATH
EOF
[root@web01 mysql-5.7.22]# source /etc/profile

#my.cnf配置文件

官网说:从5.7.18开始不在二进制包中提供my-default.cnf文件,不再需要my.cnf。从源码编译安装的mysql,基本配置在预编译和systemd启动文件中已定义好,不用my.cnf配置

5、初始化 无密码 mysql 数据库

之前版本在/usr/local/src/mysql-5.7.22/script目录下的初始化程序mysql_install_db本版本已移除,转而使用mysqld替代。

-–initialize 会生成一个随机密码(保存在~/.mysql_secret),而 -–initialize-insecure 不会生成密码。
-–datadir目标目录下不能有数据文件

[root@web01 mysql]# rm -rf /usr/local/mysql/data/*
[root@web01 mysql]# /opt/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql –datadir=/usr/local/mysql/data
[root@web01 mysql]# mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
2019-10-31T08:14:56.695152Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-10-31T08:14:56.862366Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-10-31T08:14:56.898419Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-10-31T08:14:56.953826Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 866eca1f-fbb6-11e9-9e6e-1866dafae7b8.
2019-10-31T08:14:56.954350Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-10-31T08:14:56.954981Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
[root@web01 mysql]#

6、启动mysql

systemctl enable mysqld.service
systemctl daemon-reload
systemctl start mysqld.service
systemctl status mysqld.service

7、密码问题

初始化

查看默认root本地登录密码
如果初始化不是用空密码  则初始化的数据库的密码在: [root@web01 mysql]# grep 'temporary password' /var/log/mysql.log (好像为空)

[root@web01 mysql]#mysql_secure_installation
Change the password for root ? y
New password:Tuzuu@159#357!
Remove anonymous users? y
Disallow root login remotely? y
Remove test database and access to it? y
Reload privilege tables now? y

更改用户密码命令

mysql -u root -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Xsssx1231231';
mysql> FLUSH PRIVILEGES;
mysql> exit;

开放 Root 远程连接权限  (password 为远程连接用的密码 )

mysql -u root -p
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Tuzuu@159#357!' WITH GRANT OPTION; 
mysql> FLUSH PRIVILEGES;
mysql> exit;

 

8、防火墙开放mysql端口

firewall-cmd --add-service=mysql --permanent
firewall-cmd --reload
lsof -i:3306

 Nginx安装

 见 https://www.cnblogs.com/linux985/p/11363600.html

[root@ ~]# useradd -M -s /sbin/nologin nginx  #新建Nginx用户  下面php也用到了


PHP安装

1、通过yum命令安装php需要的常用库

yum -y install libmcrypt-devel mhash-devel libxslt-devel libjpeg libjpeg-devel 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

2、下载并安装php

#下载路径 https://www.php.net/downloads.php

[root@web01 src]# tar -xf php-7.3.11.tar.gz
[root@web01 src]# cd /usr/local/src/php-7.3.11
[root@web01 php-7.3.11]# ./configure 
--prefix=/usr/local/php7 
--exec-prefix=/usr/local/php7 
--bindir=/usr/local/php7/bin 
--sbindir=/usr/local/php7/sbin 
--includedir=/usr/local/php7/include 
--libdir=/usr/local/php7/lib/php 
--mandir=/usr/local/php7/php/man 
--with-config-file-path=/usr/local/php7/etc 
--with-mysql-sock=/usr/local/mysql/conf/mysql.sock 
--with-mhash 
--with-openssl 
--with-pdo-mysql=shared,mysqlnd 
--with-mysqli=shared,mysqlnd 
--with-pdo-mysql=shared,mysqlnd 
--with-gd 
--with-iconv 
--with-zlib 
--enable-zip 
--enable-inline-optimization 
--disable-debug 
--disable-rpath 
--enable-shared 
--enable-xml 
--enable-bcmath 
--enable-shmop 
--enable-sysvsem 
--enable-mbregex 
--enable-mbstring 
--enable-ftp 
--enable-pcntl 
--enable-sockets 
--with-xmlrpc 
--enable-soap 
--without-pear 
--with-gettext 
--enable-session 
--with-curl 
--with-jpeg-dir 
--with-freetype-dir 
--enable-opcache 
--enable-fpm 
--with-fpm-user=nginx 
--with-fpm-group=nginx 
--without-gdbm 
--enable-calendar 
--enable-pdo 
--with-bz2 
--enable-sysvshm 
--with-pcre-regex 
--disable-fileinfo
编译安装

常见问题:

问题一:下面这个编译安装会有问题

./configure 
--prefix=/usr/local/php7 
--exec-prefix=/usr/local/php7 
--bindir=/usr/local/php7/bin 
--sbindir=/usr/local/php7/sbin 
--includedir=/usr/local/php7/include 
--libdir=/usr/local/php7/lib/php 
--mandir=/usr/local/php7/php/man 
--with-config-file-path=/usr/local/php7/etc 
--with-mysql-sock=/usr/local/mysql/conf/mysql.sock 
--with-mcrypt=/usr/include 
--with-mhash 
--with-openssl 
--with-mysql=shared,mysqlnd  # --with-mysql --》--with-pdo-mysql
--with-mysqli=shared,mysqlnd 
--with-pdo-mysql=shared,mysqlnd 
--with-gd 
--with-iconv 
--with-zlib 
--enable-zip 
--enable-inline-optimization 
--disable-debug 
--disable-rpath 
--enable-shared 
--enable-xml 
--enable-bcmath 
--enable-shmop 
--enable-sysvsem 
--enable-mbregex 
--enable-mbstring 
--enable-ftp 
--enable-gd-native-ttf 
--enable-pcntl 
--enable-sockets 
--with-xmlrpc 
--enable-soap 
--without-pear 
--with-gettext 
--enable-session 
--with-curl 
--with-jpeg-dir 
--with-freetype-dir 
--enable-opcache 
--enable-redis 
--enable-fpm 
--enable-fastcgi 
--with-fpm-user=nginx 
--with-fpm-group=nginx 
--without-gdbm 
--enable-calendar
--enable-pdo 
--with-bz2 
--enable-sysvshm 
--with-pcre-regex 
--disable-fileinfo
编译安装

configure: WARNING: unrecognized options: --with-mcrypt, --with-mysql, --enable-gd-native-ttf, --enable-redis, --enable-fastcgi, --enable-calendar--enable-pdo

--with-mcrypt, --enable-gd-native-ttf, --enable-fastcgi 新版不支持了

问题二:./configure   报错 :checking for libzip... configure: error: system libzip must be upgraded to version >= 0.11

#先删除旧版本
yum remove -y libzip
#下载编译安装
wget https://nih.at/libzip/libzip-1.2.0.tar.gz
tar -zxvf libzip-1.2.0.tar.gz
cd libzip-1.2.0
./configure
make && make install

 问题三:off_t undefined 报错

checking libzip... yes

checking for the location of zlib... /usr

checking for pkg-config... (cached) /usr/bin/pkg-config

checking for libzip... in default path: found in /usr/local

checking for zip_open in -lzip... yes

checking for zip_file_set_encryption in -lzip... yes

checking for zip_libzip_version in -lzip... no

checking stdbool.h usability... yes

checking stdbool.h presence... yes

checking for stdbool.h... yes

checking fts.h usability... yes

checking fts.h presence... yes

checking for fts.h... yes

checking for int8_t... (cached) yes

checking for int16_t... (cached) yes

checking for int32_t... (cached) yes

checking for int64_t... (cached) yes

checking for uint8_t... (cached) yes

checking for uint16_t... (cached) yes

checking for uint32_t... (cached) yes

checking for uint64_t... (cached) yes

checking for ssize_t... yes

checking size of short... (cached) 2

checking size of int... (cached) 4

checking size of long... (cached) 8

checking size of long long... (cached) 8

checking size of off_t... 0

configure: error: off_t undefined; check your library configuration
off_t undefined 报错

 off_t 类型是在 头文件 unistd.h中定义的,
在32位系统 编程成 long int ,64位系统则编译成 long long int ,
在进行编译的时候 是默认查找64位的动态链接库,
但是默认情况下 centos 的动态链接库配置文件/etc/ld.so.conf里并没有加入搜索路径,
这个时候需要将 /usr/local/lib64 /usr/lib64 这些针对64位的库文件路径加进去。

解决方法:

#添加搜索路径到配置文件
echo '/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64'>>/etc/ld.so.conf

#然后 更新配置
ldconfig -v

[root@web01 php-7.3.11]# make -j$(cat /proc/cpuinfo| grep "processor"| wc -l) && make install -j$(cat /proc/cpuinfo| grep "processor"| wc -l)

问题四:usr/local/include/zip.h:59:21: fatal error: zipconf.h: No such file or directory

解决方法:[root@web01 php-7.3.11]# cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h

3、配置文件

1、php-fpm.conf是PHP-FPM特有的配置文件
2、php.ini是所以php模式中必须的配置文件
两者的区别是,php-fpm.conf是PHP-FPM进程管理器的配置文件,php.ini是PHP解析器的配置文件

有的PHP版本的配置文件路径中还有/php-fpm.d/www.conf配置文件
这是php-fpm.conf配置的文件的扩展文件,可以打开php-fpm.conf文件查看 
cat php-fpm.conf
 ; include=fpm.d/*.conf

官方提供的php模式配置模板拷贝到配置参数中所指定的目录中

[root@web01 ~]# cd /usr/local/src/php-7.3.11
[root@web01 php-7.3.11]# cp /usr/local/src/php-7.3.11/php.ini-production /usr/local/php7/etc/php.ini

 官方提供的php-ftm.conf配置模板拷贝到配置参数中所指定的目录中

cd /usr/local/php7/etc
cp php-fpm.conf.default php-fpm.conf

cd /usr/local/php7/etc/php-fpm.d
cp www.conf.default www.conf

 使用vim命令对php-fpm.conf的内容进行如下修改

pid= /usr/local/php7/var/run/php-fpm.pid

 使用vim命令对www.conf的内容进行如下修改

user = nginx
group = nginx
listen = 127.0.0.1:9000
pm.max_children = 100
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35 

 其它配置可根据自己的情况进行相应修改

4、启动php-ftm

完成以上配置后,即可启动php-fpm,执行以下命令 : /usr/local/php7/sbin/php-fpm

启动后,使用 ps aux | grep php-fpm 命令查看进程,若能够看到相关进程,则证明启动成功。查询进程时,可以看到进程是以nginx用户身份执行的

5、加入系统环境变量

使用vim命令打开/etc/profile文件,在文件最末尾加上如下代码

vim /etc/profile
export PHP_HOME=/usr/local/php7
export PATH=$PATH:$PHP_HOME/bin:$PHP_HOME/sbin
source /etc/profile
echo $PATH  #查看是否成功

6、设置php-fpm开机自启动

经过下面的操作,php-fpm已被添加为系统服务,所以可以将其设置为开机自启动,这样就可以避免每次服务器重启之后需要手动开启php-fpm的操作,具体命令如下

[root@ php-7.3.11]# cp /usr/local/src/php-7.3.11/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@ php-7.3.11]# chmod +x /etc/init.d/php-fpm
[root@ php-7.3.11]# chkconfig --add php-fpm
[root@ php-7.3.11]# chkconfig php-fpm on

二、yum安装

02. 网站的LNMP架构是什么

L --- linux系统
注意:  a selinux必须关闭 防火墙关闭  b /tmp  1777 mysql服务无法启动
N --- nginx服务部署
作用: 处理用户的静态请求 html jpg txt mp4/avi

P --- php服务部署
作用:1. 处理动态的页面请求  2. 负责和数据库建立关系
M --- mysql服务部署 (yum会很慢 编译安装会报错) mariadb
作用:存储用户的字符串数据信息

03. 网站的LNMP架构部署

nginx服务 --- ansible一键化部署 修改yum源
mysql服务部署:
第一个历程: 安装数据库软件
yum install mariadb-server mariadb -y

补充: 数据库初始化过程 mysql_install_db
--basedir=path The path to the MariaDB installation directory.
指定mysql程序目录
--datadir=path The path to the MariaDB data directory.
指定数据信息保存的目录
--user=mysql 让mysql管理数据目录 700

信息输出内容:
创建数据库的密码信息:
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/application/mysql/bin/mysqladmin -u root password 'new-password' --- 给本地数据库设置密码
/application/mysql/bin/mysqladmin -u root -h web01 password 'new-password' --- 给远程数据库设置密码

第二个历程: 启动数据库服务
systemctl start mariadb.service
systemctl enable mariadb.service

第三个历程: 给mysql数据库服务设置密码
mysqladmin -u root password 'oldboy123' --- 设置密码
mysql -u root -poldboy123

PHP服务部署流程:
第一个历程: 更新yum源/卸载系统自带的PHP软件
yum remove php-mysql php php-fpm php-common
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

第二个历程: 安装php软件
yum install -y php71w php71w-cli php71w-common php71w-devel php71w-embedded php71w-gd php71w-mcrypt php71w-mbstring php71w-pdo php71w-xml php71w-fpm php71w-mysqlnd php71w-opcache php71w-pecl-memcached php71w-pecl-redis php71w-pecl-mongodb

第三个历程: 编写配置文件
vim /etc/php-fpm.d/www.conf
user = www     # nginx---www
group = www
PS: 保证nginx进程的管理用户和php服务进程的管理用户保持一致

第四个历程: 启动php服务
systemctl start php-fpm


04. LNMP架构的原理

用户访问网站--->nginx(fastcgi_pass) --FastCGI-->(php-fpm -- wrapper) php (php解析器) ---> mysql(读取或写入)

05. 实现LNMP之间建立关系

1. 实现nginx + php 建立关系
第一个历程: 编写nginx文件
编写nginx配置文件

[root@web01 html]# vim /usr/local/nginx-1.16/conf/nginx.conf

user nginx nginx;
worker_processes  2;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    server_tokens off;
    sendfile        on;
    keepalive_timeout  65;
    include /usr/local/nginx-1.16/conf.d/*.conf;
}

 [root@web01 html]# vim /usr/local/nginx-1.16/conf.d/server.conf 

server {
    listen       80;
    server_name  localhost;
    location / {
        root   html;
        index  index.html index.htm index.php;
        #autoindex on;  #开启nginx站点目录索引功能
    }
    location ~ .php$ {
        root    html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;  
        include        fastcgi_params;
    }
    location /nginx_status {
        stub_status on;
        access_log off;
        allow 10.192.27.0/25;
        deny all;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }
}

 SCRIPT_FILENAME 相当于 URL   $document_root$fastcgi_script_name相当于URI

 $document_root$fastcgi_script_name 相当于 /usr/local/nginx-1.16/html$fastcgi_script_name

重启nginx服务

[root@web01 html]# ps -ef | grep nginx
nginx      5979   5975  0 10月31 ?      00:00:00 php-fpm: pool www
nginx      5980   5975  0 10月31 ?      00:00:00 php-fpm: pool www
root      18148      1  0 14:21 ?        00:00:00 nginx: master process /usr/local/nginx-1.16/sbin/nginx -c /usr/local/nginx-1.16/conf/nginx.conf
nginx     18149  18148  0 14:21 ?        00:00:00 nginx: worker process
nginx     18150  18148  0 14:21 ?        00:00:00 nginx: worker process
root      18152  15483  0 14:21 pts/0    00:00:00 grep --color=auto nginx


第二个历程: 编写动态资源文件
[root@web01 conf.d]# cat  /usr/local/nginx-1.16/html/index.php

<?php
phpinfo();
?>

第三个历程: 进行访问测试
http://IP/


2. 实现php + mysql 建立关系
编写php代码文件
[root@web01 html]# vim test_mysql.php

<?php
    $servername = "localhost";
    $username = "root";
    $password = "xxxx";
    //$link_id=mysql_connect('主机名','用户','密码');
    //mysql -u用户 -p密码 -h 主机
    $conn = mysqli_connect($servername, $username, $password);
    if ($conn) {
        echo "mysql successful by root !
";
    }
    else{
        die("Connection failed: " . mysqli_connect_error());
    }
?>

06. 部署搭建网站页面(代码上线)

第一个历程: 获取代码信息(git)---使用开源的网站代码
www网站页面: http://www.dedecms.com/
bbs网站页面: http://www.discuz.net/forum.php
blog网站页面: https://cn.wordpress.org/
wecenter网站页面: http://www.wecenter.com/?copyright

第二个历程: 将代码解压,将解压后信息放入到站点目录中
tar xf wordpress-5.2.1.tar.gz
mv ...

第三个历程: 修改站点目录权限
chown -R nginx.nginx html

第四个历程: 进行网站页面初始化操作

第五个历程: 对数据库服务进行配置
创建数据库: create databases wordpress;
检查: show databases;
创建数据库管理用户: grant all on wordpress.* to 'wordpress'@'localhost' identified by 'xxxx';
检查: select user,host from mysql.user

第六个历程: 利用blog网站发布博文

以上内容概述

1. LNMP架构的组成部分
2. LNMP架构的部署安装
3. LNMP架构的工作原理
4. LNMP架构的服务如何建立关系
  nginx --- php 
  php --- mysql 
5. 利用LNMP架构搭建了一个网站

问题
01. 上传主题提示主题比较大    413 Request Entity Too Large --- nginx需要添加一个指令???
02. 图片保存在服务器什么位置了,如何将图片保存在存储服务器中
03. 数据库服务安装在web服务器本地有没有问题???
04. 如何将数据库数据迁移到指定服务器中

    1) 上传wordpress主题,报413错误,如何解决?
    总结:
    第一个历程: 修改nginx配置文件
    vim blog.conf
    server {
      client_max_body_size 50m;   --- 指定用户上传数据的大小限制(默认1M)
    }

    第二个历程: 修改php.ini配置文件
    upload_max_filesize = 50M     --- 使PHP接收用户上传的更大的数据(默认2M)
    
    2) 如何让LNMP架构和存储服务器建立关系
    第一个历程: 找出图片存储的目录
    01方法: 根据图片链接地址获取图片存储位置
    http://blog.oldboy.com  /wp-content/uploads/2019/05/meinv-1024x1024.jpg
        url                               uri
    02方法: 先定位数据存放在站点目录中
    find /html/blog -type f -mmin -5
    inotifywait -mrq /html/blog
    
    第二个历程: 使web服务器和存储服务器建立关系
    检查存储服务是否正常
    编写存储服务配置文件
    [root@nfs01 ~]# showmount -e 172.16.1.31
    Export list for 172.16.1.31:
    /data/bbs  172.16.1.0/24
    /data/www  172.16.1.0/24
    /data/blog 172.16.1.0/24
    mkdir /data/{bbs,blog,www}
    将web服务器blog存储的数据进行迁移
    mv /tmp/2019/ /html/blog/wp-content/uploads/

    默认存储服务器无法存储数据:
    管理用户无法存储: root_squash  --- nfsnobody
    普通用户无法存储: no_all_squash
    解决:
    第一个历程: 修改nfs配置文件,定义映射用户为www
    useradd www -u 1002
    chown -R www /data

    第二个历程: 使root用户可以上传数据
    sed -ri.bak 's#(sync)#1,anonuid=1002,anongid=1002#g' /etc/exports
    
    3) 如何让LNMP架构和数据库服务器建立关系???
    第一个历程: 将web服务器本地数据库数据进行备份
    mysqldump -uroot -poldboy123 --all-database >/tmp/web_back.sql

    第二个历程: 将备份数据进行迁移
    scp -rp /tmp/web_back.sql 172.16.1.51:/tmp
    
    第三个历程: 恢复数据信息
    yum install -y mariadb-server mariadb
    mysql -uroot -poldboy123 </tmp/web_back.sql
    
    第四个历程: 修改数据库服务器中数据库用户信息
    MariaDB [(none)]> select user,host from mysql.user;
    +-----------+-----------+
    | user      | host      |
    +-----------+-----------+
    | root      | 127.0.0.1 |
    | root      | ::1       |
    |           | localhost |
    | root      | localhost |
    | wordpress | localhost |
    |           | web01     |
    | root      | web01     |
    +-----------+-----------+
    7 rows in set (0.00 sec)
    
    优化: 删除无用的用户信息
    delete from mysql.user where user="" and host="localhost";
    delete from mysql.user where user="" and host="web01";
    
    添加: 添加新的用户信息
    grant all on wordpress.* to 'wordpress'@'172.16.1.%' identified by 'oldboy123';
    flush privileges;
    
    第五个历程: 修改web服务器代码文件信息
    vim wp-config.php
    /** MySQL hostname */
    define( 'DB_HOST', '172.16.1.51' );
    
    第六个历程: 停止web服务器上数据库服务
    ....

    问题01:
    数据库服务没有正确启动: Error establishing a database connection  连接不上3306端口
    问题02: 
    PHP服务没有开启,报502错误
    
    4) web01代码信息迁移到web02服务器,并且修改了网站域名无法正确访问
    访问新域名会自动跳转到老的域名
    方法一: 
    修改wordpres后台设置信息,将后台中老的域名改为新的域名
    方法二:
    修改数据库中的一个表, 在表中修改一个和域名有关的条目信息 (update phpmyadmin)
解决方法

参考:https://www.jianshu.com/p/06c7f17318a7

原文地址:https://www.cnblogs.com/linux985/p/11769672.html