暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

lnmp 脚本,一键搭建 LNMP 环境!

蒲东平 2023-12-12
242

服务器 CentOS 系统搭建 lnmp 并配置 tp5 项目

(这篇文章是我刚入门编程时,做的笔记,可能有点儿乱,但是可能对于小白比较友好)

  • 官方网站地址:https://lnmp.org

  • 服务器上下载完整版安装包(下载版本为1.5)

wget http://soft.vpser.net/lnmp/lnmp1.5-full.tar.gz

==注意在服务器上下载时一定要用 root 账户下载并解压缩,不然会提示无法解压。(建议直接下载到 root 账户目录下)==

  • 下载完成之后,安装 lnmp

  1. 解压缩:
tar zxf lnmp1.5.tar.gz

  1. 切换到 lnmp 目录
cd lnmp1.5

  1. 使用脚本开始安装
./install.sh

  1. 按照提示选择 mysql 版本以及设置 mysql 密码、PHP 版本
  2. 使用命令检查是否安装完成
lnmp status

//会出现以下样子,则代表安装成功!
[root@iZuf6aig35m8ho0xq75ijnZ ~]# lnmp status
+-------------------------------------------+
| Manager for LNMP, Written by Licess |
+-------------------------------------------+
| https://lnmp.org |
+-------------------------------------------+
nginx (pid 7850 7848) is running...
php-fpm is runing!
SUCCESS! MySQL running (2915)

  1. 现在可以在浏览器中输入 IP 地址来访问一下,但是一般来说是访问不了的,因为阿里云默认关闭了80端口,此时需要进入阿里云管理后台设置安全组配置,添加安全组规则,将入方向的80端口设置,并且将数据库的3306端口也一并设置。此时再次访问 IP 地址就会显示 lnmp 安装成功界面!
  2. 服务管理
lnmp status 查看环境状态
lnmp start 开启服务
lnmp stop 关闭
lnmp reload 重加载
lnmp restart 重启

  1. 默认项目根目录在 home/wwwroot/default
    PHP的配置文件在:/usr/local/php/etc/php.ini
    Nginx的配置文件在:/usr/local/nginx/conf
  2. 将项目移动到项目根目录下面(使用 git 或者其他任何方式都可以)
  3. 访问项目时可能会提示
scandir() has been disabled for security reasons
//表示 lnmp 这个安装包禁用了 scandir() 这个(危险)函数

于是我们就要打开这个函数,打卡方式如下:

答:修改/usr/local/php/etc/php.ini 查找disable_functions 将后面要使用的函数删掉,保存,然后执行:/etc/init.d/php-fpm restart 重启后即可(lnmp 官方解答)==建议直接用 “;” 将 disable_functions 这一行全部注释掉,然后重启==

  1. 解决完以上危险函数报错之后可能会出现
file_put_content 权限不足
//解决方式如下:chmod 0777 -R runtime

  1. 出现 404 not found 是因为 url 重写的缘故
//进入 nginx 的配置目录
cd usr/local/nginx/conf/

//用 vim 创建一个该项目的配置文件 注意:必须要用 .conf 为后缀
vim my_cake_test.conf

文件中要写如下内容:(因为 tp5 的访问会基于 public 目录下,因此我们要参考 nginx 的二级目录)

tp5.0 参考网址:架构-> url 访问->最底端 "url 重写"->nginx二级目录

https://www.kancloud.cn/manual/thinkphp5/177576

复制 tp5.0 手册一下内容

location youdomain/ {
if (!-e $request_filename){
rewrite ^/youdomain/(.*)$ youdomain/index.php?s=/$1 last;
}
}

修改成

location my_cake_test/public/ {
if (!-e $request_filename){
rewrite ^/my_cake_test/public/(.*)$ my_cake_test/public/index.php?s=/$1 last;
}
}

==注意这里的 youdomain 必须修改成 基于项目根目录的项目地址。
比如我的项目文件夹 my_cake_test 在 home/wwwroot/default/ 下面
但是需要访问到 my_cake_test 目录下的 public 目录 才可以访问我的项目
于是修改就是以上的配置==

之后保存 my_cake_test.conf 配置文件的修改

再打开 nginx的配置文件 nginx.conf

