之前学习Apache了解过LAMP(Linux、Apache、Mysql、Perl/PHP/Python)
由于上述架构,已然是一个商业版软件的代名词,后来自由软件趋势兴起,如何用开源软件替代商业软件,这是人们追求的,所有的Linux发行版几乎都会内置这样的软件。
但是由于Nginx的兴趣,卓越的性能,更简单的配置,更强大的功能,更低的资源消耗,LAMP已经渐渐被LNMP取代。
image.png
LNMP是网站架构初期最合适的单体架构。因为初创型技术团队对于技术的选型,需要考虑如下因素

  1. 在创业初期,研发资源有限,研发人力有限,技术储备有限,需要选择一个易维护、简单的技术架构;
  2. 产品需要快速研发上线,并能够满足快速迭代要求,现实情况决定了一开始没有时间和精力来选择一个过于复杂的分布式架构系统,研发速度必须要快;
  3. 创业初期,业务复杂度比较低,业务量也比较小,如果选择过于复杂的架构,反而会增加研发难度以及运维难度;
  4. 遵从选择合适的技术而不是最好的技术原则,并权衡研发效率和产品目标,同时创业初期只有一个PHP研发人员,过于复杂的技术架构必然会带来比较高昂的学习成本。

基于如上的因素,LNMP架构就是最合适的。
image.png
如此架构,一般三台服务器足以,Nginx与后台系统部署在一台机器,Mysql数据库单独服务器,Mencached缓存一台服务器。这样的架构优势在于

  • 单体架构,架构简单,清晰的分层结构;
  • 可以快速研发,满足产品快速迭代要求;
  • 没有复杂的技术,技术学习成本低,同时运维成本低,无需专业的运维,节省开支。

    LNMP组合工作流程

    LNMP工作流是用户通过浏览器输入域名访问Nginx web服务,Nginx判断请求是静态请求则由Nginx返回给用户。如果是动态请求(如.php结尾),那么Nginx会将该请求通过FastCGI接口发送给PHP引擎(php-fpm进程)进行解析,如果该动态请求需要读取mysql数据库,php会继续向后读取数据库,最终Nginx将获取的数据返回给用户。
  1. 用户通过浏览器输入域名访问到nginx web服务器
  2. nginx进行用户请求判断
    1. 静态请求 nginx直接相应给用户
    2. 动态请求

image.png

Nginx安装配置

  1. 1.安装nginx所需的pcre库,让nginx支持url重写的rewrite功能
  2. yum install pcre pcre-devel -y
  3. 2.安装openssl-devel模块,nginx需要支持https
  4. [root@web01 opt]# yum install openssl openssl-devel -y
  5. 2.1 安装gcc编译器
  6. yum install gcc -y
  7. 3.下载nginx源码包
  8. [root@web01 opt]# mkdir -p /home/chaoge/tools
  9. [root@web01 opt]# cd /home/chaoge/tools/
  10. [root@web01 tools]# wget http://nginx.org/download/nginx-1.16.0.tar.gz
  11. [root@web01 tools]# echo $?
  12. 0
  13. 4.创建nginx普通用户
  14. [root@web01 tools]# useradd nginx -u 1111 -s /sbin/nologin -M
  15. 5.开始解压缩编译nginx
  16. [root@web01 tools]# tar -zxf nginx-1.16.0.tar.gz
  17. [root@web01 nginx-1.16.0]# ./configure --user=nginx --group=nginx --prefix=/opt/nginx-1.16.0/ --with-http_stub_status_module --with-http_ssl_module
  18. [root@web01 nginx-1.16.0]# echo $?
  19. 0
  20. [root@web01 nginx-1.16.0]# make && make install
  21. [root@web01 nginx-1.16.0]# echo $?
  22. 0
  23. 6.配置软连接,生产环境常用操作,便于运维、开发、测试使用,以及nginx以后的升级
  24. [root@web01 nginx-1.16.0]# ln -s /opt/nginx-1.16.0/ /opt/nginx
  25. [root@web01 nginx-1.16.0]# ll /opt/
  26. 总用量 0
  27. lrwxrwxrwx 1 root root 18 3 18 22:45 nginx -> /opt/nginx-1.16.0/
  28. drwxr-xr-x 6 root root 54 3 18 22:43 nginx-1.16.0
  29. 7.配置nginx环境变量
  30. [root@web01 ~]# tail -1 /etc/profile
  31. export PATH="/opt/nginx/sbin:/opt/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
  32. [root@web01 ~]#
  33. [root@web01 ~]# echo $PATH
  34. /opt/nginx/sbin:/opt/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin

