Slim 2.6.2 render() Only Works for Index.html - php

I'm currently learning about the Slim framework over at TeamTreehouse.com, and ran into an issue that I haven't been able to resolve.
At this point in the project, we have installed Slim via Composer and setup .htaccess and index.php files in our document root (which on my computer is /home/daniel/src/public_html/treehouse/build_websites_php/).
In a templates folder we have index.html and contact.html. Here is the layout of the folders.
DocumentRoot (/home/daniel/src/public_html/treehouse/build_websites_php/)
index.php
.htaccess
composer.json
composer.lock
vendor/
templates/
index.html
contact.html
In index.php, I instantiate a new Slim object:
$app = new \Slim\Slim();
And then call the get() method and then call render() to render the index.html and contact.html pages when the url is localhost/treehouse/build_websites/ and localhost/treehouse/build_websites/contact, respectively.
$app->get('/', function () use($app) {
$app->render('index.html');
});
$app->get('/contact', function () use($app) {
$app->render('contact.html');
});
Then run the app:
$app->run();
My index.html page shows up fine, but I get a 404 error (not through Slim, just the server's default) when I try and visit the /contact url. Here are some specs from my system:
Ubuntu 16.04.1 LTS
Apache 2.4.18
Slim 2.6.2
PHP 7.0.8
Anything in my /home/daniel/src/public_html/ directory can be accessed by Apache, as I've run PHP scripts from in there for the past year.
I've tried the suggestions from here (and restarted the server after each update to conf.d or other files) and have had no luck.
Any help would be greatly appreciated, I've only been using PHP/Ubuntu/Apache for about a year, so I'm probably missing something obvious!
Here is the index.php file:
<?php
require 'vendor/autoload.php';
$app = new \Slim\Slim();
$app->get('/', function () use($app) {
/* When using render(), the url localhost/treehouse/build_websites_php/ to
gets you the home page. */
$app->render('index.html');
});
/* This SHOULD bring up the contact page at url
localhost/treehouse/build_websites_php/contact, but it doesn't! */
$app->get('/contact', function () use($app) {
$app->render('contact.html');
});
$app->run();
?>
Here is the .htacess file:
RewriteEngine On
# Some hosts may require you to use the `RewriteBase` directive.
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
# RewriteBase /home/daniel/src/public_html/treehouse/build_websites_php/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
And here are various conf.d files for Apache:
/etc/apache2/apache2.conf
# Sets the default security model of the Apache2 HTTPD server. It does
# not allow access to the root filesystem outside of /usr/share and /var/www.
# The former is used by web applications packaged in Debian,
# the latter may be used for local directories served by the web server. If
# your system is serving content from a sub-directory in /srv you must allow
# access here, or in any related virtual host.
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /home/daniel/src/public_html>
Order allow,deny
Allow from all
Require all granted
</Directory>
I tried adding AllowOverride All as suggested here to the last directive and then I wasn't able to access PHP files from the server at all and got a 500 error instead of a 404 error.
/etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
/etc/apache2/sites-available/mysite.conf
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster#localhost
DocumentRoot /home/daniel/src/public_html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

OK wow I finally figured it out thanks to Mika pointing me in the right direction. It turns out, my /etc/apache2/sites-available/mysite.conf file needed the following directive:
<Directory /home/daniel/src/public_html >
AllowOverride All
</Directory>
I had tried adding that directive to /etc/apache2/apache2.conf in addition to the other directives like so:
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /home/daniel/src/public_html>
Order allow,deny
Allow from all
Require all granted
AllowOverride All #THIS DIDN'T WORK
</Directory>
But the AllowOverride All from above through a 500 error from the server. Apparently, it had to be by itself in the /etc/apache2/sites-available/mysite.conf file, who knew!
I also ran sudo a2enmod rewrite && sudo /etc/init.d/apache2 restart to make sure the mod_rewrite was loaded after discovering this error message (thanks to Mika for pointing out to check the log files!):
[Sun Nov 13 10:37:51.054347 2016] [core:alert] [pid 10979] [client ::1:51900] /home/daniel/src/public_html /treehouse/build_websites_php/.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration
I did that before adding the AllowOverride All directive, so I'm not sure if it played a part in solving the problem, but I figured I would document it for any interested.
These sites had valuable information on how to finally solve the issue:
1 - solved mod_rewrite issue
2 - solved where to place the AllowOverride All directive
3 - Slim troubleshooting for .htaccess

It looks like your Apache settings do not allow the .htaccess file to override any settings. Add something like the following to apache2.conf.
<Directory /home/daniel/src/public_html>
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>

Related

Problems accessing PHP project in apache2

I am developing a platform and am using an open-source php project, I cloned it off github.
So, the main problem I've been having is with Apache2, for some reason even if I place the file containing my PhP project under var/www/html, I specified the config file of apache to read the php index, but it is not opening it. The only result I am getting is the welcome webpage of apache2. PHP is working just fine, since Phpmyadmin and the test website of php that I placed in the same /html works as well. Am I missing a configuration setting within apache?
I also enabled a custom config file for my page and added it to hosts here is the file I enabled within apache2:
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin admin#moritologon.com
ServerName moritologon.com
DocumentRoot /home/Sistema_php/online-food-ordering-system-in-php-master/
<Directory /home/Sistema_php/online-food-ordering-system-in-php-master/>
Options Indexes FollowSymLinks
AllowOverride all
Require all granted
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
vim: syntax=apache ts=4 sw=4 sts=4 sr noet

Laravel Site in Folder of Another Laravel Site .htaccess?

I'm trying to find the best way to go about this so I'd appreciate any help. I've got a primary Laravel site in my /var/www/html folder. This pretty much handles everything but I need to add another project for a specific task and would like to add it under /var/www/html/wrettin. At the moment, I don't have anything in the base folder's .htaccess as it is all pretty much handled through the default.conf file of the Apache2 server.
Here is the conf file:
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster#me
DocumentRoot /var/www/html/public
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
<Directory /var/www/html/public/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
What would be the appropriate configuration in order to keep all paths going to the primary application while forcing those that go to the wrettin path to be redirected to the new application?
I'd appreciate any and all help! Thanks!

YII2 The requested URL was not found on this server

Totally new to the framework (YII2) and my first interaction with the framework am working with an already existing web app done was developed by someone else. After successful connection to the database, I try visiting the site through localhost just to get an error The requested URL /dwg/auth/auth/login was not found on this server.
I get this on both windows machine and linux. What could be the issue? Have I missed something on the configuration?
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster#localhost
ServerName www.dwg.com
ServerAlias www.dwg.com
DocumentRoot /home/hemedi/public_html
<Directory /home/hemedi/public_html/>
Options Indexes FollowSymlinks
AllowOverride all
Require all granted
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
its now working. DId move all the project files to the root directory /var/www/html mv dwg/* /var/www/html

Setting document root for Laravel project on Apache virtual host

I inherited a php/Laravel app that was running on an Apache server that I don't have access to. My task is to get it running on another Apache server. I'm pretty good with php but relatively new to Laravel and very new to Apache configuration.
I have figured out how to get the Laravel app running on Apache that is running on an Ubuntu VM (VirtualBox.) I can access the Laravel app in a browser on the Ubuntu VM via http://localhost. I can also access the Laravel app in a browser from the Internet via http://appname.com/public. However, if I just use http://appname.com, then I just get a folder listing of /var/www/appname.
I have tried several modifications to the /etc/apache2/available-sites/appname.conf file but haven't quite got it right yet, apparently. I have also read a number of posts around the nets about making modifications to various other config files including php config files and Apache config files. It seems like these other mods (while they may be workable) shouldn't be necessary.
Here is my current /etc/apache2/available-sites/appname.conf
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName appname.com
ServiceAlias www.appname.com
DocumentRoot /var/www/appname/public
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Any advise is appreciated.
Bob
You need to allow the mod_rewrite in the apache server and allowSymLinks.
Source
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName appname.com
ServiceAlias www.appname.com
DocumentRoot /var/www/appname/public
<Directory "/var/www/appname/public">
Options FollowSymLinks
ReWriteEngine On
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
in the DocumentRoot Directory i would also allow MultiViews
<Directory "/var/www/appname/public">
Options FollowSymLinks MultiViews
ReWriteEngine On
</Directory>
You may need to also do
sudo a2enmod rewrite
to enable module rewrite.
Edit 1:
In my .conf files i got them with the quotes and they are working.
Did you enable the modudle rewrite?
Besides some options i also have the "/" folder with the next config.
<Directory "/">
Options FollowSymLinks
AllowOverride All
ReWriteEngine On
</Directory>
and here i'll write my full code of public directory
<Directory "/var/www/appname/public">
Options FollowSymLinks MultiViews
Order Allow,Deny
Allow from all
ReWriteEngine On
</Directory>
Try it and see if it works, after delete the options that you don't like to use.
Follow the steps and all will be good and easy,
1). Type following command in terminal
cd /etc/apache2/sites-available
2). Make a new config file
sudo cp 000-default.conf appname.dev.conf
3. Open the new config file and paste the following code
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin yourmail#example.com
ServerAlias appname.dev
DocumentRoot /var/www/html/appname/public
<Directory /var/www/html/appname/public>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
<FilesMatch \.php$>
#Change this "proxy:unix:/path/to/fpm.socket"
#if using a Unix socket
#SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
4). CTRL+x, then press y then press enter and run following command in terminal
sudo a2ensite appname.dev.conf
5). Type following command and edit the /etc/hosts file
sudo nano /etc/hosts
127.0.0.1 appname.dev
press CTRL x then press Enter and type following command
sudo service apache2 restart
6). Now your app will execute on appname.dev successfully.

apache virtual host with laravel 5 "Internal error 500"

I want to learn laravel, and recently installed a fresh copy laravel on an amazon ec2 instance through composer composer create-project laravel/laravel laravel-app with apache2, and php installed. I configure the conf.d file to change the document root to laravel-folder/public, when i try to go to the public ip address it show a server 500 error.
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#elastic IP address
ServerName 42.66.33.52
ServerAdmin webmaster#localhost
DocumentRoot laravel-app/public
<Directory laravel-app/public>
DirectoryIndex index.php
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
I've tried chmod 777 to the entire laravel folder and changed the url in app.php to 42.66.33.52, and it still showing the same error. I'm new to laravel, and I hope someone can point me to the right direction. I've host other PHP website from this server without using laravel framework and it is function normally.
You should chmod -R 775 on /storage folder and all files in it.
as #Alexey Mezenin said, you should change the chmod of storage and all files inside by -R, and also don't forget to do the same with the bootstrap/cache folder. Check that on "Directory Permissions" in the official laravel documentation installation :
https://laravel.com/docs/5.3/installation
hope that help ;) Regards.

Categories