Linux RedHat7.4搭建LNMP(部署Discuz论坛)

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS PostgreSQL,集群系列 2核4GB
简介: Linux RedHat7.4搭建LNMP(部署Discuz论坛)

什么是LNMP?

Linux+Nginx+MySQL+PHP

实验效果

用LNMP搭建一个Discuz论坛

实验准备(在VMware中进行实验)

RedHat(RHEL)7.4

已经联网

一、更换成阿里云yum源

查询已安装的yum

[root@localhost ~]# rpm -qa | grep yum
PackageKit-yum-1.1.5-1.el7.x86_64
yum-3.4.3-154.el7.noarch
yum-rhn-plugin-2.0.1-9.el7.noarch
yum-utils-1.1.31-42.el7.noarch
yum-metadata-parser-1.1.4-10.el7.x86_64
yum-langpacks-0.4.2-7.el7.noarch

使用root权限卸载已安装的yum源

[root@localhost ~]# rpm -qa | grep yum | xargs rpm -e --nodeps
warning: /etc/yum/pluginconf.d/langpacks.conf saved as /etc/yum/pluginconf.d/langpacks.conf.rpmsave

下载并安装阿里的yum源rpm包

(浏览器打开网址:https://mirrors.aliyun.com/centos/7/os/x86_64/Packages

然后一个一个的去搜索下载到本地

最后到Linux里面去建一个rpm文件,然后把下载好的包复制进去)

wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/yum-3.4.3-161.el7.centos.noarch.rpm
wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.31-50.el7.noarch.rpm
wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/yum-utils-1.1.31-50.el7.noarch.rpm
wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/python-urlgrabber-3.10-9.el7.noarch.rpm

查看rpm包

[root@localhost ~]#  mkdir rpm
[root@localhost ~]# cd rpm/
[root@localhost rpm]# ls
python-urlgrabber-3.10-10.el7.noarch.rpm
yum-3.4.3-168.el7.centos.noarch.rpm
yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch.rpm
yum-utils-1.1.31-54.el7_8.noarch.rpm

安装(必须要强制安装)

[root@localhost rpm]# rpm -ivh *.rpm  --force --nodeps
warning: python-urlgrabber-3.10-10.el7.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:yum-metadata-parser-1.1.4-10.el7 ################################# [ 20%]
   2:python-urlgrabber-3.10-10.el7    ################################# [ 40%]
   3:yum-plugin-fastestmirror-1.1.31-5################################# [ 60%]
   4:yum-3.4.3-168.el7.centos         ################################# [ 80%]
   5:yum-utils-1.1.31-54.el7_8        ################################# [100%]

下载阿里镜像到/etc/yum.repos.d/目录下