LNMP之MySQL数据库

Mysql是互联网领域非常重要的,深受广大用户欢迎的一款开源数据库。MysSQL是一款关系型数据库,且把数据保存在不同的二维表,且把数据表再放入数据库中管理,而不是所有的数据统一放在一个大仓库,这样的设计提高MySQL的读写速度。
我们会在后面章节专门学习MYSQL,进阶DBA

安装Mysql

数据库——大的文件夹
数据表——excel表格
安装方式我们可以选择多种

  • Yum/rpm包安装,简单、快速、无法定制化、新手推荐使用
  • 二进制安装,解压缩后直接简单配置即可使用,速度较快,专业DBA常用
  • 源码编译安装,特点是可以定制化安装需求,缺点过程较为复杂 ```shell 1.创建mysql用户 [root@web01 ~]# useradd -s /sbin/nologin mysql [root@web01 ~]# id mysql uid=1000(mysql) gid=1000(mysql) 组=1000(mysql)

2.下载mysql二进制软件包,提前配置好yum源,下载wget命令 [root@web01 ~]# yum install wget -y wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.re wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

[root@web01 ~]# mkdir -p /home/mysql/tools [root@web01 ~]# cd /home/mysql/tools/

该mysql文件600M左右,下载时间看网速

[root@web01 tools]# wget http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz

wget https://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz

  1. 【软件包选择】2103227582
  2. ```shell
  3. mysql二进制安装包体积较大,名字和源代码包有些区别,但是安装过程较快
  4. mysql二进制包 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz 615M
  5. mysql源码包 mysql-5.7.26.tar.gz 52M

二进制方式安装mysql

我们这里搭建LNMP环境,nginx和mysql是安装在一台机器上的,当然也可以是分开在不同的服务器上

  1. 1.解压并且移动mysql二进制软件包路径
  2. [root@web01 tools]# tar -zvxf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
  3. [root@web01 tools]# mv mysql-5.7.26-linux-glibc2.12-x86_64 /opt/mysql-5.7.26
  4. 2.生成软连接
  5. [root@web01 opt]# ln -s /opt/mysql-5.7.26/ /opt/mysql
  6. [root@web01 opt]# ls -l /opt/
  7. 总用量 0
  8. lrwxrwxrwx 1 root root 18 3 18 10:15 mysql -> /opt/mysql-5.7.26/
  9. drwxr-xr-x 9 root root 129 3 18 10:13 mysql-5.7.26
  10. 3.卸载centos7自带的mariadb库,防止冲突
  11. [root@web01 mysql]# rpm -e --nodeps mariadb-libs
  12. 4.手动创建mysql配置文件 vim /etc/my.cnf
  13. [mysqld] 是区段的含义,以下的参数对其生效
  14. [mysqld] 对服务端生效的参数
  15. [mysql] 对客户端生效的参数
  16. [root@web01 mysql]# cat /etc/my.cnf
  17. [mysqld]
  18. # 基础文件夹
  19. basedir=/opt/mysql/
  20. # mysql产生的相关数据放在这个目录下
  21. datadir=/opt/mysql/data
  22. # 进程启动之后产生的socket
  23. socket=/tmp/mysql.sock
  24. server_id=1
  25. # 数据库启动之后的默认端口
  26. port=3306
  27. log_error=/opt/mysql/data/mysql_err.log
  28. [mysql]
  29. socket=/tmp/mysql.sock

