2014-02-20

I wanted to run a rails application and wordpress in a same amazon ec2 server.

Rails app on example.com

wordpress blog on blog.example.com

Added a CNAME blog.example.com to example.com in the DNS settings.

Enabled port 22,80,443,3000 on amazon ec2

To enable port 80

Login to http://aws.amazon.com/console/ and goto EC-2

Go to the Security Group settings in the left hand navigation

Find the Security Group that your instance is apart of

Click on Inbound Rules

Use the drop down and add HTTP (port 80)

Click Apply and enjoy

Rails application and WordPress in LAMP are already running in the server.

Here is the ngnix configuration:

File: /etc/nginx/sites-enabled/example

upstream unicorn {

server unix:/tmp/unicorn.nabthat.sock fail_timeout=0;

}

server {

listen 80;

server_name example.com;

root /home/ubuntu/apps/nabthat/current/public;

return 301 https://$host$request_uri;

if ($http_user_agent ~ “Purebot|Lipperhey|MaMa CaSpEr|libwww-perl|Mail.Ru|gold crawler” ) {

return 403;

}

location ^~ /assets/ {

gzip_static on;

expires max;

add_header Cache-Control public;

}

try_files $uri/index.html $uri @unicorn;

location @unicorn {

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header Host $http_host;

proxy_redirect off;

proxy_pass http://unicorn;

}

error_page 500 502 503 504 /500.html;

client_max_body_size 4G;

keepalive_timeout 10;

}

server {

listen 443 ssl default_server;

ssl on;

server_name example.com;

root /home/ubuntu/apps/nabthat/current/public;

ssl_certificate     http://www.example.com.chained.crt;

ssl_certificate_key http://www.example.com.key;

ssl_protocols       SSLv3 TLSv1 TLSv1.1 TLSv1.2;

ssl_ciphers         HIGH:!aNULL:!MD5;

ssl_prefer_server_ciphers on;

location ^~ /assets/ {

gzip_static on;

expires max;

add_header Cache-Control public;

}

try_files $uri/index.html $uri @unicorn;

location @unicorn {

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header Host $http_host;

proxy_redirect off;

proxy_pass http://unicorn;

}

error_log    /var/log/nginx/example.com.error.log debug;

error_page 500 502 503 504 /500.html;

client_max_body_size 4G;

keepalive_timeout 10;

}

ssl_session_cache  builtin:1000  shared:SSL:10m;

—————

The above configuration makes nginx to listen on port 80 and 443

Now, let us add the configuration for wordpress blog served by LAMP on port 3000

File : /etc/nginx/conf.d/blog

server {

listen 80;

server_name  blog.example.com;

location / {

proxy_pass http://127.0.0.1:3000;

}

proxy_set_header Host $host;

}

————

To run apache on port 3000, change the file /etc/apache2/ports.conf

File: /etc/apache2/ports.conf

# If you just change the port or add more ports here, you will likely also

# have to change the VirtualHost statement in

# /etc/apache2/sites-enabled/000-default

Listen 3000

————–

File : /etc/apache2/sites-enabled/000-default.conf

<VirtualHost *:3000>

ServerAdmin webmaster@localhost

DocumentRoot /var/www

ErrorLog ${APACHE_LOG_DIR}/error.log

CustomLog ${APACHE_LOG_DIR}/access.log combined

<Directory /var/www>

#Options -Indexes FollowSymLinks MultiViews

AllowOverride all

Order allow,deny

allow from all

</Directory>

</VirtualHost>

——————–

Now, the rails app is running fine on http://www.example.com

but, if we access blog.example.com it is not redirecting to wordpress blog.

it redirects to same rails application.

To debug this, I stopped the ngnix server.

sudo /etc/init.d/nginx stop

Now, only apache is running.

let us access it using console browser links, inside the server itself.

links http://localhost:3000

got the the following error.

can not connect.

To test the internals, tried with wget

wget http://localhost:3000

–2014-02-20 10:35:03–  http://localhost:3000/

Resolving localhost (localhost)… 127.0.0.1

Connecting to localhost (localhost)|127.0.0.1|:3000… connected.

HTTP request sent, awaiting response… 301 Moved Permanently

Location: http://localhost/ [following]

–2014-02-20 10:35:03–  http://localhost/

Connecting to localhost (localhost)|127.0.0.1|:80… failed: Connection refused.

Resolving localhost (localhost)… 127.0.0.1

Connecting to localhost (localhost)|127.0.0.1|:80… failed: Connection refused.

Got this weired error.

I dont know how the port 3000 is automaticaly redireced to port 80.

After a deep research, found the following.

LAMP is running wordpress.

In its setting->general, the WordPress Address (URL) and Site Address (URL) are defined as http://blog.example.com

When the browser contacts port 3000, it reaches http://blog.example.com only, which means port 80.

Hence, the 301 redirect happens.

To solve this, we have to make WordPress Address (URL) and Site Address (URL) as http://blog.example.com:3000

To change the Site Address of an existing wordpress blog,

add the following entries in wp-settings.php

define(‘WP_HOME’,'http://blog.example.com:3000′);

define(‘WP_SITEURL’,'http://blog.example.com:3000′);

That’s all.

Now, if you access http://blog.example.com:3000

the wordpress blog works fine.

Thus, I managed to run rails app (example.com) and wordpress blog(blog.example.com:3000) in a same amazon ec2 server.

Show more