[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
--2020-12-21 14:36:48--  http://mirrors.aliyun.com/repo/Centos-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 112.19.3.184, 112.19.3.182, 112.19.3.183, ...
Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|112.19.3.184|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2523 (2.5K) [application/octet-stream]
Saving to: ‘/etc/yum.repos.d/CentOS-Base.repo’
100%[======================================>] 2,523       --.-K/s   in 0s      
2020-12-21 14:36:48 (140 MB/s) - ‘/etc/yum.repos.d/CentOS-Base.repo’ saved [2523/2523]

修改Centos-7.repo文件将所有$releasever替换为7

[root@localhost yum.repos.d]# vim CentOS-Base.repo 
按Ctrl+:
输入:%s/$releasever/7/g

清除缓存并重新生成

[root@localhost yum.repos.d]# yum clean all
[root@localhost yum.repos.d]# yum makecache 

更新yum包

[root@localhost yum.repos.d]# yum update

二、安装Nginx

下载会用到的工具包

[root@localhost ~]# yum install -y bash-completion vim wget  curl 

设置Nginx源

[root@localhost ~]# vim /etc/yum.repos.d/nginx.repo   ---写入以下配置文件
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
[root@localhost ~]# yum clean all
[root@localhost ~]# yum makecache 

安装Nginx

[root@localhost ~]# yum install nginx -y

重启

[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# systemctl enable nginx

设置防火墙

[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# systemctl disable firewalld.service 
[root@localhost ~]# setenforce 0
[root@localhost ~]# vim /etc/selinux/config 
SELINUX=disabled   ---改为disabled

打开浏览器

在客户端访问:192.168.112.131

image.png

四、更改Nginx的配置文件(更改默认html路径及增加一些内容)

替换Nginx配置文件(删除原有的文件,替换成下面的)

[root@localhost ~]# vim /etc/nginx/nginx.conf 
user nginx nginx;
worker_processes 4;
worker_cpu_affinity 00000001 00000010 00000100 00001000;
worker_rlimit_nofile 65535;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
  use epoll;
  worker_connections 10240;
}
http {
  include /etc/nginx/mime.types;
  default_type application/octet-stream;
  server_names_hash_bucket_size 128;
  client_header_buffer_size 2k;
  large_client_header_buffers 4 4k;
  client_max_body_size 8m;
  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
      '$status $body_bytes_sent "$http_referer" '
      '"$http_user_agent" "$http_x_forwarded_for"';
  access_log /var/log/nginx/access.log main;
  sendfile off;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 65;
  fastcgi_cache_path /etc/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m;
  fastcgi_cache_key http://$host$request_uri;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 4k;
  fastcgi_buffers 8 4k;
  fastcgi_busy_buffers_size 8k;
  fastcgi_temp_file_write_size 8k;
  fastcgi_cache TEST;
  fastcgi_cache_valid 200 302 1h;
  fastcgi_cache_valid 301 1d;
  fastcgi_cache_valid any 1m;
  fastcgi_cache_min_uses 1;
  fastcgi_cache_use_stale error timeout invalid_header http_500;
  open_file_cache max=204800 inactive=20s;
  open_file_cache_min_uses 1;
  open_file_cache_valid 30s;
  gzip on;
  gzip_min_length 1K;
  gzip_buffers 4 16K;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types text/plain application/x-javascript text/css application/xml;
  gzip_vary on;
  include /etc/nginx/conf.d/*.conf;
}

替换default.conf(删除原有的文件,替换成下面的)

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
 server {
     listen       80;
     server_name  192.168.112.131;   ---这里换成自己的IP地址
    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
location / {
    root   /opt/nginx/html;
    index  index.html index.htm;
}
location /status
{
    stub_status on;
}
#error_page  404              /404.html;
# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /opt/nginx/html;
}
location ~ .*/.(gif|jpg|jpeg|png|bmp|swf|js|css)$
{
    expires 30d;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
#    root           html;
#    fastcgi_pass   127.0.0.1:9000;
#    fastcgi_index  index.php;
#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
#    include        fastcgi_params;
#}
  # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#    deny  all;
#}
}

由于更改了根目录,所以需要创建新的根目录

[root@localhost ~]# mkdir /opt/nginx/html -p
[root@localhost ~]# cp /usr/share/nginx/html/* /opt/nginx/html/
[root@localhost ~]# ll /opt/nginx/html/
total 8
-rw-r--r--. 1 root root 494 Dec 21 15:01 50x.html
-rw-r--r--. 1 root root 612 Dec 21 15:01 index.html

改变了服务配置文件,所以需要重新启动服务

[root@localhost ~]# systemctl restart nginx
[root@localhost ~]# systemctl status nginx

五、安装配置MySQL数据库服务器

下载安装MySQL

[root@localhost ~]# wget https://dev.mysql.com/get/mysql80-community-release-el7-2.noarch.rpm
[root@localhost ~]# rpm -Uvh mysql80-community-release-el7-2.noarch.rpm
[root@localhost ~]# yum install mysql-community-server -y

启动MySQL

[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# systemctl status mysqld

通过grep过滤出他的默认密码

[root@localhost ~]# grep 'password' /var/log/mysqld.log
2020-12-21T07:09:12.309082Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: =Wei9rR%io)P

使用默认密码登录数据库

[root@localhost ~]# mysql -uroot -p'=Wei9rR%io)P'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.22
Copyright (c) 2000, 2020, 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> ALTER USER 'root'@'localhost' IDENTIFIED BY 'XXGC.lab123';
Query OK, 0 rows affected (0.01 sec)
mysql> exit
Bye

重启MySQL数据库并设置开机自启

[root@localhost ~]# systemctl restart mysqld
[root@localhost ~]# systemctl enable mysqld

六、安装配置PHP环境

安装php7的YUM源

[root@localhost ~]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
[root@localhost ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

安装PHP7.2

[root@localhost ~]# yum install  php72w php72w-cli php72w-common php72w-gd php72w-ldap php72mbstring php72w-mcrypt php72w-mysql php72w-pdow -y

安装php-fpm并启动

[root@localhost ~]# yum install php72w-fpm php72w-opcache -y
[root@localhost ~]# systemctl restart php-fpm.service 
[root@localhost ~]# systemctl enable php-fpm.service

修改php-fpm配置文件

[root@localhost ~]# vim /etc/php-fpm.d/www.conf 
[www]
user = nginx    ---改为nginx
group = nginx   ---改为nginx
调整nginx配置文件(分为以下两个部分,都是在同一个配置文件下)

1、先修改这一区块的内容

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
    location / {
        root   /opt/nginx/html;
        index  index.php index.html index.htm;
    }

2、添加内容到location ~ .php$ 这个区块内

首先取消这几行的注释,然后再添加新增的内容

location ~ \.php$ {
    root           /opt/nginx/html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME /opt/nginx/html$fastcgi_script_name;
    include        fastcgi_params;
}

创建MySQL数据库管理员

[root@localhost ~]# mysql -uroot -p'XXGC.lab123'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.22 MySQL Community Server - GPL
Copyright (c) 2000, 2020, 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> create user 'dbadmin'@'%' identified with mysql_native_password by 'XXGC.lab123';
Query OK, 0 rows affected (0.01 sec)
mysql> grant all on *.* to 'dbadmin'@'%';
Query OK, 0 rows affected (0.01 sec)
mysql> grant GRANT OPTION  on *.* to 'dbadmin'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye

七、LNMP环境测试

写配置文件

[root@localhost ~]# vim /etc/nginx/conf.d/default.conf
[root@localhost ~]# cd /opt/nginx/html/
[root@localhost html]# vim test.php   ---写入以下内容
<?php
phpinfo();
?>
[root@localhost html]# systemctl restart php-fpm.service 
[root@localhost html]# systemctl restart nginx

在客户端访问:192.168.112.131/test.php

(可以看到php的测试界面 可以看到你少哪些具体的组件)image.png

测试是否可以连接MySQL数据库文件

[root@localhost ~]# cd /opt/nginx/html/
[root@localhost html]# vim mysql.php
<?PHP
    $conn=mysqli_connect("192.168.112.131","dbadmin","XXGC.lab123");
    if($conn){
        echo"ok";
    }else{
        echo"error";    
    }
?>

在客户端访问:192.168.112.131/mysql.php

如果php与mysql连接正确浏览器访问会反馈OK的字段image.png

八、下载Discuz论坛文件

Discuz现在在gitee.com进行维护

地址为:https://gitee.com/ComsenzDiscuz/DiscuzX

可进入网站自行下载 或上传Discuz压缩包到虚拟机

[root@localhost ~]# cd rpm/
[root@localhost rpm]# ls
Discuz_X3.4_SC_UTF8【20200818】.zip
python-urlgrabber-3.10-10.el7.noarch.rpm
yum-3.4.3-168.el7.centos.noarch.rpm
yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch.rpm
yum-utils-1.1.31-54.el7_8.noarch.rpm

解压

[root@localhost rpm]# unzip Discuz_X3.4_SC_UTF8【20200818】.zip 
[root@localhost rpm]# mv upload/ /opt/nginx/html/   ---把包移动到nginx网页下

建立Discuz论坛所用数据库

[root@localhost ~]# mysql -uroot -p'XXGC.lab123'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.22 MySQL Community Server - GPL
Copyright (c) 2000, 2020, 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> create database discuz;
Query OK, 1 row affected (0.01 sec)
mysql> exit
Bye

解决所有文件权限,需要可写权限

[root@localhost upload]# cd /opt/nginx/html/upload/
[root@localhost upload]# chmod -R 777 ./config/ ./data/ ./uc_client/ ./uc_server/
[root@localhost upload]# chown  nginx:nginx -R /opt/nginx/html/upload/

在客户端使用浏览器打开

网站地址:http://192.168.112.131/upload/install/

会显示以下安装界面:

选择全新安装,点击下一步

添加安装数据库有关信息,必须要按照实际情况添加。

开始安装论坛

image.png

安装完毕后就可以访问论坛了

image.png


相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
2月前
|
Ubuntu Linux 测试技术
Linux系统之部署轻量级Markdown文本编辑器
【10月更文挑战第6天】Linux系统之部署轻量级Markdown文本编辑器
140 1
Linux系统之部署轻量级Markdown文本编辑器
|
2天前
|
消息中间件 Java Kafka
【手把手教你Linux环境下快速搭建Kafka集群】内含脚本分发教程,实现一键部署多个Kafka节点
本文介绍了Kafka集群的搭建过程,涵盖从虚拟机安装到集群测试的详细步骤。首先规划了集群架构,包括三台Kafka Broker节点,并说明了分布式环境下的服务进程配置。接着,通过VMware导入模板机并克隆出三台虚拟机(kafka-broker1、kafka-broker2、kafka-broker3),分别设置IP地址和主机名。随后,依次安装JDK、ZooKeeper和Kafka,并配置相应的环境变量与启动脚本,确保各组件能正常运行。最后,通过编写启停脚本简化集群的操作流程,并对集群进行测试,验证其功能完整性。整个过程强调了自动化脚本的应用,提高了部署效率。
【手把手教你Linux环境下快速搭建Kafka集群】内含脚本分发教程,实现一键部署多个Kafka节点
|
1天前
|
Ubuntu 网络协议 Linux
快速部署WSL(Windows Subsystem for Linux)
WSL提供了一种轻量级的方法,使开发者能够在Windows上无缝运行Linux环境。通过本文介绍的步骤,可以快速安装、配置和使用WSL,以满足开发和测试的需求。
20 8
|
2月前
|
搜索推荐 Linux 测试技术
Linux系统之部署homer静态主页
【10月更文挑战第11天】Linux系统之部署homer静态主页
86 41
Linux系统之部署homer静态主页
|
2月前
|
运维 监控 Linux
Linux系统之部署Linux管理面板1Panel
【10月更文挑战第20天】Linux系统之部署Linux管理面板1Panel
138 3
Linux系统之部署Linux管理面板1Panel
|
1月前
|
消息中间件 Linux RocketMQ
在Red Hat Enterprise Linux 9上使用Docker快速安装并部署
通过以上步骤,你可以在Red Hat Enterprise Linux 9上使用Docker快速安装并部署RocketMQ。这种方法不仅简化了安装过程,还提供了一个灵活的环境来管理和扩展消息队列系统。RocketMQ作为一款高性能的分布式消息系统,通过Docker可以实现快速部署和高效管理。
75 2
|
1月前
|
消息中间件 Linux RocketMQ
在Red Hat Enterprise Linux 9上使用Docker快速安装并部署
通过以上步骤,你可以在Red Hat Enterprise Linux 9上使用Docker快速安装并部署RocketMQ。这种方法不仅简化了安装过程,还提供了一个灵活的环境来管理和扩展消息队列系统。RocketMQ作为一款高性能的分布式消息系统,通过Docker可以实现快速部署和高效管理。
49 3
|
2月前
|
Java Linux 网络安全
NIFI在Linux服务区上的部署配置过程是什么?
【10月更文挑战第21天】NIFI在Linux服务区上的部署配置过程是什么?
75 2
|
2月前
|
关系型数据库 MySQL Linux
基于阿里云服务器Linux系统安装Docker完整图文教程(附部署开源项目)
基于阿里云服务器Linux系统安装Docker完整图文教程(附部署开源项目)
469 3
|
关系型数据库 MySQL 应用服务中间件
手动部署LNMP环境(Alibaba Cloud Linux 2)
本场景带您体验如何在Alibaba Cloud Linux 2.1903 LTS 64位操作系统的云服务器上搭建LNMP环境。