lnmp安装fastadmin管理后台


相关文档:

  • fastadmin官网:https://www.fastadmin.net/
  • 官方源码下载:https://www.fastadmin.net/download.html
  • 官方常见问题:https://doc.fastadmin.net/doc/faq.html
  • 在线演示网页:https://www.fastadmin.net/demo.html
  • 后台演示网页:https://demo.fastadmin.net/admin.php/index/login
  • 开发者文档:https://doc.fastadmin.net/developer
  • 官方问答:https://ask.fastadmin.net/
  • gitee: https://gitee.com/karson/fastadmin.git
  • github:https://github.com/karsonzhang/fastadmin.git
  • centos7部署文档:https://cimen.club/138.html

一、环境信息

操作系统:公共镜像CentOS 7.8 64位

本文的部署配置中,服务版本如下,如果需要其它版本,需要另行安装配置。

Nginx版本:Nginx 1.20.1
MySQL版本:MySQL 5.7.36
PHP版本:PHP 7.0.33

二、LNMP环境搭建

2.1 准备编译环境

  • 关闭防火墙

运行 systemctl status firewalld 命令,查看当前防火墙的状态

如果防火墙的状态参数是inactive,则防火墙为关闭状态;如果防火墙的状态参数是active,则防火墙为开启状态。

# 临时关闭
systemctl stop firewalld

# 禁止开机自启
systemctl disable firewalld
  • 关闭SELinux

运行 getenforce 命令查看SELinux的当前状态。

如果SELinux状态参数是Disabled,则SELinux为关闭状态;如果SELinux状态参数是Enforcing,则SELinux为开启状态。

# 临时关闭
setenforce 0

# 永久关闭
vi /etc/selinux/config
找到SELINUX=enforcing,按i进入编辑模式,将参数修改为SELINUX=disabled。

2.2 nginx安装

  • 安装Nginx
yum -y install nginx
  • 查看Nginx版本。
# nginx -v
nginx version: nginx/1.20.1

2.3 mysql安装

  • 更新Yyum源
rpm -Uvh  http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
  • 安装MySQL。

注:使用的操作系统内核版本为el8,可能会提示报错信息No match for argument。您需要先运行命令yum module disable mysql禁用默认的MySQL模块,再安装MySQL。

yum -y install mysql-community-server --nogpgcheck
  • 查看mysql版本号

返回结果如下所示,表示MySQL安装成功。

# mysql -V
mysql  Ver 14.14 Distrib 5.7.41, for Linux (x86_64) using  EditLine wrapper
  • 启动mysql
systemctl start mysqld
  • 设置开机启动mysql
systemctl enable mysqld
systemctl daemon-reload

2.4 php安装

  • 更新yum源
yum install \
https://repo.ius.io/ius-release-el7.rpm \
https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
  • 添加Webtatic源
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
  • 安装php
yum -y install php70w-devel php70w.x86_64 php70w-cli.x86_64 php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64 php70w-mcrypt.x86_64  php70w-pdo.x86_64   php70w-mysqlnd  php70w-fpm php70w-opcache php70w-pecl-redis php70w-pecl-mongodb
  • 查看php版本。
# php -v
PHP 7.0.33 (cli) (built: Dec  6 2018 22:30:44) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.33, Copyright (c) 1999-2017, by Zend Technologies

2.5 nginx配置

  • nginx 主配置文件
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;
}
  • conf 文件配置
server {
    listen 80;
    server_name 域名;
    charset utf-8;
     
    location / {
		root   /usr/share/nginx/xxx;
		try_files $uri $uri/ /index.html;
		index index.html index.htm index.php;
    }
    
	location = /50x.html {
		root   /usr/share/nginx/html/xxx;
    }

    location ~ .php$ {
       fastcgi_pass   127.0.0.1:9000;
       fastcgi_index  index.php;
       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
       include        fastcgi_params;
    }
      access_log /var/log/nginx/xxx.access.log main;
      error_log /var/log/nginx/xxx.error.log warn;

#如果需要http强制跳转至https,则开启
#    rewrite ^(.*)$  https://$host$1 permanent;

}
server {
        listen 443 ssl;
        server_name  域名;
        charset utf-8;
        ssl_certificate /etc/nginx/cert/xxx.pem;
        ssl_certificate_key /etc/nginx/cert/xxx.key;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1.2;
        ssl_prefer_server_ciphers on;

    location / {
	    root   /usr/share/nginx/xxx;
		try_files $uri $uri/ /index.html;
		index index.html index.htm index.php;
	}
    
	location = /50x.html {
		root   /usr/share/nginx/html/xxx;
    }

    location ~ .php$ {
       fastcgi_pass   127.0.0.1:9000;
       fastcgi_index  index.php;
       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
       include        fastcgi_params;
    }
      access_log /var/log/nginx/xxx.access.log main;
      error_log /var/log/nginx/xxx.error.log warn;
}
  • 检查配置文件是否正确
