我正在尝试将PHP应用程序部署到我的DigitalOcean服务器,使用apache2和php5运行Ubuntu 14.04
首先让我提一下,应用程序使用内置的php-server
在localhost上完美运行我可以通过访问" site.com /"来点击我的index.php没有问题,但它在每个其他php文件上给我404。直接访问site.com/index.php我也得到404 我的应用程序使用一个简单的PHP路由器重定向到处理视图的控制器。
apache服务器设置为指向/ var / www / my_site / public
这是文件结构: File structure
这是我的index.php:
<?php
use App\Core\Autoloader;
use App\Core\Container;
use App\Core\Router;
/*************
* CONFIG *
*************/
error_reporting(-1);
// dev only
ini_set('display_errors', 1);
define('SCRIPT_DEBUG', true);
define('VIEW_DIR', realpath(__DIR__ . '/../views'));
define('CONFIG_DIR', realpath(__DIR__ . '/../config/'));
/***********
* INIT *
***********/
session_start();
require __DIR__ . '/../App/Core/Autoloader.php';
require CONFIG_DIR . '/db.php';
$dbh = new PDO('mysql:host=127.0.0.1;dbname=' . $db_name, $db_user, $db_pass);
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$loader = new Autoloader();
$loader->addNamespace('App', __DIR__ . '/../App');
$loader->register();
$container = new Container();
$container->bindArguments('App\\Model\\User', ['dbh' => $dbh]);
$container->bindArguments('App\\Model\\Image', ['dbh' => $dbh]);
/**************
* ROUTING *
**************/
$router = new Router();
$router->addRoute('GET', '/', ['App\\Controller\\SessionController', "redirect" ]);
$router->addRoute('GET', '/log_in', ['App\\Controller\\SessionController', "newSession" ]);
$router->addRoute('GET', '/log_out', ['App\\Controller\\SessionController', "destroy" ]);
$router->addRoute('GET', '/sign_up', ['App\\Controller\\UserController', "signUp" ]);
$router->addRoute('GET', '/edit_user', ['App\\Controller\\UserController', "edit" ]);
$router->addRoute('GET', '/account', ['App\\Controller\\UserController', "account" ]);
$router->addRoute('GET', '/users' , ['App\\Controller\\UserController', "users" ]);
$router->addRoute('GET', '/upload', ['App\\Controller\\ImageController', "upload" ]);
$router->addRoute('GET', '/gallery', ['App\\Controller\\ImageController', "gallery" ]);
$router->addRoute('POST', '/log_in', ['App\\Controller\\SessionController', "create" ]);
$router->addRoute('POST', '/sign_up', ['App\\Controller\\UserController', "create" ]);
$router->addRoute('POST', '/delete_user', ['App\\Controller\\UserController', "destroy" ]);
$router->addRoute('POST', '/edit_user', ['App\\Controller\\UserController', "update" ]);
$router->addRoute('POST', '/upload', ['App\\Controller\\ImageController', "save" ]);
$router->addRoute('POST', '/delete_image', ['App\\Controller\\ImageController', "destroy" ]);
// Convert i.e. "/foo%40bar?id=1" to "/foo@bar"
$uri = rawurldecode(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
$route = $router->match($_SERVER['REQUEST_METHOD'], $uri);
if ($route === null) {
$route = [
'handle' => ['App\\Controller\\ErrorController', 'error404'],
'arguments' => []
];
}
$controller = $container->create($route['handle'][0]);
$container->call([$controller, $route['handle'][1]], $route['arguments']);