lnmp单机部署

lnmp的协同工作流程,与动静态处理的解释图

nginx和php协同工作的原理


1.用户发起http请求,请求到达LNMP中的nginx

2.nginx根据用户请求的url进行判断,通过location进行匹配

3.locaiton判断出url是静态请求,nginx直接查找静态资源,返回响应。

4.location判断出是动态请求,nginx反向代理转发给后端(这个架构里是fastcgi的php)程序

5.nginx通过fastcgi_pass模块把请求转发给php-fpm进程,php-fpm进行后续的处理
(php-fpm进程调用wrapper进程)

6.如果请求是xx.php文件,那么php解释器直接阅读程序,完成php脚本任务,然后返回结果。

7.如果请求需要调用数据库,php则会通过代码连接数据库,发起SQL操作。

8.最终数据库的内容返回流程。
mysql 
↓
php
↓
php-fpm
↓
fastcgi
↓
nginx
↓
http
↓
浏览器

部署nginx

1.创建nginx的运行环境,准备一个新的机器,去测试部署LNMP
groupadd www -g 666
useradd www -s /sbin/nologin -M -u 666 -g 666


2.设置nginx官网方库
cat > /etc/yum.repos.d/nginx.repo << 'EOF'
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
EOF



3.安装,启动nginx
yum clean all 
yum install nginx -y

systemctl start nginx
systemctl enable nginx

部署php7的安装

# 卸载旧php环境,可能会导致冲突
yum remove php-mysql-5.4 php php-fpm php-common -y

# 安装第三方epel源
# 其实就是在下载2个 yum的repo仓库文件
# 这俩都是用于给centos安装一些额外的rpm包的仓库。

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


# 如果这个命令执行失败,安装不了这个rpm怎么办?
# 这玩意,不就是一个https提供的一个静态资源吗?
你可以用N个办法,去下载这个资源,放到linux里面去 rpm安装

- 在window下,下载好了,放到linux里安装即可
[root@web-7 ~]#rpm -Uvh ./webtatic-release.rpm 
warning: ./webtatic-release.rpm: Header V4 RSA/SHA1 Signature, key ID 62e74ca5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:webtatic-release-7-3             ################################# [100%]

到这里成功安装执行1,失败执行2
1

安装php7版本的依赖
yum install -y 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 php71w-json php71w-pecl-apcu php71w-pecl-apcu-devel

2

自定义yum本地仓库

你不需要依赖外网的yum仓库,可能导致该仓库无法访问,下载软件失败。。
大公司,会自建yum仓库
防止出现网络问题,自建了yum仓库,本地yum仓库

# 先准备好了一个 lnmp所有的rpm包,以及他们需要的依赖
# 准备一个minomal最小化安装的机器,基于
# 只下载对应的软件包,以及依赖,到指定目录,rpm,但是不安装


yum install --downloadonly --downloaddir=/your_rpm/   xxxxxxx
比如:yum install --downloadonly --downloaddir=/root/nginx_rpm/ nginx




# 如果是本地的rpm包安装
#针对LNMP涉及的RPM包,rsync,nfs等

[root@web-7 /etc/yum.repos.d/local-rpm]#ls |wc -l
160


# 自建yum仓库
[root@web-7 ~]#yum install createrepo -y


[root@web-7 ~]#createrepo /etc/yum.repos.d/local-rpm/
Spawning worker 0 with 160 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete


[root@web-7 /etc/yum.repos.d]#
cat > /etc/yum.repos.d/local-rpm.repo  <<EOF
[local-rpm]
name=local yum repo
baseurl=file:///etc/yum.repos.d/local-rpm/
enabled=1
gpgcheck=0
EOF

# 最终的仓库源如下
# 我当前的yum源目录,只有一个repo文件,只读这个本地yum仓库

[root@web-7 /etc/yum.repos.d]#ls
local-rpm/  local-rpm.repo


# 指定仓库安装
# 安装仓库安装

yum install --enablerepo=local-rpm 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 php71w-json php71w-pecl-apcu php71w-pecl-apcu-devel

修改php的配置文件

修改php-fpm配置文件,修改php-fpm进程的运行用户,改为和nginx一致

[root@web-7 ~]#grep -E '^(user|group)' /etc/php-fpm.d/www.conf 
user = apache
group = apache


# 改为www用户
[root@web-7 ~]#sed -i '/^user/c user = www' /etc/php-fpm.d/www.conf 
[root@web-7 ~]#sed -i '/^group/c group = www' /etc/php-fpm.d/www.conf 
[root@web-7 ~]#grep -E '^(user|group)' /etc/php-fpm.d/www.conf 
user = www
group = www