vim nginx.conf

在 nginx.conf 文件中找到 include enable-php.conf; 这一行

在 include enable-php.conf; 这一行下面 新增一行

include my_cake_test.conf;

然后保存并退出

重启 nginx 命令为:lnmp nginx restart


  1. 使用 Navicat 远程连接数据库 由于 lnmp 默认设置了 iptables 防火墙,于是还需要进一步修改
//将所有iptables以序号标记显示,执行:
iptables -L -n --line-numbers

//比如要删除INPUT里序号为8的规则(要删除OUTPUT的话就改成OUTPUT,以此类推),执行:
iptables -D INPUT 8

//我们可以看到 num = 6 就是3306端口 于是我们就直接删除 num = 6 按照以下操作
[root@iZuf6aig35m8ho0xq75ijnZ phpmyadmin]# iptables -L -n --line-numbers
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
2 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
3 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
4 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
5 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:443
6 DROP tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306
7 ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0 icmptype 8

Chain FORWARD (policy ACCEPT)
num target prot opt source destination

Chain OUTPUT (policy ACCEPT)
num target prot opt source destination
[root@iZuf6aig35m8ho0xq75ijnZ phpmyadmin]# iptables -D INPUT 6


  1. 再次使用 Navicat 测试连接,发现还是连不上去。是因为没有开启任意主机连接

我们就需要使用 phpmyadmin 添加账户

在 phpmyadmin 界面找到 “账户” 按钮,找到“新建”下面的“新增用户账号”,User name 设置为你想要用 Navicat 连接的用户名 比如我设置的为 alex 也可以使用 默认的 root 账户(不修改)

Host name 设置为 百分号 % 代表任意主机可以连接 之后设置密码 在下面的 "全局权限"按钮下 点击 “全选”按钮 再在最下面找到 “执行”按钮 并点击 就大功告成了!

  1. 添加虚拟主机,服务器和域名绑定
//01、使用添加虚拟主机的命令

[root@iZuf6aig35m8ho0xq75ijnZ vhost]# lnmp vhost add
+-------------------------------------------+
| Manager for LNMP, Written by Licess |
+-------------------------------------------+
| https://lnmp.org |
+-------------------------------------------+

//02、这里要输入要添加网站的域名,这里我以 www.drling.xin 为例子,回车

Please enter domain(example: www.lnmp.org): www.drling.xin
Your domain: www.drling.xin

//03、这里询问是否添加更多域名,直接再输入要绑定的域名,这里我们将 drling.xin 也绑上,多个域名空格隔开,如不需要绑其他域名就直接回车

//(注:带www和不带www的是不同的域名,如需带www和不带的www的域名都
//访问同一个网站需要同时都绑定)
//在这里也就是说 访问 drling.xin 和访问 www.drling.xin 都访问的是同一个目录

Enter more domain name(example: lnmp.org *.lnmp.org): drling.xin
domain list: drling.xin
Please enter the directory for the domain: www.drling.xin

//04、这里是告诉我们 默认的项目根目录位置,直接回车即可

Default directory: home/wwwroot/www.drling.xin:
Virtual Host Directory: home/wwwroot/www.drling.xin

//05、是否使用伪静态 选择使用
Allow Rewrite rule? (y/n) y
Please enter the rewrite of programme,
wordpress,discuzx,typecho,thinkphp,laravel,codeigniter,yii2 rewrite was exist.

//06、是否写入其它框架啥的 直接回车 表示不用
(Default rewrite: other):
You choose rewrite: other

//07、这个不知道是啥意思 直接选择的 y
Enable PHP Pathinfo? (y/n) y
Enable pathinfo.

//08、是否开启日志 选择开启 y
Allow access log? (y/n) y
Enter access log filename(Default:www.drling.xin.log):
You access log filename: www.drling.xin.log

//09、是否创建 mysql 选择不创建 n
Create database and MySQL user with same name (y/n) n

//10、是否使用 SSL 也就是 https 选择不要使用,因为可能会需要使用到证书
Add SSL Certificate (y/n) n

//11、现在就创建成功了,等待提示即可
Press any key to start create virtul host...

