1

I am struggling with deploying my RESTful zend application to a remote apache server for couple of days. I know similar questions has been posted many times. But firstly I am completely new to server configuration world and secondly I've read and tried many of the solutions on those posts without any success.

The app is completely working on the localhost. On the remote server, however, the situation is different. After uploading the whole zend skeleton app to the var/www/html directory of the apache server, The public folder that contains all the front-end code can easily be accessed by this url : domain/myZendApp/public/pathToView. Nevertheless,this is firstly, unprofessional, and secondly I cannot access any of the zend RESTful endpoints that I have developed for my app. In the local host, for instance, the url I use for accessing the users list from the user controllers is like this: myZendApp.localhost/user.

As I said I have tried many solutions from this post or many other posts such as creating .htaccess in the root directory or inside the app directory as well as inside the public folder. After many changes, At the moment the .htaccess and index.php files are as below:

var/www/html/myZendApp/public/.htaccess:

RewriteEngine On
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
# The following rewrites all other queries to index.php. The 
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to 
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size 
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]

/var/www/html/whately2.0/.htaccess

SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteRule .* index.php
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/app/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$
RewriteRule ^(.*)$ /public/app/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ /public/index.php [NC,L]

/var/www/html/.htaccess

RewriteEngine On  
RewriteBase /whately2.0/public  
RewriteCond %{REQUEST_FILENAME} -s [OR]  
RewriteCond %{REQUEST_FILENAME} -l [OR]  
RewriteCond %{REQUEST_FILENAME} -d  
RewriteRule ^.*$ - [NC,L]  
RewriteRule ^.*$ whately2.0/public/index.php [NC,L]  

var/www/html/myZendApp/public/index.php:

<?php

chdir(dirname(__DIR__));

// Decline static file requests back to the PHP built-in webserver
if (php_sapi_name() === 'cli-server') {
    $path = realpath(__DIR__ . parse_url($_SERVER['REQUEST_URI'],     PHP_URL_PATH));
if (__FILE__ !== $path && is_file($path)) {
    return false;
}
unset($path);
}
require 'init_autoloader.php';
Zend\Mvc\Application::init(require 'config/application.config.php')->run();

I also created a virtual host pointing to the public folder of the app like below:

<VirtualHost>
   ServerName domain/myZendApp
   DocumentRoot "/var/www/html/myZendApp/public"
   Options Indexes FollowSymLinks
   SetEnv APPLICATION_ENV "development"

    <Directory "/var/www/html/myZendApp/public">
        DirectoryIndex index.php
        AllowOverride All 
        Order deny,allow
        Allow from all   
    </Directory>
</VirtualHost>

This solution did not work too. and when I want to reach (for example) domain/user I receive 404 error: The requested URL /user was not found on this server.

Now I am really frustrated and cannot find any other solution that I have not done. I will be very thankful if some one tells me how can I access the zend endpoints in the remote host. And please forgive me if I am missing some necessary information here and please let me know if you need more details.

Thanks in advance.

Updates: After I edited the httpd.conf like below( AllowOverride All instead of AllowOverride None):

<Directory "/var/www/html/whately2.0/public">
    AllowOverride All
    allow from all
    Options None
    Require all granted
</Directory>

Now when I want to access the domain I get this error(403 forbidden):You don't have permission to access / on this server. (I am sure the permission is right)

If I want access domain/permalink again the same 403 error.

Note that before adding the 'AllowOverride All', I could access the domain that showed me the zend skeleton homepage but when I wanted to access domain/permalink(query from database like domain/user) I get the error 404.

Please help me: whatever I do this problem is not going away. I also added this line: LoadModule rewrite_module modules/mod_rewrite.so to the httpd.conf to enable mode-rewrite (Usually it is recommended to uncomment this line in httpd.conf file but I could not find it, so I added it to the code there)

Again: please someone help...

Thanks

Community
  • 1
  • 1
zahra
  • 199
  • 1
  • 7
  • 19
  • `ServerName` must be set to a hostname (so just 'domain', not 'domain/path'). It's also best not to have doc roots for your vhosts in a subfolder of the default vhost. So fix the server name, move your ZF app to `/var/www/myZendApp`, update the paths in the vhost and then restart Apache. – Tim Fountain Sep 15 '15 at 11:26
  • Thank you @Tim Fountain for your comment. I have tried your solution but I could not see the result. And that is because I was not able to restart Apache. As I don't have full access to the host I was forced to stop and start apache from webmin. After I stopped it when I want to start it again I receive an error: Failed to start apache : Redirecting to /bin/systemctl start httpd.service Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -xn' for details. – zahra Sep 15 '15 at 17:08
  • I'd guess that means there's a syntax error in your Apache configuration. Hopefully there's a log file that'll tell you where that might be. – Tim Fountain Sep 15 '15 at 17:27
  • I did what you said and could restart the server. Now I can access the myZendApp/public folder from the 'domain' in the browser. But I cannot access any of the modules I have in the data base. For example for domain/user I get 404 error again ' The requested URL /user was not found on this server'. – zahra Sep 15 '15 at 22:50
  • Do you think the problem is from the zend part and not form the server configuration? (I can access the zend skeleton home page from 'domain') – zahra Sep 15 '15 at 22:55
  • Nope, if the homepage works but everything else 404s that means your rewrite rules are not being used. Can you double check that your `.htaccess` file is there in the public folder? – Tim Fountain Sep 16 '15 at 09:31
  • I really appreciate your help @Tim Fountain. Actually I have the .htaccess in the public folder. Please see the updates above. – zahra Sep 16 '15 at 16:36
  • @Tim Fountain Considering all the updates (things I have done without a success), could it be that the mode-rewrite is not installed on the Apache server? If yes, how can I install it on the server. Should I contact the server admin or should I do it by myself? – zahra Sep 16 '15 at 16:40
  • It's possible, although I'd expect a different error if that was the case. Since you're getting a 403 check the logs if you can to see what's causing it. – Tim Fountain Sep 16 '15 at 17:10
  • I checked the error log and this is the error I see in logs: 'Options FollowSymLinks and SymLinksIfOwnerMatch are both off, so the RewriteRule directive is also forbidden due to its similar ability to circumvent directory restrictions'. I don't know how can I solve it. Though I am trying to figure it out, please help me if you know the solution. Thanks. – zahra Sep 17 '15 at 03:39
  • I could solve the problem of accessing permalinks by adding this line to the related directive: Options Indexes FollowSymLinks SymLinksIfOwnerMatch – zahra Sep 17 '15 at 05:29
  • But unfortunately though the app was completely working on the localhost, when I want to access for example domain/user(That should give me the users list) I get zend errors. Please the question I posted just now for more details [here](http://stackoverflow.com/questions/32624302/why-i-receive-error-table-doesnt-exist-when-i-run-my-zend-framework-2-app-to) – zahra Sep 17 '15 at 07:08

0 Answers0