初始mysql数据库文件

  1. 1.卸载系统自带的centos7 mariadb-libs,且安装mysql的依赖环境
  2. rpm -qa mariadb-libs #检查是否存在
  3. [root@web01 mysql]# yum install libaio-devel -y
  4. 2.创建mysql数据文件夹且授权
  5. [root@web01 mysql]# mkdir -p /opt/mysql/data
  6. [root@web01 mysql]# chown -R mysql.mysql /opt/mysql/
  7. 3.初始化数据库
  8. [root@web01 mysql]# /opt/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/opt/mysql/ --datadir=/opt/mysql/data/
  9. # 参数解释
  10. --user=mysql 指定用户
  11. --basedir 指定mysql安装目录
  12. --datadir=/opt/mysql/data 指定数据文件夹
  13. --initialize-insecure 关闭mysql安全策略
  14. --initialize 开启mysql安全模式

配置mysql客户端

  1. 1.配置mysql启动脚本,定义mysqld.service,脚本如下
  2. [root@web01 mysql]# cat /etc/systemd/system/mysqld.service
  3. [Unit]
  4. Description=MySQL server by chaoge
  5. Documentation=man:mysqld(8)
  6. Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
  7. After=network.target
  8. After=syslog.target
  9. [Install]
  10. WantedBy=multi-user.target
  11. [Service]
  12. User=mysql
  13. Group=mysql
  14. ExecStart=/opt/mysql/bin/mysqld --defaults-file=/etc/my.cnf
  15. LimitNOFILE=5000

启动mysql数据库

  1. [root@web01 mysql]# systemctl start mysqld
  2. [root@web01 mysql]# systemctl enable mysqld
  3. Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /etc/systemd/system/mysqld.service.
  4. [root@web01 mysql]# systemctl status mysqld
  5. mysqld.service - MySQL server by chaoge
  6. Loaded: loaded (/etc/systemd/system/mysqld.service; disabled; vendor preset: disabled)
  7. Active: active (running) since 2020-03-18 11:30:01 EDT; 6s ago
  8. Docs: man:mysqld(8)
  9. http://dev.mysql.com/doc/refman/en/using-systemd.html
  10. Main PID: 5315 (mysqld)
  11. CGroup: /system.slice/mysqld.service
  12. └─5315 /opt/mysql/bin/mysqld --defaults-file=/etc/my.cnf
  13. 3 18 11:30:01 web01 systemd[1]: Started MySQL server by chaoge.
  14. 3 18 11:30:01 web01 systemd[1]: Starting MySQL server by chaoge...

检查mysql启动状态

  1. [root@web01 mysql]# netstat -tunlp|grep mysql
  2. tcp6 0 0 :::3306 :::* LISTEN 5315/mysqld
  3. [root@web01 mysql]# ps -ef|grep mysql |grep -v grep
  4. mysql 5315 1 0 11:30 ? 00:00:00 /opt/mysql/bin/mysqld --defaults-file=/etc/my.cnf

配置mysql命令环境变量

[root@web01 mysql]# echo "export PATH=/opt/mysql/bin:$PATH" >> /etc/profile
[root@web01 mysql]# source /etc/profile
[root@web01 mysql]# echo $PATH
/opt/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

登录mysql

默认无须输入密码直接进入mysql数据库,且身份是root

[root@web01 ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.02 sec)

mysql> select user();            # 查看当前登录的用户
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

# 查看mysql所有用户信息
mysql> select user,authentication_string,host from mysql.user;
+---------------+-------------------------------------------+-----------+
| user          | authentication_string                     | host      |
+---------------+-------------------------------------------+-----------+
| root          |                                           | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost |
| mysql.sys     | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost |
+---------------+-------------------------------------------+-----------+
3 rows in set (0.01 sec)

默认mysql账号root是没有密码的,我们给其设置密码,加大安全性


[root@web01 ~]# mysqladmin -uroot password 'chaoge666'
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