nginx -t
  • 生效nginx配置文件
nginx -s reload

2.6 mysql配置

查看/var/log/mysqld.log文件,获取并记录root用户的初始密码。

grep 'temporary password' /var/log/mysqld.log
  • 配置mysql的安全性。
mysql_secure_installation

输入MySQL的初始密码。

说明 在输入密码时,系统为了最大限度的保证数据安全,命令行将不做任何回显。只需要输入正确的密码信息,然后按Enter键即可。

Securing the MySQL server deployment.

Enter password for user root: #输入上一步获取的root用户初始密码

为mysql设置新密码。

The existing password for the user account root has expired. Please set a new password.

New password: #输入新密码。长度为8至30个字符,必须同时包含大小写英文字母、数字和特殊符号。特殊符号包含()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/

Re-enter new password: #确认新密码。
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100 #返回结果包含您设置的密码强度。
Change the password for root ? ((Press y|Y for Yes, any other key for No) :Y #您需要输入Y以确认使用新密码。

#新密码设置完成后,需要再次验证新密码。
New password:#再次输入新密码。

Re-enter new password:#再次确认新密码。

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) :Y #您需要输入Y,再次确认使用新密码。
  • 输入Y删除匿名用户
Remove anonymous users? (Press y|Y for Yes, any other key for No) :Y
Success.
  • 输入Y禁止使用root用户远程登录mysql
Disallow root login remotely? (Press y|Y for Yes, any other key for No) :Y
Success.
  • 输入Y删除test库以及用户对test库的访问权限
Remove test database and access to it? (Press y|Y for Yes, any other key for No) :Y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.
  • 输入Y重新加载授权表
Reload privilege tables now? (Press y|Y for Yes, any other key for No) :Y
Success.

All done!

2.7 配置php

  • 创建phpinfo.php文件

<网站根目录>是您在nginx.conf配置文件中location ~ .php$大括号内,配置的root参数值,如下图所示。网站根目录本文配置的网站根目录为/usr/share/nginx/html,因此需要运行以下命令新建phpinfo.php文件:

vim /usr/share/nginx/html/phpinfo.php

输入下列内容,函数phpinfo() 会展示PHP的所有配置信息。
<?php echo phpinfo(); ?>
  • 启动php-fpm
systemctl start php-fpm
  • 设置php-fpm开机自启动
systemctl enable php-fpm
  • 测试访问LNMP配置信息页面

在本地Windows主机或其他具有公网访问能力的Windows主机中,打开浏览器。
在浏览器的地址栏输入http://域名/phpinfo.php进行访问。
访问结果为php测试页即为成功

注:测试访问LNMP配置信息页面后,建议将phpinfo.php文件删除,消除数据泄露风险。

rm -rf /usr/share/nginx/html/phpinfo.php

三、lnmp环境安装常见问题

3.1 安装其它版本的nginx服务

  • 下载nginx 1.21.3
wget http://nginx.org/download/nginx-1.21.3.tar.gz
  • 安装Nginx相关依赖。
yum install -y gcc-c++
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel
  • 解压nginx 安装包
tar zxvf nginx-1.21.3.tar.gz
cd nginx-1.21.3
  • 编译源码
./configure \
 --user=nobody \
 --group=nobody \
 --prefix=/usr/local/nginx \
 --with-http_stub_status_module \
 --with-http_gzip_static_module \
 --with-http_realip_module \
 --with-http_sub_module \
 --with-http_ssl_module
make && make install
  • 启动nginx
/usr/local/nginx/sbin/nginx

3.2 php版本过低

