Deploy on Shared Web Host - apache

I have a simple question but I just can't find a straightforward answer anywhere.
I'm not very familiar with apache .htaccess and now I should deploy a ZF2 app on a web host that doesn't allow me to change the website root folder.
I have a standard project structure so my index.php file is located inside the /public folder, like this:
www.mydomain.com
root/ <--- I can't set root to /public folder
/config
...
/module
...
/public
.htaccess
index.php
...
/vendor
...
...
This is from the ZF2 skeleton app.
Please I need a workaround, I'm sure this is a pretty common problem that many ZF2 developers already tackled. I think I should add a .htaccess file to /root but I don't know how to write one that would work in this case.
As always any help is much appreciated,
Dan

In order to make this work you have to take everything from the /public folder and put it inside your web root. The file index.php from the skeleton app must be edited to take account of its new position.
First remove the line:
chdir(dirname(__DIR__));
Because its purpose is to set the current directory to the web root which we are already inside.
Second you have to either:
Replace the line require 'init_autoloader.php' with require 'vendor/autoload.php' (if using composer).
OR
Inside your .htaccess set the ZF2_PATH env variable to where zf2 is installed on your server:
#First line of your .htaccess
SetEnv ZF2_PATH path/to/zf2
Now it should be working.
Make it better
With the above setup you'll have to put all your public folders inside the web root. If you, like me, don't like that just continue reading.
You can put your public folders (e.g., /js, /css) inside /public (index.php and .htaccess still need to be in the root) by simply telling zf2 your new base_path. This is what is used by the basePath() view helper inside your view scripts. You simply need to add the following to your config:
'view_manager' => array(
'base_path' => './public',
),
And the basePath() view helper will output the correct urls.
Make it even better
The last problem with this setup is that your app files are accessible from the web. To fix this I put everything I want to hide inside the /private folder. You end up with a project structure similar to this:
/root
/private
/config
/data
/module
/vendor
.htaccess <-- You have to create this one
composer.json
composer.lock <-- Created by composer after install
composer.phar
/public
/css
/js
.htaccess
index.php
Inside the /private folder you have to create a new .htaccess file and put this line inside it:
Deny from all
This makes everything inside the folder not accessible from the web.
With this change you have broken your app. Infact inside /private/config/application.config.php you have to adjust the module_paths config:
'module_listener_options' => array(
'module_paths' => array(
'./module',
'./vendor',
),
),
You have to prepend /private to the paths:
'module_listener_options' => array(
'module_paths' => array(
'./private/module',
'./private/vendor',
),
),
This will make your app run once again. Now you can deploy your ZF2 apps to shared web hosts and retain a clean project structure.

Related

How to include an .htaccess file wit Nuxt?

I have a Nuxt.js app with an .htaccess file.
The problem is that when I execute nuxt generate in the terminal, my .htaccess file disappears. What can I do to include my .htaccess file when I execute nuxt generate?
You could put your .htaccess file into the /static directory, more info on that in the doc.
That way, you will have direct access to it once pushed to production.
Otherwise, you can also use this approach if you need something more customizable.

How to Serving static files with express if it is inside a subdirectory

I am having trouble with serving a html file which is inside a subdirectory inside public folder.
My directory structure looks like:
public/
HTML/
index.html
index.js
I want to serve the index.html for the root route.
This doesn't work:
app.use('/',express.static(path.join(__dirname,'public')));
According to the docs, this should be enough:
app.use(express.static('public'))
It also gives you such example
http://localhost:3000/images/kitten.jpg
http://localhost:3000/css/style.css
http://localhost:3000/js/app.js
http://localhost:3000/images/bg.png
http://localhost:3000/hello.html
It doesn't go much further other than explaining how you can set up multiple public directories. Anyway, it has worked for me in my projects.
Perhaps you need to state directly app.use(express.static('html')) or app.use(express.static('public/html')). Let me know what works for you.

How do I add a folder to run PHP scripts in Nuxt Project folder?

I have a working Nuxt website configured using Nginx web server.
What I want to do is that I want to run some demo PHP script from a folder called demo which is placed inside root folder of the project.
Now if I access it like this: http://mywebsite.com/demo/test.php
It works fine but If I add a folder with more scripts and css files like this
http://mywebsite.com/demo/todo_list/index.php
Here the index.php is loaded with some images, css and js files. But the page on browser only executes the php code. It can't load any resources like js, css and image files.
Basically I want this folder to be independent so I can do my research work from this folder making it completely independent from Nuxt.
How can I do this?
Nevermind, I figured out.
Just had to add one more rule inside nginx config file as below
# Demo Folder
location /demo/ {
try_files $uri $uri/ /index.php$is_args$args;
}

How to change path in the laravel config if It's located in the folder upper then public?

I have laravel 5 and config file in the config folder and My css located in the resources folder which is a same level with public folder where located index.php. Virtual Host Apache config looks to the public folder as a root site directory, but in this situation I cannot declare correct path from /public/index.php to the resources folder.
From one side I can try easy way and just relocate public folder into root of the laravel, but I don't like this way, any ideas?
Use resource_path('path/to/your/css')
https://laravel.com/docs/5.3/helpers#method-resource-path
EDIT
The most logical is to include your stylesheets in your public folder though. If you need to style a page, the style is public anyway. So why not put in the public folder. There's 2 options to do this:
Do it manually by just copying/moving the files
Use an automated tool like Gulp or Laravel's own Elixir, which provides a really easy way to copy your assets.
add this code in public/index.php
$app->bind('path.public', function() {
return __DIR__;
});

Apache directory root and public directory can't access to files

In my /var/www/ i have:
my_project
|-app
|-controllers
|-models
|-views
|-core
|-css
|-js
|-public
|-index.php
I have set up "DirectoryRoot" to:
/var/www/my_project/public/
Now when i open web page in browser my views are loaded but links to files in css and js folders don't work. It looks like I can't go one directory up. What am i doing wrong ?