# 再次登录,输入密码
[root@web01 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.26 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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>

具体mysql知识,跟着后期mysql精讲课程学习

FastCGI介绍

CGI(Common Gateway Interface),全文名是通用网关接口
用于HTTP服务器和其他机器通信的一种工具。
image.png
传统CGI程序性能较弱,因此每次HTTP服务器遇到动态程序的时候,都需要重启解析器来执行解析,之后的处理结果才会返回给HTTP服务器。这样在高并发场景下访问几乎是太差劲了,因此诞生了FastCGI。

FastCGI是一个可伸缩、高速的在HTTP服务器和动态脚本之间通信的接口(在Linux环境下,FastCGI接口就是socket,这个socket可以是文件socket,也可以是IP socket,也就意味着本地通信,远程通信两种),主要优点是把动态语言和HTTP服务器分离开。

  1. 如果http服务器和后端程序运行在两台服务器上,这个fastcgi通信形式就是IP+端口的形式
  2. 如果http服务器和后端程序运行在同一台服务器上,那么socket可以是本地通信

最好是运行在两台机器上。
多数主流的web服务器都支持FastCGI,如Apache、nginx、LightHttpd等
同时FastCGI也被许多脚本语言所支持,比较流行的脚本语言之一为PHP。
Fast-CGI接口采用c/s架构,可以将HTTP服务器和脚本解析服务器分离开。

  1. 当HTTP服务器遇见静态请求,直接返回,
  2. 遇见动态请求转发给动态服务器,交给FastCGI去处理,实现动静分离,提升服务器性能。

image.png
当HTTP服务器发送一个动态请求给FastCGI。


当进来一个请求时,Web 服务器把环境变量和这个页面请求通过一个unix domain socket(都位于同一物理服务器)或者一个IP Socket(FastCGI部署在其它物理服务器)传递给FastCGI进程。
image.png

Nginx FastCGI的运行原理

Nginx默认不支持外部动态程序直接解析,所有的外部程序都得通过FastCGI接口调用。FastCGI接口运行在LInux平台默认是socket进程通信,为了调用CGI程序,还需要FastCGI的wrapper(启动cgi的程序),这个wrapper绑定在某个固定的socket上,例如端口或者文件socket都行。
当Nginx把CGI请求发送给该socket,通过FastCGI接口wrapper接收到请求,派生出一个新的线程,这个线程调用解释器或者外部程序处理脚本来读取返回的数据;
wrapper再吧返回的数据通过FastCGI接口沿着固定的socket传递给Nginx;
最后Nginx把返回的数据交给客户端。
image.png

FastCGI部署(本地部署)

1.检查Nginx和mysql的安装路径

[root@web01 ~]# ll /opt/
总用量 0
lrwxrwxrwx  1 root  root   18 3月  18 10:15 mysql -> /opt/mysql-5.7.26/
drwxr-xr-x 10 mysql mysql 213 3月  18 10:47 mysql-5.7.26
lrwxrwxrwx  1 root  root   18 3月  18 22:45 nginx -> /opt/nginx-1.16.0/
drwxr-xr-x  6 root  root   54 3月  18 22:43 nginx-1.16.0

2.保证nginx、mysql都启动了

[root@web01 ~]# netstat -tunlp|grep -E "nginx|mysql"
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      17214/nginx: master
tcp6       0      0 :::3306                 :::*                    LISTEN      5315/mysqld

3.安装部署PHP程序所需的系统库,不要求必须安装,而是安装上之后,可以扩展php更多功能

yum install  gcc gcc-c++ make zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel \
freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel libxslt-devel -y

默认yum源中缺少libiconv-devel软件包,需要编译安装,用于php的编码转换

[root@web01 ~]# wget -P /home/chaoge/tools/  http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.15.tar.gz
[root@web01 ~]# cd /home/chaoge/tools/
[root@web01 tools]# ls
libiconv-1.15.tar.gz  nginx-1.16.0  nginx-1.16.0.tar.gz
[root@web01 tools]# tar zxf libiconv-1.15.tar.gz
[root@web01 libiconv-1.15]# cd libiconv-1.15
[root@web01 libiconv-1.15]# ./configure --prefix=/opt/libiconv
[root@web01 libiconv-1.15]# make && make install

安装PHP(FastCGI形式)

1.下载获取php软件包
[root@web01 tools]# wget http://mirrors.sohu.com/php/php-7.3.5.tar.gz
2.解压缩php源码包,编译安装
[root@web01 tools]# tar -zxvf php-7.3.5.tar.gz
[root@web01 tools]# cd php-7.3.5
[root@web01 php-7.3.5]#

./configure --prefix=/opt/php7.3.5 \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv-dir=/opt/libiconv \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-fpm \
--enable-mbstring \
--with-gd \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--enable-short-tags \
--enable-static \
--with-xsl \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-ftp \
--enable-opcache=no

待看到如下显示,表示正确的编译了php
image.png
对于如上的参数,根据自己实际工作环境优化增删即可
部分参数解释

--prefix=  指定php安装路径
--enable-mysqlnd 使用php自带的mysql相关软件包
--with-fpm-user=nginx  指定PHP-FPM程序的用户是nginx,和nginx服务保持统一
--enable-fpm 激活php-fpm方式,以FastCGI形式运行php程序
3.在执行完编译脚本文件后,开始执行编译安装
[root@web01 php-7.3.5]# make && make install

看到如此的画面,表示php正确安装结束了,可以用特殊变量验证

[root@web01 php-7.3.5]# echo $?
0

image.png
【编译安装结束后,配置环境变量】

[root@web01 php-7.3.5]# ln -s /opt/php7.3.5/ /opt/php

php配置文件

配置文件路径

[root@web01 php-7.3.5]# ls php.ini*
php.ini-development  php.ini-production

俩配置文件,分别默认用于开发环境,生成环境,配置参数有所不同

# 可以用如下命令对比文件区别
[root@web01 php-7.3.5]# vimdiff php.ini-development php.ini-production

开发环境下开起了更多的日志、调试信息,生产环境该参数都关闭了

拷贝php配置文件到php默认目录,且改名

[root@web01 php-7.3.5]# cp php.ini-development /opt/php/lib/php.ini

FastCGI的配置文件

1.默认FastCGI的配置文件路径
[root@web01 etc]# pwd
/opt/php/etc
[root@web01 etc]# ls
pear.conf  php-fpm.conf.default  php-fpm.d

2.生成2个php-frpm的配置文件,先用默认配置,后续可以再后话
[root@web01 etc]# cp php-fpm.conf.default php-fpm.conf
[root@web01 etc]# cp php-fpm.d/www.conf.default php-fpm.d/www.conf

启动PHP服务(FastCGI模式)

# 启动服务,并且检查状态
[root@web01 etc]# /opt/php/sbin/php-fpm
[root@web01 etc]# netstat -tunlp|grep php
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      109647/php-fpm: mas

修改Nginx支持PHP

1.修改nginx配置文件,在最底行添加 包含文件参数,建议删除nginx.conf原有的server配置
[root@web01 conf]# vim /opt/nginx/conf/nginx.conf

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    gzip  on;
include extra/01_www.conf;
include extra/02_bbs.conf;
include extra/03_blog.conf;
include extra/04_status.conf;
}

