vagrant学习:Nginx出现的问题和apache的配置

本文详细介绍了如何在Nginx和Apache环境下部署ThinkPHP5.1框架,包括环境搭建、配置步骤及常见错误排查。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文根据慕课网的视频教程练习,感谢慕课网!

慕课视频学习地址:https://www.imooc.com/video/14218。

慕课的参考文档地址:https://github.com/apanly/mooc/tree/master/va

(1)下载ThinkPHP5.1的框架:

如果使用git克隆框架,需要先安装git:

sudo apt-get install  git

创建web目录:

mkdir /home/www

在www目录:

cd /home/www
sudo git clone https://github.com/top-think/think.git tp5

然后进入自动创建的tp5目录,进行克隆核心代码:

cd tp5
sudo git clone https://github.com/top-think/framework thinkphp

(2)如果web服务器是nginx:

配置nginx:

cd /etc/nginx/conf.d/

创建tp5.conf
复制代码

server{
    server_name study.tp5.com;
    root /home/www/tp5/public;
    index index.php index.html;
    location / {
        if ( -f $request_filename){
            break;
        }
        if ( !-e $request_filename){
            rewrite ^/(.*)$ /index.php/$1 last;
            break;
        }
    }


    location ~ \.php{
        set $script $uri;
        set $path_info "";
        if ($uri ~ "^(.+\.php)(/.+)"){
            set $script $1;
            set $path_info $2;
        }
        include fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param PATH_INFO $path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $script;
        try_files $uri = 404;
        
    }
}

复制代码
然后重启nginx:

方式一:sudo /etc/init.d/nginx restart

方式二:sudo service nginx restart

如果重启nginx失败,例如:

sudo /etc/init.d/nginx restart

* Restarting nginx nginx               [fail]

解决方法,使用 sudo nginx -t查看错误原因:

nginx: [emerg] unknown directive "if(" in /etc/nginx/conf.d/tp5.conf:6
nginx: configuration file /etc/nginx/nginx.conf test failed

可以看出是if(的地方有问题,if和(中间增加空格后错误消失。

  • Restarting nginx nginx [OK]
    配置虚拟域名:
sudo vim /etc/hosts
ip地址  study.tp5.com

然后测试:

复制代码

vagrant@vagrant-ubuntu-trusty:/etc$ curl -I 'study.tp5.com'
HTTP/1.1 502 Bad Gateway
Server: nginx/1.4.6 (Ubuntu)
Date: Thu, 30 Aug 2018 02:23:47 GMT
Content-Type: text/html
Content-Length: 181
Connection: keep-alive

发现报502错误,查看nginx的错误日志,

打开nginx的配置文件,找到错误日志的文件的位置,然后打开查看具体错误。

cat /etc/nginx/nginx.conf

查找error_log的配置:

error_log /var/log/nginx/error.log;
cat /var/log/nginx/error.log
[error] 1443#0: *3 connect() failed (111: Connection refused) while connecting to upstream, 
client: 192.168.8.172, server: study.tp5.com, request: "HEAD / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "study.tp5.com"

连接被拒绝,进入php的配置 :

cd /etc/php5/fpm/pool.d

编辑www.conf:

sudo vim www.conf

找到 listen监听的位置:

listen = /var/run/php5-fpm.sock

sock域方式没有端口方式稳定,修改为端口监听。

listen = 127.0.0.1:9000

重启fpm:

方式一:sudo service php5-fpm restart

方式二:sudo /etc/init.d/php5-fpm restart

再次访问域名,发现报500错误:

[error] 884#0: *3 FastCGI sent in stderr: "PHP message: PHP Parse error:  syntax error, unexpected '.', 
expecting '&' or variable (T_VARIABLE) in /home/www/tp5/thinkphp/library/think/Loader.php on line 391" while reading response header from upstream, 
client: 192.168.8.172, server: study.tp5.com, request: "HEAD / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "study.tp5.com"

打开文件:thinkphp/library/think/Loader.php,找到391行,发现

public static function factory($name, $namespace = '', ...$args)

这个$args参数前多了三个点,可能是这个错误,于是就删除这三个点,重新访问 study.tp5.com

**补充:**如果还是不行,继续修改thinkphp/library/think/Facade.php,找到90行,去掉 三个点(…)

    public static function instance(...$args)

显示 ThinkPHP V5.1的界面,访问成功!

之前以为是配置问题,但是不是,是tp的代码问题,这里应该还有其他解决方法,大家有其他看法请多多指教

(3)如果web服务器是apache:

进入apache的配置目录,新建一个单独的tp5的配置文件,
cd /etc/apache2/sites-enabled
sudo touch tp5.conf
编辑文件:

<VirtualHost *:8888>
        ServerName study.tp5.com
        DocumentRoot /home/www/tp5/public/
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
   </VirtualHost>

然后就可以访问了,接下来配置url重写,

tp5框架下的目录public下的.htaccess默认是不生效的,需要开启apache的重写模块:

sudo a2enmod rewrite

然后修改apache的配置:

<Directory />
             Options FollowSymLinks
             AllowOverride None #修改成AllowOverride ALL 打开rewrite
             #Require all denied #把这行屏蔽掉,拒绝一切链接
</Directory>

然后重启apache

sudo service apache2 restart 或者 sudo /etc/init.d/apache2 restart

测试 http://study.tp5.com:8888,显示:

?
ThinkPHP V5.1
12载初心不改(2006-2018) - 你值得信赖的PHP框架

配置成功!

本文转载于https://www.cnblogs.com/gyfluck/p/9555760.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值