写在前面:所有操作目录均是在/usr/local/下执行的,拿到值守命令,直接在/usr/local/下执行,无需创建其他文件夹,系统自动创建lnmp1.6
1、去LNMP官网:https://lnmp.org/install.html 直接在服务器wget安装LNMP1.5版本
2、我选择的是无人值守版 LNMP: 地址:https://lnmp.org/auto.html
安装步骤:
1、使用putty或类似的SSH工具登陆VPS或服务器;
登陆后运行:screen -S lnmp
如果提示screen: command not found 命令不存在可以执行:yum install screen 或 apt-get install screen安装,详细内容参考screen教程。
2、下载并安装LNMP一键安装包:
您可以选择使用下载版(推荐美国及海外VPS或空间较小用户使用)或者完整版(推荐国内VPS使用,国内用户可用在下载中找国内下载地址替换),两者没什么区别,只是完整版把一些需要的源码文件预先放到安装包里。
安装LNMP稳定版
如需无人值守安装,请使用 无人值守命令生成工具,或查看无人值守说明教程
生成命令:
1.5=>php5.6
wget http://soft.vpser.net/lnmp/lnmp1.5.tar.gz -cO lnmp1.5.tar.gz && tar zxf lnmp1.5.tar.gz && cd lnmp1.5 && LNMP_Auto="y" DBSelect="3" DB_Root_Password="lnmp.org" InstallInnodb="y" PHPSelect="5" SelectMalloc="1" ./install.sh lnmp
1.6=>php7
wget http://soft.vpser.net/lnmp/lnmp1.6.tar.gz -cO lnmp1.6.tar.gz && tar zxf lnmp1.6.tar.gz && cd lnmp1.6 && LNMP_Auto="y" DBSelect="3" DB_Root_Password="lnmp.org" InstallInnodb="y" PHPSelect="6" SelectMalloc="1" ./install.sh lnmp
2.1 如果报错:wget command not found,请直接执行 yum -y install wget 。
3、安装完毕,查看是否成功:
4、错误排查:解决LNMP502和500问题,请查看下面地址 https://lnmp.org/faq.html
首先查看LNMP常见错误和解决方案,至少能节省你三分之二的拍错时间(地址:https://lnmp.org/faq.html)
5、开始配置:
我没有使用它的脚本进行配置的。我是直接配置 vhost/项目.conf文件 (必须先查看php-fpm.conf的listen)
6、重点提醒:如果你的PHP项目是laravel、tp5等 项目根目录指向/public/ 下的,一定要执行下面步骤,否则网站一直报错 500.(.user.ini直接删除)
##防跨目录设置##
LNMP 1.1及之前的版本使用php.ini里面,open_basedir设置
LNMP 1.2及更高版本防跨目录功能使用.user.ini,该文件在网站根目录下,可以修改.user.ini 里面的open_basedir的值来设置限制访问的目录或删除来移除防跨目录的设置。
.user.ini文件无法直接修改,如要修或删除需要先执行:chattr -i /网站目录/.user.ini
可以使用winscp文件管理、vim编辑器或nano编辑器进行修改。
删除的话rm -f /网站目录/.user.ini 就可以。
修改完成后再执行:chattr +i /网站目录/.user.ini
.user.ini不需要重启一般5分钟左右生效,也可以重启一下php-fpm立即生效。
如果要更改网站目录必须要按上述方法修改防跨目录的设置,否则肯定报错!!
LNMP 1.4上如果不想用防跨目录或者修改.user.ini的防跨目录的目录还需要将 /usr/local/nginx/conf/fastcgi.conf 里面的fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/tmp/:/proc/"; 在该行行前添加 # 或删除改行,需要重启nginx。
LNMP 1.4上也可以直接使用lnmp1.4/tools/ 目录下的 ./remove_open_basedir_restriction.sh 进行移除。
在Thinkphp、codeigniter、Laravel等框架下,网站目录一般是在public下,但是public下的程序要跨目录调用public上级目录下的文件,因为LNMP默认是不允许跨目录访问的,所以都是必须要将防跨目录访问的设置去掉,有时候这些框架类的程序提示500错误也可能是这个问题引起的。
7、其他的伪静态和pathinfo ,按照nginx的惯例配置进行设置即可。
通用版:tp5、laravel5都可以使用下面的nginx配置(使用ip:port访问或者域名:port)
server { listen 8054; server_name localhost; root /home/wwwroot/default/erpv31/public/; #index index.php index.html index.htm; location / { index index.html index.htm index.php; autoindex off; if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=$1 last; break; } } location ~ \.php(.*)$ { fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; fastcgi_split_path_info ^((?U).+\.php)(/?.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } access_log /home/wwwlogs/erpv31-access.log; error_log /home/wwwlogs/erpv31-error.log; }
thinkcmf5的nginx惯例配置:
server
{
listen 80;
server_name api.***.xin;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/api.***.xin/public;
location / {
index index.php index.html index.htm;
#如果请求既不是一个文件,也不是一个目录,则执行一下重写规则
if (!-e $request_filename)
{
#地址作为将参数rewrite到index.php上。
rewrite ^/(.*)$ /index.php?s=$1;
#若是子目录则使用下面这句,将subdir改成目录名称即可。
#rewrite ^/subdir/(.*)$ /subdir/index.php?s=$1;
}
}
location ~ \.php(.*)$ {
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
#fastcgi_split_path_info ^(.+\.php)(.*)$;
#fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /.well-known {
allow all;
}
location ~ /\.
{
#deny all;
}
access_log /home/wwwlogs/access.log2.log;
}