Report this

What is the reason for this report?

Deploying Rails with Capistrano, Nginx, and Passenger Now Working

Posted on December 13, 2018

I have Capistrano working, everything deploys properly to the server when executing cap production deploy but I get a 403 forbidden error when navigating to the site.

Nginx and passenger are installed on the server. Here are the configurations:

nginx.conf:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
	worker_connections 768;
	# multi_accept on;
}

http {

	##
	# Basic Settings
	##

	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;
	# server_tokens off;

	# server_names_hash_bucket_size 64;
	# server_name_in_redirect off;

	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	##
	# SSL Settings
	##

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
	ssl_prefer_server_ciphers on;

	##
	# Logging Settings
	##

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;

	##
	# Gzip Settings
	##

	gzip on;
	gzip_disable "msie6";

	# gzip_vary on;
	# gzip_proxied any;
	# gzip_comp_level 6;
	# gzip_buffers 16 8k;
	# gzip_http_version 1.1;
	# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

	##
	# Phusion Passenger config
	##
	# Uncomment it if you installed passenger or passenger-enterprise
	##
	
	include /etc/nginx/passenger.conf;

	##
	# Virtual Host Configs
	##

	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;
}


#mail {
#	# See sample authentication script at:
#	# http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#	# auth_http localhost/auth.php;
#	# pop3_capabilities "TOP" "USER";
#	# imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#	server {
#		listen     localhost:110;
#		protocol   pop3;
#		proxy      on;
#	}
# 
#	server {
#		listen     localhost:143;
#		protocol   imap;
#		proxy      on;
#	}
#}

passenger.conf:

passenger_root /home/saycod/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0/gems/passenger-6.0.0;
passenger_ruby /home/saycod/.rbenv/versions/2.5.3/bin/ruby;

I am a rookie when it comes to server setup.

Any help is much appreciated, thank you!



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

The 403 Forbidden error often means that the server understands the request but it refuses to authorize it. This typically occurs when the permissions of your files/directories are not set correctly, or your Nginx configuration is not set to serve your application correctly.

Here are a few steps you can take to troubleshoot this issue:

  1. Check the directory and file permissions: Make sure that the www-data user (which Nginx uses) has access to the directories where your application is stored. You can change the ownership of the directory with chown. For instance:

    sudo chown -R www-data:www-data /path/to/your/app
    
  2. Check your Nginx server block configuration: The server block (similar to Apache’s VirtualHosts) allows you to specify configuration for your specific site, including the document root. Make sure that this is correctly pointing to your application’s public directory. It’s often located in /etc/nginx/sites-available/default or a separate file in /etc/nginx/sites-available/. For a Rails application, it might look like this:

    server {
        listen 80;
        server_name yourdomain.com;
    
        root /path/to/your/app/public;
        passenger_enabled on;
    
        location / {
            try_files $uri/index.html $uri.html $uri @app;
        }
    
        location @app {
            proxy_pass http://app_server;
        }
    }
    

    Replace yourdomain.com with your domain and /path/to/your/app with the path to your application.

  3. Check Nginx error logs: The Nginx error logs can provide more information about the issue. They are typically located at /var/log/nginx/error.log. You can view the most recent entries with:

    sudo tail -n 20 /var/log/nginx/error.log
    
  4. Restart Nginx: After making any changes, be sure to restart Nginx to apply them:

    sudo service nginx restart
    
  5. Check SELinux: If you are using a distribution with SELinux enabled, it might be denying Nginx access to certain resources. You can check the SELinux audit log at /var/log/audit/audit.log.

If you’re still experiencing issues, you might want to provide more information about your Nginx server block configuration, the directory structure of your application, and any relevant entries from the Nginx error log.

Also, I could suggest the following tutorial on how to troubleshoot common Nginx errors:

https://www.digitalocean.com/community/tutorials/how-to-troubleshoot-common-nginx-errors

Best,

Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.