Centos7+nginx上部署laravel(同时解决laravel访问新建路由出现404的问题、No input file specified的问题、URL模式问题)

LNMP一键安装https://lnmp.org/install.html来安装好centos下的环境,本文以此为服务器环境的例子。

-------------------------------------------------------------

laravel访问新建路由出现404问题、No input file specified问题这两个问题可能是因为用了一键lnmp,不知道实际nginx配置造成的,百度问题也很难解决,部署过程中也碰到了不少莫名其妙等问题。

-

#1. 一般直接在nginx.conf里面添加如下即可完成URL的路由访问

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

但是,我用的是一键lnmp https://blog.csdn.net/weixin_41827162/article/details/82914526,里面的nginx添加了如上代码,新建路由访问时依然404。

最终在nginx.conf里面找到一句include enable-php.conf,里面有一句 try_files $uri =404; 是这句每次访问新路由时都是404。

[root@localhost conf]# vi enable-php.conf

注释掉try_files $uri =404;就行了就可以了,如下#。

        location ~ [^/]\.php(/|$)
        {
            #try_files $uri =404;
            fastcgi_pass  unix:/tmp/php-cgi.sock;
            fastcgi_index index.php;
            include fastcgi.conf;
        }

重启nginx即可访问:

[root@localhost conf]# service nginx restart

-

#2. 接下来解决No input file specified  问题:

这个问题等出现是因为 主机未开启 pathinfo 函数导致的。需要启用 cgi.fix_pathinfo 参数

参考:https://blog.csdn.net/a787031584/article/details/53400250

1)进入php.ini文件。将cgi.fix_pathinfo的值改成1

[root@localhost ~]# find / -name php.ini
/usr/local/php/etc/php.ini
[root@localhost ~]# vi /usr/local/php/etc/php.ini

2)然后到配置域名解析的文件下(一般是以域名命名的配置文件)。加上这三句:

比如我加在nginx.conf下【如果因为添加如下而导致nginx不能正常启动,请注释下面的代码的第一行即可,第二行和第三行不需要注释】:

astcgi_split_path_info ^(.+\.php)(/.+)$;  
fastcgi_param   PATH_INFO   $fastcgi_path_info;  
fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name; 

-

3. 下载laravel最新版离线包:https://laravelacademy.org/resources-download ,

或者使用我的完整离线包:  https://makeoss.oss-cn-hangzhou.aliyuncs.com/laravel58_run.7z

a. 将laravel离线包上传到centos上,并将laravel的bootstrap和storage两个目录设置成权限777;

b. 在php.ini里面设置cgi.fix_pathinfo=1来打开php报错调试;

c. 在laravel/config/app.php的调试打开:

'debug' => env('APP_DEBUG', true)

d. 在laravel目录添加.env文件,然后在此目录运行php artisan key:generate ,系统会自动生成一个key。

-

4. 最终laravel部署完毕,新建路由访问成功:

ThinkPHP5也成功(感叹一下,TP真的比laravel好部署万倍,各种省心):

-

-------拓展--------------

pathinfo URL写法:

  • laravel:/index.php/参数
  • TP5:/?s=/参数   或者  /index.php?s=/参数

TP5等URL模式比较方便,可以适用于pathinfo和兼容模式;laravel以上配置的是pathinfo模式,而且官方推荐等也是pathinfo

模式。

-------解决其他报错(从GiT上面拉下来的)----------

Warning: require(/home/wwwroot/default/laravel/public/../vendor/autoload.php): failed to open stream: No such file or directory in /home/wwwroot/default/laravel/public/index.php on line 24

Fatal error: require(): Failed opening required '/home/wwwroot/default/laravel/public/../vendor/autoload.php' (include_path='.:/usr/local/php/lib/php') in /home/wwwroot/default/laravel/public/index.php on line 24

我从GitHub上面clone下来的laravel,结果不含vendor目录,vendor里面的laravel目录就相当于tp5.1里面的thinkPHP目录,所以要么手动添上框架的framework,要么下载完整离线框架包。

Laravel学院提供的相关资源下载:https://laravelacademy.org/resources-download

tp5.1也会有类似的报错,添加核心框架即可。

-

nginx.conf里面的全部内容如下:

user  www www;

worker_processes auto;
worker_cpu_affinity auto;

error_log  /home/wwwlogs/nginx_error.log  crit;

pid        /usr/local/nginx/logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;

events
    {
        use epoll;
        worker_connections 51200;
        multi_accept off;
        accept_mutex off;
    }

http
    {
        include       mime.types;
        default_type  application/octet-stream;

        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;

        sendfile on;
        sendfile_max_chunk 512k;
        tcp_nopush on;

        keepalive_timeout 60;

        tcp_nodelay on;

        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";

        #limit_conn_zone $binary_remote_addr zone=perip:10m;
        ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.

        server_tokens off;
        access_log off;

server
    {
        listen 80 default_server reuseport;
        #listen [::]:80 default_server ipv6only=on;
        server_name _;
        index index.html index.htm index.php;
        root  /home/wwwroot/default;
		
		#astcgi_split_path_info ^(.+\.php)(/.+)$;  
		fastcgi_param   PATH_INFO   $fastcgi_path_info;  
		fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name; 

        #error_page   404   /404.html;

        # Deny access to PHP files in specific directory
        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

         location / {
                try_files $uri $uri/ /index.php?$query_string;
         }

        include enable-php.conf;

        location /nginx_status
        {
            stub_status on;
            access_log   off;
        }

        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.log;
    }
include vhost/*.conf;
}

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我知道你的问题了。针对你的问题,我可以给你一些简单的部署步骤: 1.安装nginx和PHP 在CentOS 6.8中安装nginx和PHP的方式如下: ``` sudo yum install nginx sudo yum install php-fpm ``` 2.配置nginx 在配置nginx之前,你需要备份一下默认的nginx配置文件。备份命令如下: ``` sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak ``` 然后,你可以通过编辑/etc/nginx/nginx.conf文件来配置nginx。 下面是一个简单的nginx配置示例,可以将它添加到/etc/nginx/nginx.conf文件中: ``` user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; 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; keepalive_timeout 65; include /etc/nginx/conf.d/*.conf; server { listen 80; server_name localhost; root /usr/share/nginx/html; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php; } location ~ \.php$ { try_files $uri =404; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } } ``` 在这个示例配置中,我们将nginx的根目录设置为/usr/share/nginx/html,并且将index.php添加到默认文档列表中。同时,我们还配置了一个location块,用于处理PHP文件。 3.配置PHP 在配置PHP之前,你需要备份一下默认的php-fpm配置文件。备份命令如下: ``` sudo cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.conf.bak ``` 然后,你可以通过编辑/etc/php-fpm.d/www.conf文件来配置PHP。 下面是一个简单的PHP配置示例,可以将它添加到/etc/php-fpm.d/www.conf文件中: ``` [www] user = nginx group = nginx listen = /var/run/php-fpm/php-fpm.sock listen.owner = nginx listen.group = nginx pm = dynamic pm.max_children = 50 pm.start_servers = 5 pm.min_spare_servers = 5 pm.max_spare_servers = 35 ``` 在这个示例配置中,我们将PHP-FPM的用户和组设置为nginx,并将PHP-FPM的监听套接字设置为/var/run/php-fpm/php-fpm.sock。 4.重启服务 完成配置后,你需要重启nginx和php-fpm服务,以使配置生效。重启命令如下: ``` sudo systemctl restart nginx sudo systemctl restart php-fpm ``` 到这里,就完成了nginx+PHP的简单部署。同时,为了进一步提升性能,你可以使用Nginx+PHP+FastCGI加速模式

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值