【配置PHP的解析配置文件】

[root@web01 conf]# mkdir extra
[root@web01 conf]# vim extra/03_blog.conf
[root@web01 conf]#
[root@web01 conf]#
[root@web01 conf]# cat extra/03_blog.conf
server {
listen 80;
server_name blog.chaoge.com;
location / {
    root html/blog;
    index index.html;
}

#添加有关php程序的解析
location ~ .*\.(php|php5)?$ {
    root html/blog;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi.conf;
}


}

检查且启动nginx

[root@web01 conf]# touch extra/01_www.conf
[root@web01 conf]# touch extra/02_bbs.conf
[root@web01 conf]# touch extra/04_status.conf
[root@web01 conf]# nginx -t
nginx: the configuration file /opt/nginx-1.16.0//conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx-1.16.0//conf/nginx.conf test is successful

测试LNMP环境

[root@web01 conf]# mkdir -p /opt/nginx/html/blog
[root@web01 conf]# echo "<?php phpinfo(); ?>" > ../html/blog/test_info.php
[root@web01 conf]#

当看见如下页面,表示lnmp环境已经能够正确解析了
image.png
测试正确后,保证服务器安全,就得删除该test_info.php文件了

测试php访问mysql

php的代码,直接在网页中解析即可

[root@web01 conf]# cat ../html/blog/test_mysql.php

<?php
$link_id=mysqli_connect('localhost','root','chaoge666') or mysql_error();
if($link_id){

    echo "mysql successful by chaoge.\n";
}else {

    echo mysql_error();
}
?>


-----
如上脚本的中文解释
1.建立mysql连接,把连接信息复制为变量  link_id
2.如果link_id为真,那么就打印一串字符串信息,
3.否则输出报错信息,调试程序

