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

1、源碼安裝(Linux+Nginx+MySQL+PHP)

 

MySQL5.7安裝   

一、刪除殘留的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/

 

二、下載源碼包

[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/php

 

三、編譯安裝

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

 

 四、配置文件詳細

#編輯systemd啓動文件mysql

[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

#添加環境變量linux

[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配置文件nginx

官網說:從5.7.18開始不在二進制包中提供my-default.cnf文件,再也不須要my.cnf。從源碼編譯安裝的mysql,基本配置在預編譯和systemd啓動文件中已定義好,不用my.cnf配置c++

五、初始化 無密碼 mysql 數據庫

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

-–initialize 會生成一個隨機密碼(保存在~/.mysql_secret),而 -–initialize-insecure 不會生成密碼。
-–datadir目標目錄下不能有數據文件web

[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]#

六、啓動mysql

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

七、密碼問題

初始化redis

查看默認root本地登陸密碼
若是初始化不是用空密碼  則初始化的數據庫的密碼在: [root@web01 mysql]# grep 'temporary password' /var/log/mysql.log (好像爲空)sql

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

 

八、防火牆開放mysql端口

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

 

 Nginx安裝

 見 http://www.javashuo.com/article/p-aijaqoni-mo.html

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

 


PHP安裝

一、經過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

二、下載並安裝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

 

三、配置文件

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 

 其它配置可根據本身的狀況進行相應修改

四、啓動php-ftm

完成以上配置後,便可啓動php-fpm,執行如下命令 : /usr/local/php7/sbin/php-fpm

啓動後,使用 ps aux | grep php-fpm 命令查看進程,若可以看到相關進程,則證實啓動成功。查詢進程時,能夠看到進程是以nginx用戶身份執行的

 

五、加入系統環境變量

使用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  #查看是否成功

 

六、設置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

 

2、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 !\n";
    }
    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

相關文章
相關標籤/搜索