以上安装中,php默认安装的版本是 PHP 7.0.33 ,在某些时候,php的版本过低,将导致LNMP环境出现很多问题,那么我们就需要升级php的版本

  • 卸载php
# 查看当前php已安装的包
rpm -qa|grep php 

会出现很多php相关包,基本只需要卸载几个名为common的包即可,其他同版本依赖会被全部删除,
删除php70w-common,70w版本的依赖包全部会被删除。

yum remove php70w-common
yum remove php74w-common

安装 PHP7.2

  • 安装 EPEL 软件包
yum install epel-release
  • 安装 remi 源
yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
  • yum 扩展包:
yum install yum-utils

启用 remi 仓库:

yum-config-manager --enable remi-php72
yum update
  • 安装 PHP7.2
yum install php72

安装 php-fpm 和一些其他模块

yum install php72-php-fpm php72-php-gd php72-php-json php72-php-mbstring php72-php-mysqlnd php72-php-xml php72-php-xmlrpc php72-php-opcache
  • php72 -v 查看安装结果
# php72 -v
PHP 7.2.34 (cli) (built: Dec 19 2022 16:12:02) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.2.34, Copyright (c) 1999-2018, by Zend Technologies
  • 设置开机自启
systemctl enable php72-php-fpm.service
  • 常用 php-fpm 命令
# 开启服务
systemctl start php72-php-fpm.service
# 停止服务
systemctl stop php72-php-fpm.service
# 查看状态
systemctl status php72-php-fpm.service
  • 设置php与nginx为同一个用户名及用户组
egrep '^(user|group)' /etc/nginx/nginx.conf

# 结果示例:
user  nginx;

# 编辑 /etc/opt/remi/php72/php-fpm.d/www.conf,修改执行 php-fpm 的权限:

vim /etc/opt/remi/php72/php-fpm.d/www.conf

# 设置用户和用户组为 nginx:

user = nginx
group = nginx

# 保存并关闭文件,重启 php-fpm 服务:
 systemctl restart php72-php-fpm.service
  • 路径参考
# php 安装路径
/etc/opt/remi/php72

# nginx 配置文件
/etc/nginx/nginx.conf

# nginx 默认项目路径
/usr/share/nginx/html

四、fastadmin部署

4.1 nodejs安装

  • 安装git、wget、screen
yum -y install git wget screen
  • 下载nodejs二进制包
 wget https://nodejs.org/download/release/v10.12.0/node-v10.12.0-linux-x64.tar.xz
  • 解压压缩包
tar xvf node-v10.12.0-linux-x64.tar.xz 
  • 更改包名
mv node-v10.12.0-linux-x64 /usr/local/nodejs
  • 添加环境变量
#  vim /etc/profile
#nodejs
export NODE_HOME=/usr/local/nodejs
export PATH=$NODE_HOME/bin:$PATH
  • 生效环境变量
source /etc/profile
  • 验证nodejs是否部署成功
# node -v
v10.12.0

# npm -v
6.4.1
  • Bower 安装
npm install -g bower

4.2 composer 安装

  • 使用命令下载
curl -sS https://getcomposer.org/installer | php
  • 下载之后设置环境变量
mv composer.phar /usr/local/bin/composer
  • 修改权限,否则执行会出错
chmod -R 777 /usr/local/bin/composer

4.3 fastadmin安装

  • 克隆FastAdmin到你本地
git clone https://gitee.com/karson/fastadmin.git

或

git clone https://github.com/karsonzhang/fastadmin.git
  • 进入fastadmin目录
cd fastadmin
  • 下载前端插件依赖包
bower install
  • 下载php依赖包
composer install

4.4 修改fastadmin中的数据库配置文件

  • 创建fastadmin数据库
create database fastadmin
  • 创建用户名及密码
create user 'fastadmin'@'%' identified by 'Fastadmin@123456';
  • 授权用户对数据库的权限
GRANT All ON fastadmin.* TO 'fastadmin'@'%'  identified by 'Fastadmin@123456';
  • 刷新权限
FLUSH PRIVILEGES;

4.5 nginx配置文件修改

