Moving Rails app to production with nginx - ruby-on-rails-3

I'm on a VPS. I created a new rails app with rails new rails_app -d mysql.
I'm running nginx with passenger. I'm running Rails 3.2.12 and Ruby 1.9.3. In my nginx.conf file I added the following to the server directive:
listen 80;
server_name www.mydomain.com;
passenger_enabled on;
root /home/mike/www/rails_app/public;
rails_env production;
When I point to www.mydomain.com I see Welcome aboard You’re riding Ruby on Rails!. When I click on About your application’s environment I get this error:
The page you were looking for doesn't exist.
When I check my production.log I see this error and don't know what to do with it:
ActionController::RoutingError (No route matches [GET] "/rails/info/properties")
I've been up all night and have read all SO issues similar to this but still I cannot resolve my issue. If I run this in development everything works fine.
EDIT
I found this explanation for a Rails 2.3.4 problem: The link fires off an AJAX request to rails/info/properties. The properties action is defined in the Rails::InfoController which lives in /rails/railties/builtin/rails_info/rails/info_controller.rb.
The route doesn't need to be explicitly defined because it conforms to the Rails default route of :controller/:action/:id (although there is no ID in this case and the controller lives within a Rails namespace.)
But I don't see info_controller.rb anywhere.
Ok I found this: config.consider_all_requests_local is a flag. If true then any error will cause detailed debugging information to be dumped in the HTTP response, and the Rails::Info controller will show the application runtime context in /rails/info/properties. True by default in development and test environments, and false in production mode. For finer-grained control, set this to false and implement local_request? in controllers to specify which requests should provide debugging information on errors.
but setting this to false does nothing.
EDIT 2
Ok, I'm an idiot. Found this somewhere: You're clicking the "About your application’s environment" link on the Rails default index.html page. This link only works in development environment, not in production.
And this entire night I thought my Rails app wasn't working. So I guess I'll give up and go to sleep.

You forgot to add passenger_base_uri:
server {
listen 80;
server_name domain.com;
charset utf-8;
root /www/domain.com/public_html;
passenger_enabled on;
passenger_base_uri /blog;
rails_spawn_method smart;
rails_env production;
}
Also check that passenger and rails work in the same environment (production or development).

For those who might encounter this problem in the future, first check your production logs in your server.
ssh into your server, ssh username#serverIP
Check the last 20 or so error messages tail -20 /home/username/appname/current/log/production.log
If it's a bug in your code (mine was returning an empty array due to an empty db), then fix that bug and run cap production deploy once again.
Repeat to check for more errors.

Same problem is also faced by me, but I found later wards due to permission, our application is not able to access the specific folder.
Try this command :
chmod -R 777 {name of your project folder}/

Related

apache2: subdirectory won't work if I enable app at /

I built a personal server at home which is hosted at, let's say, example.org and I installed GitLab as a subdirectory so I can access to it at example.org/gitlab. As GitLab is the only one application I have so far, everything works smoothly.
Now I want to have a blog at root, so I enabled Apache default site to do some tests. I can see apache2 default page at example.org (which is where I want my blog) but GitLab won't work anymore at example.org/gitlab. All I have is a "page not found". GitLab will only work if I disable the default page at /.
I tried using Alias, I also read something about redirections but I haven't managed to make example.org and example.org/gitlab work together so far.
Any advice about where to look, please?
If you generate two virtualhosts both must have different ServerName, otherwise first match receives all requests and second virtualhost will be ignored.

Laravel Ubuntu 16.04 returning status code 500

