1. 什么是 LNMP 架构
LNMP 是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写. L 指 Linux,N 指 Nginx,M 一般指 MySQL, 也可以指 MariaDB,P 一般指 PHP, 也可以指 Perl 或 Python
2.LNMP 架构是如何工作的
- location / {
- index index.PHP;
- }
- location ~ \.PHP$ {
- fastcgi_pass 127.0.0.1:9000;
- }
- location ~ \.(jpg|PNG|gif)$ {
- root /code/images;
- }
3.Nginx 与 PHP,MySQL 之间是如何工作的
nginx 通过 libevent 建立了网络连接池, 通过 nginx 的 fast_cgi 配置 (fastcgi_pass ip:port), 用 tcp 127.0.0.1:9000 方式连接 或者 unix socket unix:///var/run/unix.sock 方式 连接, PHP 与 MySQL 通过 PHP _ MySQL 扩展, 实现的 tcp 连接 (可以持久连接或非持久连接)*
4. 如何安装 LNMP 架构
1. 安装
- [[email protected] ~]# VIM /etc/yum.repos.d/PHP.repo
- [webtatic-PHP]
- name = PHP Repository
- baseurl = http://us-east.repo.webtatic.com/yum/el7/x86_64/
- gpgcheck = 0
- yum install nginx -y
- yum remove PHP-MySQL-5.4 PHP PHP-fpm PHP-common
- yum -y install 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
- yum install mariadb mariadb-server -y
2. 启动 nginx PHP-fpm
- systemctl start nginx
- systemctl start PHP-fpm
5.Nginx 与 PHP 集成的原理.
1. 编写能解析 PHP 的 Nginx 配置文件
- [[email protected] conf.d]# cat PHP.cheng.com.conf
- server {
- listen 80;
- server_name PHP.cheng.com;
- root /code;
- location / {
- index index.PHP;
- }
- location ~ \.PHP$ {
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- include fastcgi_params;
- }
- }
2. 检测语法:
[[email protected] conf.d]# nginx -t
3. 重启 nginx 服务:
[[email protected] conf.d]# systemctl restart nginx
4. 编写 PHP 代码, 测试访问效果.
- [[email protected] conf.d]# cat /code/info.PHP
- <?PHP
- phpinfo();
- ?>
5. 在本地做 host 劫持
6.PHP 与 MySQL 集成的原理.
1. 启动数据库
[[email protected] ~]# systemctl start mariadb
2. 配置连接密码
[[email protected] ~]# mysqladmin password 000000
3. 测试登录 MySQL
- [[email protected] ~]# MySQL -uroot -p000000
- MariaDB [(none)]>
4. 编写 PHP 连接数据库的代码
- [[email protected] conf.d]# cat /code/mysqli.PHP
- <?PHP
- $servername = "localhost";
- $username = "root";
- $password = "000000";
- // 创建连接
- $conn = mysqli_connect($servername,$username,$password);
- // 检测连接
- if(!$conn){
- die("Connection failed:".mysqli_connect_error());
- }
- echo "php 连接 MySQL 数据库成功";
- ?>
5. 可以直接使用 PHP 命令测试
[[email protected] ~]# PHP /code/mysqli.PHP
6. 也可以通过浏览器去测试
7. 通过 LNMP 架构部署 WordPress,Wecenter,edusoho,phpmyadmin,ecsho
1. WordPress 部署:
1. 编写 Nginx 集成 PHP 的配置文件 (定义域名以及站点的目录位置)
- [[email protected] conf.d]# cat Word.cheng.com.conf
- server {
- listen 80;
- server_name Word.cheng.com;
- root /code/WordPress;
- location / {
- index index.PHP;
- }
- location ~ \.PHP$ {
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- include fastcgi_params;
- }
- }
2. 检测语法:
[[email protected] conf.d]# nginx -t
3. 根据 Nginx 配置, 初始化环境, 然后上传代码
1. 准备站点目录
[[email protected] conf.d]# mkdir /code
2. 上传 WordPress 代码
- [[email protected] conf.d]# cd /code
- [[email protected] code]# tar xf WordPress-5.2.3-zh_CN.tar.gz
3. 创建数据库名
- [[email protected] code]# MySQL -uroot -p000000
- MariaDB [(none)]> create database WordPress;
- MariaDB [(none)]> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | MySQL |
- | performance_schema |
- | test |
- | WordPress |
- +--------------------+
- 5 rows in set (0.01 sec)
4. 统一 Nginx PHP 的权限 为 www
- [[email protected] code]# groupadd www -g 666
- [[email protected] code]# useradd -u666 -g666 www
- [[email protected] code]# chown -R www.www /code
5. 修改配置及重启 nginx PHP
- [[email protected] code]# sed -i '/^user/c user www;' /etc/nginx/nginx.conf
- [[email protected] code]# systemctl restart nginx
- [[email protected] code]# sed -i '/^user/c user = www' /etc/PHP-fpm.d/www.conf
- [[email protected] code]# sed -i '/^group/c group = www' /etc/PHP-fpm.d/www.conf
- [[email protected] code]# systemctl restart PHP-fpm
6. 域名解析
部署图解一:
2.Wecenter 部署:
1. 编写 Nginx 的配置文件
- [[email protected] zh]# cat /etc/nginx/conf.d/zh.cheng.com.conf
- server {
- listen 80;
- server_name zh.cheng.com;
- root /code/zh;
- client_max_body_size 100m;
- location / {
- index index.PHP;
- }
- location ~ \.PHP$ {
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- include fastcgi_params;
- }
- }
2. 上传代码, 变更代码的属主和属组
- [[email protected] conf.d]# mkdir /code/zh
- [[email protected] conf.d/zh]# rz WeCenter_3-3-2.zip
- [[email protected] conf.d/zh]# unzip WeCenter_3-3-2.zip
- [[email protected] code]# chown -R www.www /code
3. 登录数据库. 创建库名称
- [[email protected] code]# MySQL -uroot -p000000
- MariaDB [(none)]> create database zh;
- MariaDB [(none)]> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | MySQL |
- | performance_schema |
- | test |
- | WordPress |
- | zh |
- +--------------------+
- 6 rows in set (0.00 sec)
4. 重启 Nginx 服务
- [[email protected] code]# nginx -t
- [[email protected] code]# systemctl restart nginx
5. 配置域名解析
部署图解二:
3. 可道云网盘部署:
1. 部署 nginx 配置文件:
- [[email protected] kdy]# cat /etc/nginx/conf.d/kdy.cheng.com.conf
- server {
- listen 80;
- server_name kdy.cheng.com;
- root /code/kdy;
- client_max_body_size 100m;
- location \ {
- index index.PHP;
- }
- location ~ \.PHP$ {
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- include fastcgi_params;
- }
- }
2. 上传源代码文件, 更改权限
- [[email protected] conf.d]# mkdir /code/kdy
- [[email protected] conf.d]# cd /code/kdy/
- [[email protected] kdy]# rz kodexplorer4.40.zip
- [[email protected] kdy]# chown -R www.www /code/
3. 登录数据库. 创建库名称 [可以不用创建数据库]
- [[email protected] kdy]# MySQL -uroot -p000000
- MariaDB [(none)]> create database kdy;
- Query OK, 1 row affected (0.00 sec)
- MariaDB [(none)]> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | kdy |
- | MySQL |
- | performance_schema |
- | test |
- | WordPress |
- | zh |
- +--------------------+
- 7 rows in set (0.00 sec)
4. 重启 Nginx 服务
- [[email protected] kdy]# nginx -t
- [[email protected] kdy]# systemctl restart nginx
5. 配置域名解析
部署图解三:
4.edusoho 视频网站部署:
1. 编写 Nginx 的配置文件
- [[email protected] ~]# cat /etc/nginx/conf.d/edu.cheng.com.conf
- server {
- listen 80;
- server_name edu.cheng.com;
- root /code/edusoho/Web;
- location / {
- index App.PHP;
- try_files $uri @rewriteapp;
- }
- location @rewriteapp {
- rewrite ^(.*)$ /App.PHP/$1 last;
- }
- location ~ ^/udisk {
- internal;
- root /code/edusoho/App/data/;
- }
- location ~ ^/(App|app_dev)\.PHP(/|$) {
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_split_path_info ^(.+\.PHP)(/.*)$;
- include fastcgi_params;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- fastcgi_param HTTPS off;
- fastcgi_param HTTP_X-Sendfile-Type X-Accel-Redirect;
- fastcgi_param HTTP_X-Accel-Mapping /udisk=/code/edusoho/App/data/udisk;
- fastcgi_buffer_size 128k;
- fastcgi_buffers 8 128k;
- }
- # 配置设置图片格式文件
- location ~* \.(jpg|jpeg|gif|PNG|ico|swf)$ {
- # 过期时间为 3 年
- expires 3y;
- # 关闭日志记录
- access_log off;
- # 关闭 gzip 压缩, 减少 CPU 消耗, 因为图片的压缩率不高.
- gzip off;
- }
- # 配置 CSS/JS 文件
- location ~* \.(CSS|JS)$ {
- access_log off;
- expires 3y;
- }
- # 禁止用户上传目录下所有. PHP 文件的访问, 提高安全性
- location ~ ^/files/.*\.(PHP|php5)$ {
- deny all;
- }
- # 以下配置允许运行. PHP 的程序, 方便于其他第三方系统的集成.
- location ~ \.PHP$ {
- # [改] 请根据实际 PHP-fpm 运行的方式修改
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_split_path_info ^(.+\.PHP)(/.*)$;
- include fastcgi_params;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- fastcgi_param HTTPS off;
- }
- }
2. 上传产品源代码文件, 更改权限
- [[email protected] ~]# rz edusoho-8.3.43.zip
- [[email protected] ~]# chown -R www.www /code/
3. 登录数据库. 创建库名称 [可以不用创建数据库]
- [[email protected] ~]# MySQL -uroot -p000000
- MariaDB [(none)]> create database edusoho;
- MariaDB [(none)]> show databases;
- +--------------------+
- | Database |
- +--------------------+
- | information_schema |
- | edusoho |
- | MySQL |
- | performance_schema |
- | test |
- | WordPress |
- | zh |
- +--------------------+
- 7 rows in set (0.00 sec)
4. 重启 Nginx 服务
- [[email protected] ~]# nginx -t
- [[email protected] ~]# systemctl restart nginx
5. 配置域名解析
部署图解四:
来源: http://www.bubuko.com/infodetail-3204436.html