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

本文涉及的产品
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
简介: 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


相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
相关文章
|
8月前
|
安全 Linux 数据安全/隐私保护
Red Hat Enterprise Linux 9.6 (x86_64, aarch64) - 红帽企业 Linux (RHEL)
Red Hat Enterprise Linux 9.6 (x86_64, aarch64) - 红帽企业 Linux (RHEL)
1007 36
Red Hat Enterprise Linux 9.6 (x86_64, aarch64) - 红帽企业 Linux (RHEL)
|
6月前
|
NoSQL 关系型数据库 Linux
ERPNext 搭建教程:Linux 一键部署与维护
ERPNext 是一款开源免费的企业资源计划系统,适用于中小企业信息化管理。基于 Python 和 Frappe 框架开发,支持财务、销售、人力、库存等模块,具备高度可定制性。本文介绍如何通过 Websoft9 在 Linux 下快速部署 ERPNext,并提供环境配置、系统维护等实用建议,适合开发者和企业用户快速上手。
969 7
ERPNext 搭建教程:Linux 一键部署与维护
|
8月前
|
监控 Linux 应用服务中间件
Linux多节点多硬盘部署MinIO:分布式MinIO集群部署指南搭建高可用架构实践
通过以上步骤,已成功基于已有的 MinIO 服务,扩展为一个 MinIO 集群。该集群具有高可用性和容错性,适合生产环境使用。如果有任何问题,请检查日志或参考MinIO 官方文档。作者联系方式vx:2743642415。
2708 57
|
6月前
|
Java Linux 网络安全
Linux云端服务器上部署Spring Boot应用的教程。
此流程涉及Linux命令行操作、系统服务管理及网络安全知识,需要管理员权限以进行配置和服务管理。务必在一个测试环境中验证所有步骤,确保一切配置正确无误后,再将应用部署到生产环境中。也可以使用如Ansible、Chef等配置管理工具来自动化部署过程,提升效率和可靠性。
636 13
|
6月前
|
关系型数据库 Linux Nacos
Rocky Linux 部署 Docker 和 NACOS 实例
本文介绍在阿里云环境下基于 Rocky Linux 搭建 Docker 并部署 Nacos 的完整流程。涵盖 Docker 安装、镜像加速配置、网络设置及 MySQL 与 Nacos 容器的创建,适用于开发与生产环境。
856 1
|
8月前
|
Java 关系型数据库 MySQL
在Linux平台上进行JDK、Tomcat、MySQL的安装并部署后端项目
现在,你可以通过访问http://Your_IP:Tomcat_Port/Your_Project访问你的项目了。如果一切顺利,你将看到那绚烂的胜利之光照耀在你的项目之上!
441 41
|
8月前
|
开发框架 Java 关系型数据库
在Linux系统中安装JDK、Tomcat、MySQL以及部署J2EE后端接口
校验时,浏览器输入:http://[your_server_IP]:8080/myapp。如果你看到你的应用的欢迎页面,恭喜你,一切都已就绪。
559 17
|
8月前
|
Java 关系型数据库 MySQL
在Linux操作系统上设置JDK、Tomcat、MySQL以及J2EE后端接口的部署步骤
让我们总结一下,给你的Linux操作系统装备上最强的军队,需要先后装备好JDK的弓箭,布置好Tomcat的阵地,再把MySQL的物资原料准备好,最后部署好J2EE攻城车,那就准备好进军吧,你的Linux军团,无人可挡!
179 18
|
8月前
|
开发框架 关系型数据库 Java
Linux操作系统中JDK、Tomcat、MySQL的完整安装流程以及J2EE后端接口的部署
然后Tomcat会自动将其解压成一个名为ROOT的文件夹。重启Tomcat,让新“植物”适应新环境。访问http://localhost:8080/yourproject看到你的项目页面,说明“植物”种植成功。
248 10