# 尝试启动php进程
[root@web-7 ~]#systemctl start php-fpm
[root@web-7 ~]#
[root@web-7 ~]#systemctl enable php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
[root@web-7 ~]#

# 查看后端进程
[root@web-7 ~]#netstat -tunlp|grep php
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      9562/php-fpm: maste 

部署数据库(mariadb)

# 使用的是自建的yum仓库

# 在我这个机器上,我已经准备好自建的yum仓库

1.注意这个是来自于epel仓库的数据库
yum install mariadb-server mariadb -y

# 启动myqsl

什么是本地进程套接字文件



[root@web-7 ~]#systemctl start mariadb
[root@web-7 ~]#
[root@web-7 ~]## 该服务器,提供了2个连接方式,网络socket,本地进程套接字

[root@web-7 ~]#!net
netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      9562/php-fpm: maste 
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      10284/mysqld        
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      8325/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      900/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1155/master         
tcp6       0      0 :::22                   :::*                    LISTEN      900/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1155/master         


[root@web-7 ~]#ls /var/lib/mysql/mysql.sock -l
srwxrwxrwx 1 mysql mysql 0 May 26 15:10 /var/lib/mysql/mysql.sock
[root@web-7 ~]#

光速入门mysql的SQL语句

0.先登录试试 默认mysql提供了连接的方式

mysql  -u没有空格的用户名  -p没有空格的密码   -h没空格的主机地址

# 如下空密码,登录mysql

[root@web-7 ~]#mysql -uroot -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> 
# 退出mysql

MariaDB [(none)]> exit
Bye



1. 修改默认的密码,默认是空密码
# 这是改密码的完整命令,通过password指令,指定新密码

mysqladmin -uroot -p   password 'linux0224'
完毕之后,密码就已经被修改了,
此时账户密码是
root
linux0224


# 查看登录mysql的两个方式,1.基于网络socket连接
# -u, --user=name     User for login if not current user.
# -p, --password[=name] 
# -h, --host=name     Connect to host.
# -P, --port=#        Port number to use for connection

[root@web-7 ~]#mysql -uroot -plinux0224 -hlocalhost -P3306


# 基于本地套接字文件,无须指定ip:port了
# -S, --socket=name   The socket file to use for connection.
[root@web-7 ~]#mysql -uroot -plinux0224 -S/var/lib/mysql/mysql.sock


往后你学习高级的反向代理,也会用到这个socket文件(进程运行了,就有sock文件,进程挂了,sock文件也没了,通过这个sock文件,可以找到对应的进程。)

# 改完mysql密码后,登录,学习基本的SQL语句
# 默认你在本地执行mysql连接,无须指定地址信息了,默认就是本地的3306
mysql -uroot -plinux0224

# 查看数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

# 查看当前数据库的用户信息,如有多少个用户

# 切换进入数据库
MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> 


# 查看当前数据库下有多少张数据表
MariaDB [mysql]> show tables;
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| servers                   |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
24 rows in set (0.00 sec)


# 查看数据表的结构信息
MariaDB [mysql]> desc user;

# 查看表中,指定字段的信息
MariaDB [mysql]> select Host,User,Password from user;
+-----------+------+-------------------------------------------+
| Host      | User | Password                                  |
+-----------+------+-------------------------------------------+
| localhost | root | *8D45A1DAD603EDC2980B51D2C900E6BCFE6E13A8 |
| web-7     | root |                                           |
| 127.0.0.1 | root |                                           |
| ::1       | root |                                           |
| localhost |      |                                           |
| web-7     |      |                                           |
+-----------+------+-------------------------------------------+
6 rows in set (0.00 sec)

部署nignx代理php后端

部署nginx,能把请求转发给后端的php

nginx接收到关于php的动态请求,转发给php-fpm



需要用的参数是fastcgi_pass
语法如下

用法1,转发给ip:port形式
fastcgi_pass localhost:9000;

用法2,转发给unix socket本地进程套接字
fastcgi_pass unix:/tmp/fastcgi.socket;

# 语法看懂 1 不懂2



# 具体修改nginx配置如下