I just deployed a Laravel project I developed at localhost (using XAMPP) to a server.
I uploaded all files, created a new .env file (also got an app-key), ran composer install, created the db, ran artisan migrate.
Also, in
/etc/apache2/sites-enabled/000-default.conf I set the DocumentRoot to the public directory of laravel. Also I tried to edit the apache2.conf to include the directory, with Allowoverride All.
But entering the ip of my server, I get redirect to the login page (of course, I'm using the Auth of Laravel).
But I'm getting the error:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
I tried to follow those steps, but it's all the same. Except the one point, that in my apache2.conf I don't wrote the DocumentRoot anymore, as I already set it in the 000-default.conf. I also tried to set it a second time in the apache2.conf, but this changed nothing.
So what couuld the problem be?
I'm using Ubuntu 16.04.
PS: Something seems to happen inside of laravel. When I go an existing route, I always get this error code 500. When I go to a route, that doesn't exist, I get an error (NotFoundHttpException), so the Routing itself kinda seems to work, but where does this error come from? It are exactly the local files I'm using with XAMPP, and locally it works fine... Any ideas?
EDIT: After editing my logs directory to have permissions like 777, I'm getting 2 errors as well.
Both point to storage/framework/views/ and storage/framework/sessions, saying "Permission denied". Do I just have to run chmod for those directories as well?
Check the error in storage/logs/laravel.log if there is no log there then make sure your directory permissions for storage allows it to be writeable 775 is what I normally use.
Okay, I got it, basically it was the permissions.
I now changed the owner of the storage folder to www-data. Now it's working (for anyone that might experience this problem, too).
if you use vps ubuntu, you have to set write/read permission with root folder (and all file, folder of it) by using this command on putty:
chown -R www-data:www-data /var/www/html
i tried this command, and my website doest not show 500 error

Set up RefineryCMS with Passenger

I'm trying to get a RefineryCMS install running on my CentOS VPS (which has DirectAdmin installed, which I don't think makes any difference for my problem) but somehow I cannot get it to host.
In my httpd.conf I've added the following:
RailsEnv development
PassengerHighPerformance on
|?DOCROOT=/home/user/domains/domain.name/public_html/rails/refinery_test_install/|
The paths are all correct and my normal Rails 3 applications work perfectly when I put these lines in my httpd.conf yet now it will just load the index.html file I've put in the directory pure for testing.
Any ideas on how I could get this up and running?
I was totally missing I didn't forwarded passenger to Rails' public folder. The configuration works with the following docroot:
|?DOCROOT=/home/user/domains/domain.name/public_html/rails/refinery_test_install/public/|

Nginx multiple locations with rails static assets

I am new to setting up my own server with nginx so forgive any ignorance. I may have just been using the wrong search terms to find the answers to my questions.
Anyway, I am using Rails 3, Nginx, and Unicorn at the moment on a VPS on rackspace. In my rails app I have about 500mb of files in public/ and I would like to use Nginx to serve these. Typically this is just:
server {
listen 80 default deferred;
# server_name example.com;
root /home/<my_user>/apps/<my_app>/current/public;
...
}
I can make this work if I add the 500mb in public to the git repo and then deploy with capistrano, but I don't want all of those files in my git repo. It makes no sense to store them there, but if I remove them then I have to manually go upload them to my public folder on the server every time I deploy.
Is there a way to make Nginx point to a second folder of assets for it to server? I tried the following:
location /static {
gzip on;
alias /home/deployer/static/;
}
I haven't had any luck getting this to work (trying to access the files via url.com/static/...) Anyone know what I am doing wrong?
Side note: all of the shown code is in my config/nginx.conf file and it SHOULD be overriding the settings via this line in my deploy.rb:
sudo "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{application}"
location /static/ {
root /home/deployer;
}
http://nginx.org/r/alias
http://nginx.org/r/root

Internal Error 500 Apache, but nothing in the logs?