server{
    listen       80;
    server_name  域名;
    # 路径 到public目录
    root   /usr/share/nginx/html/fastadmin/fastadmin/public;
    
    location / {
        index index.php;
        if (!-e $request_filename) {
       rewrite ^(.+?\.php)(/.+)$ /$1?s=$2 last;# 加上这一句
                    rewrite ^(.*)$ /index.php?s=$1 last;
                break;
        }
    }
    
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_read_timeout 300;
        fastcgi_index index.php;
        #fastcgi_param SCRIPT_FILENAME     /scripts$fastcgi_script_name;            
        #include fastcgi.conf;
        fastcgi_param  SCRIPT_FILENAME      $document_root$fastcgi_script_name;
        include fastcgi_params;            
    }
    
    error_page 404 /404.html;
        location = /40x.html {
    }
    
    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
    access_log /var/log/nginx/php.access.log main;
    error_log /var/log/nginx/php.error.log warn;
} 

4.6 访问fastadmin管理后台

  • 下载fastadmin管理后台代码

fastadmin 管理后台域名:https://域名/admin

将会提示下载fastadmin核心代码,下载链接如下:
https://www.fastadmin.net/download/full.html

  • 赋予权限
chmod -R 777 fastadmin站点根目录

五、fastadmin部署报错处理

5.1 composer安装依赖报错处理

The requested PHP extension ext-bcmath * is missing from your system. Install or enable PHP's bcmath extension.
  • 首先查看自己php版本
# php --version
PHP 7.2.34 (cli) (built: Oct  1 2020 13:37:37) ( NTS )
  • 安装对应版本的扩展
yum  -y install php72w-bcmath

注意:72w是代表7.2版本,安装的时候记得标明版本号

  • 安装完成后,重启下php
systemctl restart php-fpm
  • 查看php扩展是否安装成功
php -m

5.2 fastadmin登录后台

fastadmin 登录后台 你所浏览的页面暂时无法访问

  • 配置文件里,admin默认是不可访问的,开启就可以了
# vim fastadmin/application/config.php
// 禁止访问模块
 64     //'deny_module_list'       => ['common', 'admin'],
 65     'deny_module_list'       => [],

注:admin用于打开管理员页面,common用于打开菜单页面

  • nginx配置文件修改
  location / {
        index index.php;
        if (!-e $request_filename) {
        rewrite ^(.+?\.php)(/.+)$ /$1?s=$2 last;# 加上这一句
        rewrite ^(.*)$ /index.php?s=$1 last;
                break;
        }
    }

5.3 在FastAdmin中开启调试模式

开启调试模式的方法有两种:

  • 打开application/config.php,找到app_debug,将它的值置为true即可
  • 如果你有启用.env环境配置,修改其中app_debug的值为true即可

5.4 fastadmin 隐藏后台登陆地址

  • 1、首页修改application/config.php中deny_module_list的值,其中默认已经有common,我们添加admin,改成[‘common’, ‘admin’]
  • 2、然后修改项目public目录下的admin.php,将其改名为admin_d75KABNWt.php,我们可以将admin.php其中的admin改成任意随机的字符串,越长越好。

登录后台

通过以上的修改后,我们不能再通过www.yoursite.com/admin的形式登录后台了,此时我们可以采用www.yoursite.com/admin_d75KABNWt.php,其中admin_d75KABNWt.php就是我们任意修改的名称。
请保护好你后台的登录入口,千万别到处去粘贴,如果有泄漏后台入口,请再次尝试修改即可。

安全建议

通过上面的隐藏后台入口地址,我们已经加好了第一道门,以下是FastAdmin给大家的安全建议,为我们后台添加更多的安全防护。

  • 1、定期修改后台管理的登录入口和超级管理员密码,越复杂越好。
  • 2、开启后台登录验证码,开启方式:修改application/config.php底部中login_captcha,将它的值改为true
  • 3、修改后台超级管理员用户名,默认是admin,建议修改,修改方式直接在权限管理->管理员管理中修改
  • 4、移除冗余的管理员,早期FastAdmin中默认添加了几个管理员用于权限划分,建议删除。

5.5 fastadmin忘记后台密码

首先,我们先进入数据库。

然后,修改数据库。具体如下:

数据库修改fa_admin表的两个字段

密码 (password):c13f62012fd6a8fdf06b3452a94430e5

密码盐(salt):rpR6Bv

修改完这两项后,登录密码就变成了 123456

这样就顺利的找回fastadmin的后台密码了。

猜你喜欢

转载自blog.csdn.net/cljdsc/article/details/130361601