LNMP:目前成熟的企业网站的应用模式之一,指的是一套协作工作的系统和相关文件
能够提供静态页面服务,也可以提供动态web服务。
这是一个缩写
L linux系统,操作系统。
N nginx网站服务,前端,提供前端的静态页面服务。同时具有代理,转发的作用。
转发后端请求。转发到PHP。nginx没有处理动态资源的功能,他有可以支持转发动态请求的模块
M MySQL 关系型数据库,保存用户的账号和密码。
P PHP 动态页面的编程语音,负责解释动态网页文件,和nginx以及数据库协同工作。
PHP的主要作用就是web开发,前端开发。

编译安装:
就是用二进制包,进行安装。
yum安装只是安装了基础的服务模块,其他的模块要想使用,用户要手动重新添加。
编译安装可以自由的指定需要的模块。
企业中:nginx MySQL 都是编译安装。
编译安装的优点
1、具有较大的自由度,功能可定制
2、可及时获得最新的软件版本
3、普遍适用于大多数Linux版本,便于一直使用
安装nginx:
安装依赖环境:
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
yum -y install pcre-devel zlib-devel gcc gcc-c++ make


把相应的包拖进来

解包之后,要在配置文件里面改才生效
编译安装一下


编译完之后,创建一个nginx的程序用户
useradd -M -s /sbin/nologin nginx

指定cpu的核心数,安装会快一点
make -j 4 && make install


装好之后的程序本身在 /usr/local/nginx/
优化一下路径,给程序建立一个软连接

添加到系统服务当中



给它一个权限,并查看语法和配置文件是否配置正确

systemctl start nginx.service
最后重启一下服务
安装数据库服务:
安装依赖环境
yum -y install \
ncurses \
ncurses-devel \
bison \
cmake



装好之后同样创建一个运行(程序)用户

编译安装
解包

在MySQL包里进行操作

配置数据库

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
#指定mysql的安装路径
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
#指定mysql进程监听套接字文件(数据库连接文件)的存储路径
-DSYSCONFDIR=/etc \
#指定配置文件的存储路径
-DSYSTEMD_PID_DIR=/usr/local/mysql \
#指定进程文件的存储路径
-DDEFAULT_CHARSET=utf8 \
#指定默认使用的字符集编码,如utf8
-DDEFAULT_COLLATION=utf8_general_ci \
#指定默认使用的字符集校对规则
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
#安装INNOBASE存储引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
#安装ARCHIVE存储引擎
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
#安装BLACKHOLE存储引擎
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
#安装FEDERATED存储引擎
-DMYSQL_DATADIR=/usr/local/mysql/data \
#指定数据库文件的存储路径
-DWITH_BOOST=boost \
#指定boost的路径,
-DWITH_SYSTEMD=1
#生成便于systemctl管理的文件

装数据库之前先清一下内存,否则内存不足会报错

完成之后,开始安装



修改mysql 配置文件

[client]
port = 3306
socket=/usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES



在[client]节中:
port: 客户端连接MySQL服务器使用的端口号,默认为3306。
socket: 客户端连接MySQL服务器使用的Unix socket文件路径,默认为/tmp/mysql.sock。
在该配置文件中指定的路径是/usr/local/mysql/mysql.sock。
在[mysqld]节中:
user: MySQL服务器运行时的用户名,一般设置为mysql。
basedir: MySQL安装目录的路径。
datadir: 存放MySQL数据文件的路径。
port: MySQL服务器监听的端口号,默认为3306。
character-set-server: MySQL服务器使用的默认字符集,这里设置为UTF-8编码(utf8)。
pid-file: 保存MySQL服务器进程ID的文件路径,默认为/var/run/mysqld/mysqld.pid。在该配置文件中指定的路径是/usr/local/mysql/mysqld.pid。
socket: MySQL服务器监听的Unix socket文件路径,默认为/tmp/mysql.sock。在该配置文件中指定的路径是/usr/local/mysql/mysql.sock。
bind-address: MySQL服务器绑定的IP地址,默认为127.0.0.1,这里设置为0.0.0.0表示允许任何IP地址访问MySQL服务器。
skip-name-resolve: 禁用DNS反向解析,可以提高性能,尤其是在有大量连接时。
max_connections: MySQL服务器支持的最大并发连接数,默认为151,这里设置为2048。
default-storage-engine: 设置默认的存储引擎为InnoDB。
max_allowed_packet: 设置MySQL服务器接收的最大数据包大小,默认为4MB,这里设置为16MB。
server-id: 设置MySQL服务器的唯一ID,在主从复制设置中使用。
NO_ENGINE_SUBSTITUTION
如果需要的存储引擎被禁用或未编译,那么抛出错误。不设置此值时,用默认的存储引擎替代,并抛出一个异常
STRICT_TRANS_TABLES
在该模式下,如果一个值不能插入到一个事务表中,则中断当前的操作,对非事务表不做限制
NO_AUTO_CREATE_USER
禁止:GRANT创建密码为空的用户
NO_AUTO_VALUE_ON_ZERO
mysql中的自增长列可以从o开始。默认情况下自增长列是从1开始的,如果你插入值为0的数据会报错
NO_ZERO_IN_DATE
mysql数据库不允许插入零日期,插入零日期会抛出错误而不是警告
ERROR_FOR_DIVISION_BY_ZERO
在INSERT或UPDATE过程中,如果数据被零除,则产生错误而非警告。默认情况下数据被零除时MysQL返回NULL
IPES_AS_CONCAT
将"||"视为字符串的连接操作符而非或运算符,这和oracle数据库是一样的,
ANSI QUOTES
启用ANSI QUOTES后,不能用双引号来引用字符串,因为它被解释为识别符
更改mysql安装目录和配置文件的属主属组,让系统能够识别
chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf

设置路径环境变量(全局变量)
echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
source /etc/profile

初始化数据库
cd /usr/local/mysql/bin/
./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data


添加mysqld系统服务
cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
systemctl daemon-reload 复制完之后重新加载配置
systemctl start mysqld.service 然后重启服务
systemctl enable mysqld


给数据库配置登录密码
mysqladmin -u root -p password "123456"


登录数据库,验证一下数据库工作是否正常
展示数据库

授权远程登录
mysql -u root -p
grant all privileges on *.* to 'root'@'%' identified by '123456'; (有分号)
#授予root用户可以在所有终端远程登录,使用的密码是123456,并对所有数据库和所有表有操作权限
GRANT: 这是授权语句的关键字,用于授予特定的权限给指定用户。
ALL PRIVILEGES: 表示授予所有权限,包括SELECT、INSERT、UPDATE、DELETE、CREATE、DROP等。
*.*: 这是指定数据库和表的位置,其中*表示所有数据库,第一个*表示所有表。
因此,这个语句授予'root'用户在所有数据库的所有表上的所有权限。
'root'@'%': 这是授予权限的用户和主机的标识。'root'是用户名,'%'表示任何主机。
所以,'root'用户可以从任何主机连接并拥有授予的权限。
IDENTIFIED BY '123456': 这是设置用户'root'的密码为'123456',这样用户在连接时需要提供相应的密码。
flush privileges; 刷新权限

这时候就可以进程远程连接了

主机连接成功
这这些都是主机自带的库
点mysql查看一下

多条数据查询


退出quit

安装配置PHP
安装依赖环境


编译安装
cd /opt
tar jxvf php-7.1.10.tar.bz2

cd php-7.1.10
./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip
了解即可
--prefix=/usr/local/php: 指定PHP的安装目录为/usr/local/php。
--with-mysql-sock=/usr/local/mysql/mysql.sock: 指定MySQL的Unix socket文件路径,用于与MySQL数据库建立连接。
--with-mysqli: 启用MySQLi扩展,用于支持MySQL数据库。
--with-zlib: 启用Zlib库,用于压缩和解压缩数据。
--with-curl: 启用cURL库,用于支持对URL的操作,如下载文件等。
--with-gd: 启用GD库,用于处理图像。
--with-jpeg-dir: 指定JPEG库的路径,用于GD库对JPEG格式图片的处理。
--with-png-dir: 指定PNG库的路径,用于GD库对PNG格式图片的处理。
--with-freetype-dir: 指定FreeType库的路径,用于GD库对字体的支持。
--with-openssl: 启用OpenSSL库,用于支持SSL加密和HTTPS协议。
--enable-fpm: 启用PHP-FPM(FastCGI Process Manager),用于提供更高效的PHP进程管理和请求处理。
--enable-mbstring: 启用多字节字符串支持,用于处理多字节字符集的操作。
--enable-xml: 启用XML支持,用于处理XML文档和数据。
--enable-session: 启用会话支持,用于在不同页面间保持用户会话状态。
--enable-ftp: 启用FTP支持,用于对FTP服务器进行操作。
--enable-pdo: 启用PDO(PHP Data Objects),用于支持数据库访问的统一接口。
--enable-tokenizer: 启用Tokenizer扩展,用于对字符串进行分词处理。
--enable-zip: 启用Zip扩展,用于对ZIP文件进行操作。