I'm getting 500 Internal Server errors when I try to make an HTTP POST to a specific address in my app. I've looked into the server logs in the custom log directory specified in the virtual hosts file, but the error doesn't show up there so debugging this has been a pain in the ass.
How do I cause Apache to log Internal 500 errors into the error log?
This is an Ancient answer from 2013, back when PHP was new and security wasn't an issue:
Here in the future it's a security risk to dump errors to screen like this. You better not be doing this in any production setting.
Why are the 500 Internal Server Errors not being logged into your apache error logs?
The errors that cause your 500 Internal Server Error are coming from a PHP module. By default, PHP does NOT log these errors. Reason being you want web requests go as fast as physically possible and it's a security hazard to log errors to screen where attackers can observe them.
These instructions to enable Internal Server Error Logging are for Ubuntu 12.10 with PHP 5.3.10 and Apache/2.2.22.
Make sure PHP logging is turned on:
Locate your php.ini file:
el#apollo:~$ locate php.ini
/etc/php5/apache2/php.ini
Edit that file as root:
sudo vi /etc/php5/apache2/php.ini
Find this line in php.ini:
display_errors = Off
Change the above line to this:
display_errors = On
Lower down in the file you'll see this:
;display_startup_errors
; Default Value: Off
; Development Value: On
; Production Value: Off
;error_reporting
; Default Value: E_ALL & ~E_NOTICE
; Development Value: E_ALL | E_STRICT
; Production Value: E_ALL & ~E_DEPRECATED
The semicolons are comments, that means the lines don't take effect. Change those lines so they look like this:
display_startup_errors = On
; Default Value: Off
; Development Value: On
; Production Value: Off
error_reporting = E_ALL
; Default Value: E_ALL & ~E_NOTICE
; Development Value: E_ALL | E_STRICT
; Production Value: E_ALL & ~E_DEPRECATED
What this communicates to PHP is that we want to log all these errors. Warning, there will be a large performance hit, so you don't want this enabled on production because logging takes work and work takes time, time costs money.
Restarting PHP and Apache should apply the change.
Do what you did to cause the 500 Internal Server error again, and check the log:
tail -f /var/log/apache2/error.log
You should see the 500 error at the end, something like this:
[Wed Dec 11 01:00:40 2013] [error] [client 192.168.11.11] PHP Fatal error:
Call to undefined function Foobar\\byob\\penguin\\alert() in /yourproject/
your_src/symfony/Controller/MessedUpController.php on line 249
I just ran into this and it was due to a mod_authnz_ldap misconfiguration in my .htaccess file. Absolutely nothing was being logged, but I kept getting a 500 error.
If you run into this particular issue, you can change the log level of mod_authnz_ldap like so:
LogLevel warn authnz_ldap_module:debug
That will use a log level of debug for mod_authnz_ldap but warn for everything else (https://httpd.apache.org/docs/2.4/en/mod/core.html#loglevel).
Check your php error log which might be a separate file from your apache error log.
Find it by going to phpinfo() and check for error_log attribute.
If it is not set. Set it: https://stackoverflow.com/a/12835262/445131
Maybe your post_max_size is too small for what you're trying to post, or one of the other max memory settings is too low.
If your Internal Server Error information doesn't show up in log files, you probably need to restart the Apache service.
I've found that Apache 2.4 (at least on Windows platform) tends to stubbornly refuse to flush log files—instead, logged data remains in memory for quite a while. It's a good idea from the performance point of view but it can be confusing when developing.
Please Note: The original poster was not specifically asking about PHP. All the php centric answers make large assumptions not relevant to the actual question.
The default error log as opposed to the scripts error logs usually has the (more) specific error. often it will be permissions denied or even an interpreter that can't be found.
This means the fault almost always lies with your script. e.g you uploaded a perl script but didnt give it execute permissions? or perhaps it was corrupted in a linux environment if you write the script in windows and then upload it to the server without the line endings being converted you will get this error.
in perl if you forget
print "content-type: text/html\r\n\r\n";
you will get this error
There are many reasons for it. so please first check your error log and then provide some more information.
The default error log is often in /var/log/httpd/error_log or /var/log/apache2/error.log.
The reason you look at the default error logs (as indicated above) is because errors don't always get posted into the custom error log as defined in the virtual host.
Assumes linux and not necessarily perl
The answers by #eric-leschinski is correct.
But there is another case if your Server API is FPM/FastCGI (Default on Centos 8 or you can check use phpinfo() function)
In this case:
Run phpinfo() in a php file;
Looking for Loaded Configuration File param to see where is config file for your PHP.
Edit config file like #eric-leschinski 's answer.
Check Server API param.
If your server only use apache handle API -> restart apache.
If your server use php-fpm you must restart php-fpm service
systemctl restart php-fpm
Check the log file in php-fpm log folder. eg /var/log/php-fpm/www-error.log
Please check if you are disable error reporting somewhere in your code.
There was a place in my code where I have disabled it, so I added the debug code after it:
require_once("inc/req.php"); <-- Error reporting is disabled here
// overwrite it
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
Add HttpProtocolOptions Unsafe to your apache config file and restart the apache server. It shows the error details.
In my case it was the ErrorLog directive in httpd.conf. Just accidently noticed it already after I gave up. Decided to share the discovery )
Now I know where to find the 500-errors.
Check that the version of php you're running matches your codebase. For example, your local environment may be running php 5.4 (and things run fine) and maybe you're testing your code on a new machine that has php 5.3 installed. If you are using 5.4 syntax such as [] for array() then you'll get the situation you described above.
Try accessing a static file. If this is not working either then
go to all directories from the root "/" or "c:\" to the directory of your file and check if they contain ".htaccess" files.
I once left a file in "c:\" and it had the most strange results.