image.png
看到如此的画面,lnmp环境基本搭建完毕,nginx+php+mysql的形式。

LNMP远程部署

上述php-fpm和nginx在一起,这里的讲解,是php-fpm和nginx在两台服务器,远程访问
参考博客

https://xuchen.wang/archives/nginxphp.html

还有注意,修改php-fpm的启动地址,改为0.0.0.0:9000
配置文件夹在安装目录的etc/php-fpm.d/下面的已conf为后缀的文件,一般在安装时我们设置成了www.conf。
把 listen = 127.0.0.1:9000 后面的端口号9000换成你需要的端口,然后重启php

image.png

LNMP部署开源博客

image.png
利用开源工具wordpress可以搭建出精美的博客站点,它是一套利用php语言和mysql数据库开发的程序,用于可以在支持php环境和mysql数据库的服务器上搭建blog站点。
你只需要掌握LNMP环境搭建,即可轻松部署wordpress程序。

LNMP环境准备

1.登录数据库创建用于存储数据库的wordpress库,存储博客数据
mysql> create database wordpress;
Query OK, 1 row affected (0.02 sec)

mysql> show databases like 'wordpress';
+----------------------+
| Database (wordpress) |
+----------------------+
| wordpress            |
+----------------------+
1 row in set (0.01 sec)

2.创建专用于wordpress的数据库用户
mysql> create  user  wordpress;

# 给wordpress用户授权,允许在localhost本地登录mysql,且有增删改查权限,设置密码为123456
mysql> grant all on wordpress.* to wordpress@'localhost' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.01 sec)

3.刷新权限表
mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)

4.查询用户信息
mysql> select user,authentication_string,host from mysql.user;
+---------------+-------------------------------------------+-----------+
| user          | authentication_string                     | host      |
+---------------+-------------------------------------------+-----------+
| root          | *83F7A15725AF362EF5EAFC16E1F3F97FDAB9B411 | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost |
| mysql.sys     | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost |
| wordpress     | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | localhost |
+---------------+-------------------------------------------+-----------+
4 rows in set (0.00 sec)

5.nginx配置,选择blog虚拟主机配置,添加一个index.php参数
[root@web01 conf]# cat extra/03_blog.conf
server {
listen 80;
server_name _;
location / {
    root html/blog;
    index index.php index.html;  #在这里新增
}

#添加有关php程序的解析
location ~ .*\.(php|php5)?$ {
    root html/blog;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi.conf;
}


}

Wordpress博客程序准备

1.下载获取wordpress程序
wget https://wordpress.org/latest.zip
wget https://wordpress.org/latest.tar.gz

2.超哥机器上的资料
[root@web01 conf]# ls /home/chaoge/tools/wordpress-5.3.2.*
/home/chaoge/tools/wordpress-5.3.2.tar.gz  /home/chaoge/tools/wordpress-5.3.2.zip

3.解压缩zip版代码
[root@web01 tools]# yum install unzip -y
[root@web01 tools]# unzip wordpress-5.3.2.zip
[root@web01 tools]# mv wordpress /opt/nginx/html/blog/
[root@web01 tools]# cd /opt/nginx/html/blog/
[root@web01 blog]# ls
test_mysql.php  wordpress

4.移动worpress代码到nginx目录下,且授权
[root@localhost tools]# mv wordpress   /opt/nginx/html/myphp/

[root@web01 blog]# mv wordpress/* .
[root@web01 blog]# ls
index.php       wordpress           wp-comments-post.php  wp-includes        wp-mail.php       xmlrpc.php
license.txt     wp-activate.php     wp-config-sample.php  wp-links-opml.php  wp-settings.php
readme.html     wp-admin            wp-content            wp-load.php        wp-signup.php
test_mysql.php  wp-blog-header.php  wp-cron.php           wp-login.php       wp-trackback.php
[root@web01 blog]#
[root@web01 blog]#
[root@web01 blog]# chown -R nginx.nginx ../blog/

5.最后一步,注意别忘记重启nginx
nginx -s reload

访问安装wordpress

image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png

至此博客站点就搭建好了,同学们可自行下载中文版wordpress,自由搭建博客了