Create Virtul Host directory......
set permissions of Virtual Host directory......
You select the exist rewrite rule:/usr/local/nginx/conf/rewrite/other.conf
Test Nginx configure file......
nginx: the configuration file usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file usr/local/nginx/conf/nginx.conf test is successful
Reload Nginx......
Gracefully shutting down php-fpm . done
Starting php-fpm done
================================================
Virtualhost infomation:
Your domain: www.drling.xin
Home Directory: home/wwwroot/www.drling.xin
Rewrite: other
Enable log: yes
Create database: no
Create ftp account: no
================================================

//12、切换到项目根目录
[root@iZuf6aig35m8ho0xq75ijnZ vhost]# cd home/wwwroot
[root@iZuf6aig35m8ho0xq75ijnZ wwwroot]# ls

//13、可以看到和默认的项目根目录平级有一个 刚刚系统默认创建的文件夹
default www.drling.xin

//14、切换到 www.drling.xin 文件夹,并创建 index.php
文件,浏览器访问 www.drling.xin 网址的时候就是 index.php 中的内容
直接使用服务器的 IP 地址,会直接访问到 default 文件夹下的内容
[root@iZuf6aig35m8ho0xq75ijnZ wwwroot]# cd www.drling.xin/
[root@iZuf6aig35m8ho0xq75ijnZ www.drling.xin]# vim index.php

  1. 配置此时的配置文件
//01、切换到 nginx 的配置目录
[root@iZuf6aig35m8ho0xq75ijnZ www.drling.xin]# cd usr/local/nginx/conf

//02、选择进入到 vhost 文件夹
[root@iZuf6aig35m8ho0xq75ijnZ conf]# cd vhost/

//03、可以看到系统已经默认创建了配置文件
[root@iZuf6aig35m8ho0xq75ijnZ vhost]# ls
www.drling.xin.conf

//04、打开 www.drling.xin.conf 配置文件
[root@iZuf6aig35m8ho0xq75ijnZ vhost]# vim www.drling.xin.conf

//05、找到 include enable-php-pathinfo.conf;
//这一行,并在这一行下面添加之前写的 my_cake_test.conf
//配置文件(这里 my_cake_test.conf 配置文件虽然和 vhost
//文件夹属于平级目录,但是写法就是如下),也可以自己重新在写一次

include enable-php-pathinfo.conf;

include my_cake_test.conf;

//06、重启 nginx
[root@iZuf6aig35m8ho0xq75ijnZ vhost]# lnmp nginx restart
+-------------------------------------------+
| Manager for LNMP, Written by Licess |
+-------------------------------------------+
| https://lnmp.org |
+-------------------------------------------+
Stoping nginx... done
Starting nginx... done

//07、此时项目虽然可以访问,但是项目网址中还含有 public
//这一级文件夹,可以复制一份 my_cake_test.conf 配置文件
[root@iZuf6aig35m8ho0xq75ijnZ conf]# cp my_cake_test.conf www.drling.xin.conf

//08、修改伪静态,将 www.drling.xin.conf 文件中删除 /public
[root@iZuf6aig35m8ho0xq75ijnZ conf]# vim www.drling.xin.conf

//09、切换到 vhost 文件夹 并修改 www.drling.xin.conf
//配置文件,找到 root /home/wwwroot/www.drling.xin;
//这一行在之后加上 public 这一级目录
//变成:root /home/wwwroot/www.drling.xin/public;

[root@iZuf6aig35m8ho0xq75ijnZ conf]# cd vhost/

[root@iZuf6aig35m8ho0xq75ijnZ vhost]# vim www.drling.xin.conf

//10、重启 nginx
[root@iZuf6aig35m8ho0xq75ijnZ vhost]# lnmp nginx restart
+-------------------------------------------+
| Manager for LNMP, Written by Licess |
+-------------------------------------------+
| https://lnmp.org |
+-------------------------------------------+
Stoping nginx... done
Starting nginx... done



最后说个事
公号算法变了,为防止看不到我的更新
大家帮忙加个星标
点击上方的公众号卡片
再点右上角三个点
就能看到设为星标
算我跪下来求你们

往期精选:

微信又出了款重磅APP,体验下?

还在使用默认的微信图标?赶紧换个吧!

我的微信和你们的不一样!?

文章转载自蒲东平,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论