目录
环境
步骤
下载nginx源码包,并解压
安装依赖包
进行预编译 、编译安装
安装php、设置开机自启
配置nginx让其支持php服务
浏览器测试
安装mariadb
部署discuz论坛
简介
LNMP架构是一种常见的Web服务器架构,由Linux、Nginx、MySQL和PHP组成。它是一套成熟、稳定、安全的Web服务器架构,广泛应用于各类中大型企业级应用和互联网站点。LNMP架构的主要特点和优势如下:
-
高性能和高并发:Nginx作为LNMP架构中的Web服务器,具有出色的性能和高并发处理能力。它采用事件驱动的非阻塞I/O模型,能够高效地处理大量并发连接和请求,适合于高流量的网站和应用程序。同时,Nginx在处理请求时采用较少的内存资源,对系统资源的消耗相对较低,使得LNMP架构在资源受限的环境下表现出色。
-
安全性高:LNMP架构提供了许多安全性保障。例如,Nginx可以通过HTTP认证和SSL/TLS加密来提供数据和服务的安全性。
-
灵活性:LNMP可以安装于多个平台和多个版本的Linux操作系统上,同时还可以容易地进行自定义和扩展以适应不同的应用需求。
-
易用性:LNMP具有易用性,不需要额外的复杂配置即可完成安装和配置。
-
高可靠性:LNMP使用MySQL作为后端数据库,具有数据可靠性高、备份恢复容易等优点。同时,MySQL还提供了复制和分区等特性,以支持容错和高可用性需求。
LNMP架构的工作原理主要如下:用户通过浏览器访问Nginx Web网站,Nginx服务器接收到用户的请求后,先判断请求的URL,根据URL和配置进行匹配,检测请求的Web文件是静态还是动态网站。如果是静态HTML,Nginx直接处理并响应给客户端;如果是动态PHP(或其他脚本语言),Nginx会将请求发送给后端的PHP模块或CGI,PHP解释器将动态网页解析为静态的HTML网页,然后返回给Nginx,Nginx再将解析后的HTML静态网页结果返回给客户。
总的来说,LNMP架构以其高性能、安全性、灵活性、易用性和高可靠性,成为目前互联网公司主流的一种Web架构,特别适用于处理高并发、多并发连接、大流量网站的情景。
环境
redhat 9
nginx:1.24.0
步骤
修改主机名
[root@admin ~]# hostnamectl hostname lnmp
[root@admin ~]# bash
[root@lnmp ~]#
设置防火墙规则允许http,https通行
[root@lnmp ~]# firewall-cmd --zone=public --add-service=http --permanent
success //zone设置防火墙区域public,适用于
[root@lnmp ~]#
[root@lnmp ~]# firewall-cmd --zone=public --add-service=https --permanent
success
[root@lnmp ~]# firewall-cmd --zone=public --add-port=443/tcp --permanent
success
[root@lnmp ~]# firewall-cmd --zone=public --add-port=8080/tcp --permanent
success
[root@lnmp ~]# firewall-cmd --reload
success
下载nginx源码包,并解压
官网:https://nginx.org/download/nginx-1.24.0.tar.gz
[root@lnmp ~]# wget -c https://nginx.org/download/nginx-1.24.0.tar.gz
--2024-04-23 09:23:37-- https://nginx.org/download/nginx-1.24.0.tar.gz
正在解析主机 nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:5c0:2600::6, ...
正在连接 nginx.org (nginx.org)|3.125.197.172|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1112471 (1.1M) [application/octet-stream]
正在保存至: “nginx-1.24.0.tar.gz”
nginx-1.24.0.tar.gz 100%[===========================================================================================>] 1.06M 835KB/s 用时 1.3s
2024-04-23 09:23:39 (835 KB/s) - 已保存 “nginx-1.24.0.tar.gz” [1112471/1112471])
[root@lnmp ~]#
[root@lnmp ~]# wget -c https://nginx.org/download/nginx-1.24.0.tar.gz //-c断点续传
--2024-04-23 09:23:37-- https://nginx.org/download/nginx-1.24.0.tar.gz
正在解析主机 nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:5c0:2600::6, ...
正在连接 nginx.org (nginx.org)|3.125.197.172|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1112471 (1.1M) [application/octet-stream]
正在保存至: “nginx-1.24.0.tar.gz”
nginx-1.24.0.tar.gz 100%[===========================================================================================>] 1.06M 835KB/s 用时 1.3s
2024-04-23 09:23:39 (835 KB/s) - 已保存 “nginx-1.24.0.tar.gz” [1112471/1112471])
[root@lnmp ~]#
安装依赖包
[root@lnmp ~]# yum -y install gcc make gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
正在更新 Subscription Management 软件仓库。
无法读取客户身份
本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。
上次元数据过期检查:0:00:07 前,执行于 2024年04月23日 星期二 09时51分27秒。
软件包 pcre-8.44-3.el9.3.x86_64 已安装。
软件包 zlib-1.2.11-39.el9.x86_64 已安装。
软件包 openssl-1:3.0.7-6.el9_2.x86_64 已安装。
依赖关系解决。
=================================================================================================================================================================================
软件包 架构 版本 仓库 大小
=================================================================================================================================================================================
安装:
gcc x86_64 11.3.1-4.3.el9 aa 32 M
gcc-c++ x86_64 11.3.1-4.3.el9 aa 13 M
make x86_64 1:4.3-7.el9 bb 542 k
openssl-devel x86_64 1:3.0.7-6.el9_2 aa 4.1 M
pcre-devel x86_64 8.44-3.el9.3 aa 511 k
zlib-devel x86_64 1.2.11-39.el9 aa 47 k
安装依赖关系:
glibc-devel x86_64 2.34-60.el9 aa 54 k
glibc-headers x86_64 2.34-60.el9 aa 556 k
kernel-headers x86_64 5.14.0-284.11.1.el9_2 aa 5.0 M
libstdc++-devel x86_64 11.3.1-4.3.el9 aa 2.4 M
libxcrypt-devel x86_64 4.4.18-3.el9 aa 32 k
pcre-cpp x86_64 8.44-3.el9.3 aa 28 k
pcre-utf16 x86_64 8.44-3.el9.3 aa 188 k
pcre-utf32 x86_64 8.44-3.el9.3 aa 178 k
事务概要
=================================================================================================================================================================================
安装 14 软件包
省略。。。。。。
创建nginx用户
[root@lnmp ~]# useradd -s /sbin/nologin nginx
进行预编译 、编译安装
--with-http_ssl_module //Nginx 将会被编译以支持 SSL/TLS 功能,通常用于 HTTPS 连接。
[root@lnmp ~]# useradd -s /sbin/nologin nginx
[root@lnmp ~]# cd /usr/local/src/nginx-1.24.0/
[root@lnmp nginx-1.24.0]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@lnmp nginx-1.24.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
checking for OS
+ Linux 5.14.0-284.11.1.el9_2.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 11.3.1 20221121 (Red Hat 11.3.1-4) (GCC)
checking for gcc -pipe switch ... found
checking for -Wl,-E switch ... found
checking for gcc builtin atomic operations ... found
省略。。。。。。
[root@lnmp nginx-1.24.0]# make && make install //编译安装
make -f objs/Makefile
make[1]: 进入目录“/usr/local/src/nginx-1.24.0”
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs \
-o objs/src/core/nginx.o \
src/core/nginx.c
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unu
省略。。。。。
启动nginx,然后查看端口
[root@lnmp ~]# /usr/local/nginx/sbin/nginx
[root@lnmp ~]# ss -anltp | grep 80
LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=8247,fd=6),("nginx",pid=8246,fd=6))
[root@lnmp ~]#
浏览器访问 ip
安装php、设置开机自启
[root@lnmp ~]# yum -y install php*
正在更新 Subscription Management 软件仓库。
无法读取客户身份
本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。
上次元数据过期检查:0:24:30 前,执行于 2024年04月23日 星期二 09时51分27秒。
依赖关系解决。
=================================================================================================================================================================================
软件包 架构 版本 仓库 大小
===================
省略 。。。。。。
[root@lnmp ~]# systemctl start php-fpm.service
[root@lnmp ~]# systemctl enable php-fpm.service
Created symlink /etc/systemd/system/multi-user.target.wants/php-fpm.service → /usr/lib/systemd/system/php-fpm.service.
[root@lnmp ~]#
配置nginx让其支持php服务
[root@lnmp ~]# vim /usr/local/nginx/conf/nginx.conf
65 location ~ \.php$ {
66 root html;
67 fastcgi_pass 127.0.0.1:9000;
68 fastcgi_index index.php;
69 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;//修改请求脚本路径
70 include fastcgi_params;
71 }
#修改php配置文件
[root@lnmp ~]# vim /etc/php-fpm.d/www.conf
24 user = nginx //改为nginx
25 ; RPM: Keep a group allowed to write in log dir.
26 group = nginx //改为nginx
38 listen = 127.0.0.1:9000 //listen改为本地IP加9000端口
[root@lnmp ~]# systemctl reload php-fpm
#增加一个index.php文件测试php是否可用
[root@lnmp ~]# vim /usr/local/nginx/html/index.php
1 <?php
2 phpinfo();
3 ?>
[root@lnmp ~]# /usr/local/nginx/sbin/nginx -s stop
[root@lnmp ~]# /usr/local/nginx/sbin/nginx
[root@lnmp ~]# ss -anltp |grep 80
LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=12862,fd=6),("nginx",pid=12861,fd=6))
[root@lnmp ~]# ss -anltp | grep 9000
LISTEN 0 511 127.0.0.1:9000 0.0.0.0:* users:(("php-fpm",pid=18409,fd=10),("php-fpm",pid=13729,fd=10),("php-fpm",pid=13728,fd=10),("php-fpm",pid=13727,fd=10),("php-fpm",pid=13726,fd=10),("php-fpm",pid=13725,fd=10),("php-fpm",pid=13724,fd=8))
[root@lnmp ~]#
浏览器测试
安装mariadb
[root@lnmp ~]# yum -y install mariadb*
正在更新 Subscription Management 软件仓库。
无法读取客户身份
本系统尚未在权利服务器中注册。可使用 subscription-manager 进行注册。
上次元数据过期检查:1:36:51 前,执行于 2024年04月23日 星期二 09时51分27秒。
软件包 mariadb-connector-c-3.2.6-1.el9_0.x86_64 已安装。
软件包 mariadb-connector-c-config-3.2.6-1.el9_0.noarch 已安装。
依赖关系解决。
====================================================================================================================================================================================
软件包 架构 版本 仓库 大小
====================================================================================================================================================================================
安装:
mariadb x86_64 3:10.5.16-2.el9_0 aa 1.6 M
mariadb-backup x86_64 3:10.5.16-2.el9_0 aa 6.4 M
mariadb-common x86_64 3:10.5.16-2.el9_0 aa 39 k
mariadb-connector-c-devel x86_64 3.2.6-1.el9_0 aa 62 k
mariadb-connector-odbc x86_64 3.1.12-3.el9 aa 115 k
mariadb-embedded x86_64 3:10.5.16-2.el9_0 aa 5.3 M
mariadb-errmsg x86_64 3:10.5.16-2.el9_0 aa 227 k
mariadb-gssapi-server x86_64 3:10.5.16-2.el9_0 aa 21 k
mariadb-java-client noarch 3.0.3-1.el9 aa 969 k
mariadb-oqgraph-engine x86_64 3:10.5.16-2.el9_0 aa 87 k
mariadb-pam x86_64 3:10.5.16-2.el9_0 aa 31 k
mariadb-server x86_64 3:10.5.16-2.el9_0 aa 9.4 M
mariadb-server-galera x86_64 3:10.5.16-2.el9_0 aa 31 k
mariadb-server-utils x86_64 3:10.5.16-2.el9_0 aa 222 k
安装依赖关系:
Judy x86_64 1.0.5-28.el9 aa 134 k
boost-program-options x86_64 1.75.0-8.el9 aa 108 k
copy-jdk-configs noarch 4.0-3.el9 aa 29 k
galera x86_64 26.4.11-1.el9_0 aa 1.2 M
java-11-openjdk-headless x86_64 1:11.0.18.0.10-2.el9_1 aa 40 M
javapackages-filesystem noarch 6.0.0-4.el9 aa 17 k
jna x86_64 5.6.0-8.el9 aa 274 k
jna-contrib noarch 5.6.0-8.el9 aa 549 k
lksctp-tools x86_64 1.0.19-2.el9 bb 98 k
lua x86_64 5.4.4-3.el9 aa 192 k
lua-posix x86_64 35.0-8.el9 aa 155 k
mysql-selinux noarch 1.0.5-1.el9_0 aa 37 k
perl-DBD-MariaDB x86_64 1.21-16.el9_0 aa 156 k
perl-Sys-Hostname x86_64 1.23-480.el9 aa 20 k
slf4j noarch 1.7.30-13.el9 aa 73 k
tzdata-java noarch 2022g-2.el9 aa 233 k
事务概要
====================================================================================================================================================================================
安装 30 软件包
[root@lnmp ~]# systemctl restart mariadb.service
[root@lnmp ~]# systemctl enable mariadb.service
Created symlink /etc/systemd/system/mysql.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service → /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service → /usr/lib/systemd/system/mariadb.service.
[root@lnmp ~]# mysql_secure_installation //初始化设置
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.
You already have your root account protected, so you can safely answer 'n'.
Switch to unix_socket authentication [Y/n] y
Enabled successfully!
Reloading privilege tables..
... Success!
You already have your root account protected, so you can safely answer 'n'.
Change the root password? [Y/n]
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n]
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n]
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n]
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n]
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB
修改mariadb配置文件
[root@lnmp ~]# vim /etc/my.cnf.d/mariadb-server.cnf
[mysqld]
17 init_connect='SET collation_connection = utf8_unicode_ci'
18 init_connect='SET NAMES utf8'
19 character-set-server=utf8
20 collation-server=utf8_unicode_ci
21 skip-character-set-client-handshake
[root@lnmp ~]# vim /etc/my.cnf.d/mysql-clients.cnf
6 [mysql]
7 default-character-set=utf8
#重启服务
[root@lnmp ~]# systemctl restart mariadb.service
[root@lnmp ~]# systemctl enable mariadb.service
[root@lnmp ~]#
部署discuz论坛
官网:Discuz!官方网站 - 开放、连接、共赢
下载并解压
[root@lnmp ~]# wget https://gitee.com/Discuz/DiscuzX/attach_files/1621557/download
--2024-04-23 12:00:26-- https://gitee.com/Discuz/DiscuzX/attach_files/1621557/download
正在解析主机 gitee.com (gitee.com)... 180.76.198.77
正在连接 gitee.com (gitee.com)|180.76.198.77|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 302 Found
位置:https://foruda.gitee.com/attach_file/1703208479609778362/discuz_x3.5_sc_utf8_20231221.zip?token=faa18eb48c16a19d7dfae3068a01b3f2&ts=1713844827&attname=Discuz_X3.5_SC_UTF8_20231221.zip [跟随至新的 URL]
--2024-04-23 12:00:27-- https://foruda.gitee.com/attach_file/1703208479609778362/discuz_x3.5_sc_utf8_20231221.zip?token=faa18eb48c16a19d7dfae3068a01b3f2&ts=1713844827&attname=Discuz_X3.5_SC_UTF8_20231221.zip
正在解析主机 foruda.gitee.com (foruda.gitee.com)... 180.76.198.77
正在连接 foruda.gitee.com (foruda.gitee.com)|180.76.198.77|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:11593050 (11M) [application/zip]
正在保存至: “download”
download 100%[==============================================================================================>] 11.06M 524KB/s 用时 22s
2024-04-23 12:00:49 (515 KB/s) - 已保存 “download” [11593050/11593050])
[root@lnmp ~]# unzip download //直接解压即可
Archive: download
inflating: LICENSE
inflating: qqqun.png
creating: readme/
inflating: readme.html
省略 。。。。。。
把解压后的upload下的文件都复制到nginx的html下
[root@lnmp ~]# rm -rf /usr/local/nginx/html/*
[root@lnmp ~]# \cp -r upload/* /usr/local/nginx/html/
修改nginx配置
[root@lnmp ~]# vim /usr/local/nginx/conf/nginx.conf
43 location / {
44 root /usr/local/nginx/html/; //修改路径
45 index index.php index.html index.htm; //增加index.php文件
46 }
65 location ~ \.php$ {
66 root /usr/local/nginx/html/;
67 fastcgi_pass 127.0.0.1:9000;
68 fastcgi_index index.php;
69 fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; //脚本路径
70 include fastcgi_params;
[root@lnmp ~]# /usr/local/nginx/sbin/nginx -s reload
[root@lnmp ~]# chown -R nginx:nginx /usr/local/nginx/html/
[root@lnmp ~]# chmod 777 -R /usr/local/nginx/html/
[root@lnmp ~]# setenforce 0
[root@lnmp ~]# systemctl restart firewalld.service
浏览器访问
回到linux,进入MySQL创建一个登录用户,并授权
[root@lnmp ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 10.5.16-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)]>
MariaDB [(none)]> create user 'DZ'@'%' identified by '123456';
Query OK, 0 rows affected (0.005 sec)
MariaDB [(none)]> grant all privileges on *.* to 'DZ'@'%';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> flush privileges; //刷新权限表
Query OK, 0 rows affected (0.001 sec)
回到浏览器刷新再次填写
下一步等待安装完成
进入后界面如下