[root@web-7 /etc/nginx/conf.d]#cat php.conf 
server{
    listen 80;
    server_name www.linux0224.cc;
    # 动态请求处理
    # 遇见以.php结尾的请求就进入到这里的location
    location ~ \.php$ {
   		 # 如果要找具体的后端代码,在/code/去找
        root /code;
        # 会吧请求转发给后端的php-fpm进行加工
        fastcgi_pass 127.0.0.1:9000;
        # 默认的首页文件名字是index.php
        fastcgi_index index.php;
        # 转发的参数,
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}


准备php的测试代码,查看nginx是否和php可以正常结合工作

1.确保php-fpm进程存在
[root@web-7 /etc/nginx/conf.d]#netstat -tunlp|grep 9000
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      9562/php-fpm: maste 


2.进入刚才nginx中设置的,php的代码目录
mkdir -p /code
cd /code

chown -R www.www /code 

创建一个测试数据,创建phpinfo页面
cat > /code/test-phpinfo.php <<EOF
<?php
phpinfo();
echo "welcome to yuchaoit.cn"
?>
EOF


nginx转发请求给php的流程图

php的动静态请求处理
我们现在不是的是php解释型语言的网站

  1. 准备好静态资源如 gif,png,jpg等,nginx去处理
  2. php特点是,解释型语言,网站源码,说白了也是一堆文本文件!!!
  3. 这堆php代码文件,必须得交给php-fpm进程去读取!!!
转化为了nginx的配置
[root@web-7 /code]#cat /etc/nginx/conf.d/php.conf 
server{
    listen 80;
    server_name www.linux0224.cc;
	# 纯静态请求处理
	location ~* \.(jpg|gif|png)$ {
		root /www/static/;
	}
    # nginx直接吧所有的请求,全部甩给了php-fpm
    # 其他全部识别为动态请求,转发给php
    location /  {
    	# 你需要吧这个php源代码,放在这个目录中!!!
        root /code;
        fastcgi_pass 127.0.0.1:9000;
        # 默认找到的首页php代码文件,名字叫做index.php
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

# 准备一个静态数据
mkdir -p /www/static/
wget -O /www/static/caixukun.jpg  http://yuchaoit.cn/data/cai.jpg

# 准备一些动态数据
mkdir -p /code
cat > /code/index.php <<EOF
<?php
phpinfo();
echo "welcome to yuchaoit.cn"
?>
EOF

# 重启nginx
nginx -s reload


# 重启php-fpm(默认就是fastcgi协议了,不用你管,你只需要知道,在nginx哪里,设置为fastcgi_pass 127.0.0.1:9000;即可!!!)
systemctl restart php-fpm




www.linux0224.cc  # 直接看到php后端的代码文件

www.linux0224.cc/caixukun.jpg

后端的php-fpm默认就是基于fastcgi协议运行的了

配置文件路径

/etc/php-fpm.d/www.conf

后面,只需要,把各种php源代码,直接放到这个/code里面就完事了,

测试php连接mysql是否正常

[root@web-7 /code]#cat test-mysql.php 
<?php

    $server="127.0.0.1";
    $mysql_user="root";
    $mysql_pwd="linux0224";


    // 创建数据库连接
    $conn=mysqli_connect($server,$mysql_user,$mysql_pwd);

    // 检测连通性
    if($conn){
        echo "mysql successful by yuchaoit.cn \n";
    }else {
        die( "Connection failed:  "  .  mysqli_connect_error());
    }

?>

wecenter

第一个php产品,部署知识网站

1. 下载源代码
cd /code/wecenter && wget http://yuchaoit.cn/data/wecenter.zip

[root@web-7 /code/wecenter]#ls /code/wecenter/wecenter.zip 
/code/wecenter/wecenter.zip



2. 设置nginx代理的配置

1.  wecenter.linux0224.cn/index.php  会发给 9000端口的php-fpm
2.  wecenter.linux0224.cn/hello.html 就直接去 /code/wecenter找资源


# 重点是,这个网站,是前端段不分离的开发模式,前端代码,和php代码都放一块了。


[root@web-7 /etc/nginx/conf.d]#cat wecenter.conf 
server{
    listen 80;
    server_name wecenter.linux0224.cn;

    # 静态请求,资源存放路径
    # 这里是个简写除了以.php结尾的请求,都判断为是静态请求
    # 来/code/wecenter目录下找资源
    # 因为这个wecenter开源产品,他们的开发模式是,单体应用
    # 静态数据(前端内容) 和 后端数据(php代码文件)全部放在了一块
    # 当你去访问wecenter.linux0224.cn 
    root /code/wecenter;
    index index.php index.html;

    # 动态请求处理
    # 只有你访问的是 wecenter.linux0224.cn/xxxx.php 
    # 这个请求就会转发给9000的php-fpm去执行。
    location ~ \.php$ {

        root /code/wecenter;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}


# 准备好源代码数据,即可
[root@web-7 /code/wecenter]#ls /code/wecenter/
app            composer.lock  license.txt     README.md   tmp          views
cache          index.php      models          robots.txt  uploads      wecenter.zip
changelog.txt  install        nginx.htaccess  static      vendor
composer.json  language       plugins         system      version.php


# 去访问了,还得做好dns解析,以及服务忘记重启了
[root@web-7 /code/wecenter]#nginx -s reload

授权步骤
[root@web-7 /code/wecenter]#chown -R www.www /code/wecenter/



# 你得先创建好对应的数据库,才能写入数据。
MariaDB [(none)]> create database wecenter;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wecenter           |
+--------------------+
5 rows in set (0.00 sec)


查看库中有数据表吗
MariaDB [(none)]> 
MariaDB [(none)]> show tables from wecenter;
Empty set (0.00 sec)


wecenter安装成功后,检查数据表
MariaDB [(none)]> show tables from wecenter;

# 创建该系统的管理员
yuchao01
chaoge666
yc_uuu@163.com

# 删除安装的脚本,防止黑客恶意重置你的网站
[root@web-7 /code/wecenter]#cd /code/wecenter/install/
[root@web-7 /code/wecenter/install]#ls
db  index.php
[root@web-7 /code/wecenter/install]#rm -f index.php 




再次使用管理员账号登录。

最后一波,验证数据库中,是否有你创建的管理员密码信息
# 查看wecenter这个库下的,数据表,名字是 aws_users;
# 指定查看几个字段


[root@web-7 /code/wecenter/install]#!mysql
mysql -uroot -plinux0224
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 32
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]> select user_name,password from wecenter.aws_users;
+-----------+----------------------------------+
| user_name | password                         |
+-----------+----------------------------------+
| yuchao01  | d0373d14bedca3ae9f1534d9b8772d54 |
+-----------+----------------------------------+
1 row in set (0.00 sec)

MariaDB [(none)]> 

最后一步的环境检查。,确保权限都是小绿勾。

wordpress的部署

1. 下载源代码
确保如下代码存在即可
[root@web-7 /code/wordpress]#ls /code/wordpress/
index.php                  wp-admin              wp-includes        wp-signup.php
license.txt                wp-blog-header.php    wp-links-opml.php  wp-trackback.php
readme.html                wp-comments-post.php  wp-load.php        xmlrpc.php
wordpress                  wp-config-sample.php  wp-login.php
wordpress-5.9.3-zh_CN.zip  wp-content            wp-mail.php
wp-activate.php            wp-cron.php           wp-settings.php



[root@web-7 /code/wordpress]#ls /code/wordpress/index.php 
/code/wordpress/index.php




2. 准备nginx的反向代理配置文件,转发请求给php
1.创建虚拟主机文件
server{
    listen 80;
    server_name wordpress.linux0224.cn;

    # 静态请求,资源存放路径
    root /code/wordpress;
    index index.php index.html;

    # 动态请求处理
    #
    location ~ \.php$ {

        root /code/wordpress;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

2.重启nginx
[root@web-7 /etc/nginx/conf.d]#nginx -s reload



[root@web-7 /etc/nginx/conf.d]#chown -R www.www /code/wordpress


创建数据库
mysql -uroot -plinux0224
MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)

下一步,访问即可

注意,做好dns解析
10.0.0.7  wecenter.linux0224.cn wordpress.linux0224.cn

开始安装wordpres,填写你的数据库信息

yuchao01
chaoge666
yc_uuu@163.com

发布一篇博客,看看对吗

去数据库看看这个博客数据在吗

MariaDB [(none)]> 
MariaDB [(none)]> use wordpress;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [wordpress]> show tables;
+-----------------------+
| Tables_in_wordpress   |
+-----------------------+
| wp_commentmeta        |
| wp_comments           |
| wp_links              |
| wp_options            |
| wp_postmeta           |
| wp_posts              |
| wp_term_relationships |
| wp_term_taxonomy      |
| wp_termmeta           |
| wp_terms              |
| wp_usermeta           |
| wp_users              |
+-----------------------+
12 rows in set (0.00 sec)


MariaDB [wordpress]> desc wp_posts;

MariaDB [wordpress]> 
MariaDB [wordpress]> select post_content from wp_posts;
posted @ 2024-03-24 16:38  不太聪明的大鹅  阅读(3)  评论(0编辑  收藏  举报