且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-08-12 17:34:22

什么是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

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

四、更改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的测试界面 可以看到你少哪些具体的组件)Linux RedHat7.4搭建LNMP(部署Discuz论坛)

测试是否可以连接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的字段Linux RedHat7.4搭建LNMP(部署Discuz论坛)

八、下载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/

会显示以下安装界面:

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

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

开始安装论坛

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

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

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