执行一下

装完数据库也清一下缓存

make -j 4 && make install


路径优化
ln -s /usr/local/php/bin/* /usr/local/bin/
ln -s /usr/local/php/sbin/* /usr/local/sbin/

调整PHP配置文件
php有三个配置文件:
php.ini 主配置文件
php-fpm.conf 进程服务配置文件
www.conf 扩展配置文件
#调整主配置文件:
cp /opt/php-7.1.10/php.ini-development /usr/local/php/lib/php.ini
vim /usr/local/php/lib/php.ini

set num 显示行数
--1170行--修改
mysqli.default_socket = /usr/local/mysql/mysql.sock

--939行--取消注释,修改
date.timezone = Asia/Shanghai

php -m #验证安装的模块

#调整进程服务配置文件:
cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf

vim php-fpm.conf
set num
--17行--去掉";"注释
pid = run/php-fpm.pid

#调整扩展配置文件:
cd /usr/local/php/etc/php-fpm.d/
cp www.conf.default www.conf


启动php-fpm
/usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini
netstat -anpt | grep 9000

cd /opt/php-7.1.10/sapi/fpm
cp php-fpm.service /usr/lib/systemd/system/php-fpm.service
systemctl restart php-fpm.service



配置 Nginx 支持 PHP 解析
vim nginx.conf

--65行--取消注释,修改
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; #将 /scripts 修改为nginx的工作目录
fastcgi_param SCRIPT_FILENAME document_rootfastcgi_script_name; #$document_root 代表当前请求在root指令中指定的值
include fastcgi_params;
}



FastCGI(Fast Common Gateway Interface)是一种用于连接Web服务器(如Nginx、Apache等)
与应用程序(如PHP、Python、Ruby等)的通信协议。
它的作用是提供一种高效、可靠、灵活的方式来处理动态网页内容的请求,
使Web服务器能够与动态应用程序进行交互。
location ~ \.php$ { ... }:这是一个正则表达式匹配的location块,用于匹配以.php为后缀的请求。
root html;:设置服务器根目录为html,即将从这个目录开始查找文件。
fastcgi_pass 127.0.0.1:9000;:设置FastCGI服务器的地址和端口号。
在这里,FastCGI服务器设置为本地地址(127.0.0.1)的9000端口。通常,这是PHP-FPM的监听地址和端口。
fastcgi_index index.php;:指定当请求目录时默认访问的文件名。
在这里,如果请求目录时,默认访问的文件为index.php。
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;:
设置FastCGI参数SCRIPT_FILENAME的值,指定PHP脚本的文件路径。
$fastcgi_script_name是FastCGI服务器传递给PHP-FPM的变量,
表示当前请求的脚本路径。这里将根目录设置为/usr/local/nginx/html,
以匹配Nginx配置文件中root指令的值。
fastcgi_param SCRIPT_FILENAME document_rootfastcgi_script_name;:
这是另一种设置SCRIPT_FILENAME的方式,使用了$document_root变量。
$document_root表示Nginx中root指令指定的值,即服务器根目录。
这里将document_rootfastcgi_script_name设置为SCRIPT_FILENAME的值,
以便传递给PHP-FPM。
include fastcgi_params;:包含fastcgi_params文件中的FastCGI参数。
这里使用include指令,将fastcgi_params中的内容合并到当前配置中。
systemctl restart nginx.service


验证PHP 测试页
vim /usr/local/nginx/html/index.php
<?php
phpinfo();
?>

浏览器访问
http://192.168.233.21/index.php

这就是获取到了PHP,请求到了PHP的页面

数据库配置(给论坛创建数据库)
验证数据库工作是否正常
mysql -u root -p
CREATE DATABASE bbs;


给用户赋权:
GRANT all ON bbs.* TO 'bbsuser'@'%' IDENTIFIED BY 'admin123';
GRANT all ON bbs.* TO 'bbsuser'@'localhost' IDENTIFIED BY 'admin123';本地用户登陆
flush privileges;刷新权限
GRANT all ON bbs.* TO 'bbsuser'@'%' IDENTIFIED BY 'admin123';
GRANT: 这是授权语句的关键字,用于授予特定的权限给指定用户。
all: 表示授予所有权限,包括SELECT、INSERT、UPDATE、DELETE、CREATE、DROP等。
bbs.*: 这是指定数据库和表的位置,其中bbs表示数据库名,*表示所有表。
因此,这个语句授予bbsuser用户在bbs数据库的所有表上的所有权限。
'bbsuser'@'%': 这是授予权限的用户和主机的标识。'bbsuser'是用户名,'%'表示任何主机。
所以,bbsuser用户可以从任何主机连接并拥有授予的权限。
IDENTIFIED BY 'admin123': 这是设置bbsuser用户的密码为admin123,这样用户在连接时需要提供相应的密码。
GRANT all ON bbs.* TO 'bbsuser'@'localhost' IDENTIFIED BY 'admin123';
这条语句与第一条类似,但是主机部分不同。'bbsuser'@'localhost'表示用户bbsuser只能从本地主机(
即MySQL服务器所在的主机)连接,并拥有相同的权限。
bbsuser用户被授予在bbs数据库的全部权限,允许从任何主机和本地主机连接MySQL服务器,并且密码为admin123.

vim /usr/local/nginx/html/index.php #替换原来的测试页内容
<?php
$link=mysqli_connect('192.168.233.10','bbsuser','admin123');
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
?>
PHP脚本
使用mysqli_connect()函数连接到MySQL数据库服务器。
'192.168.233.10':表示MySQL服务器的IP地址。
'bbsuser':表示要连接的MySQL用户。
'admin123':表示MySQL用户的密码。
判断连接状态:
如果连接成功,即$link不为空,就输出<h1>Success!!</h1>,显示"Success!!"这个标题。
如果连接失败,即$link为空,就输出Fail!!,显示"Fail!!"这个信息。
该脚本的目的是测试连接到MySQL数据库服务器的状态。如果连接成功,
将显示"Success!!",如果连接失败,将显示"Fail!!"


浏览器访问
http://192.168.233.10/index.php
success连接成功

【部署 Discuz!社区论坛 Web 应用】
cd /opt
unzip Discuz_X3.4_SC_UTF8.zip -d /opt/dis
cd /opt/dis/dir_SC_UTF8/
cp -r upload/ /usr/local/nginx/html/bbs/



调整论坛目录的权限:
cd /usr/local/nginx/html/bbs/
chown -R nginx ./config/
chown -R nginx ./data/
chown -R nginx ./uc_client/
chown -R nginx ./uc_server/
或
chmod -R 777 ./config/
chmod -R 777 ./data/
chmod -R 777 ./uc_client/
chmod -R 777 ./uc_server/


ll查看一下是否有问题

论坛页面访问
http://192.168.233.10/bbs/install/index.php



注:所有的目录的状态都要打√,依赖关系也得OK,所需状态必须和当前状态保持一致
全新安装
1、数据库名,数据库用户名必须和你前面创建的一致
这里是bbs和bbsuser
2、数据库密码也得一致admin123
3、设置一下管理员账号和密码admin123

安装成功,开始访问

出现403,就用绝对路径进行访问

用绝对路径就可以访问了

登陆看一下

整个流程就完成了
如果你只是看,没登录,就只是一个静态页面,当你登录输入用户名和密码时才是动态页面
静态

登录输入用户名和密码时,才涉及到动态页面

总结就是静态页面靠nginx来提供处理,动态页面靠PHP来处理,早期的论坛就是靠这两个相互配合来进行处理的
发帖:

一定要admin这个用户能登陆才算是论坛搭建成功
