THE INQUISITIVISTS
  • Home
  • About
  • Blog
  • Whitepapers
  • Pii Software
  • Contact
  • Videos
  • Link Page

R & D

Running Nginx and Apache Side by Side on a Single EC2 Linux Instance

8/19/2015

0 Comments

 
Given that one of the focuses of this website is technology, and I've just spent the morning on a technological struggle in order to bring said website into being, I thought I'd spend a little time documenting that struggle. Also, in my pursuit of knowledge about the world, I've found articles like this to be incredibly helpful. We live in a world in which, if you have a problem, it is highly likely that both
  1. Someone else has had that problem, and
  2. They solved it and documented the solution
So here is the problem and its solution, documented for posterity. I hope you have arrived here via The Google in search of knowledge and that this article helps you. And, by the way, if this is a method that you use in general to solve problems, congratulations on your highly evolved state of being, and you might also be interested in the rest of the stuff we do around here. ANYWAY...
I wanted to run an Nginx site and an Apache site side by side on the same AWS (that's Amazon Web Services) EC2 Linux micro instance. My wife is a novelist, and I run her website. A t1.micro with a little extra swap space configured can easily handle all of the traffic we garner, and I didn't want to pay the extra $18/month for a second server. www.inquisitivists.com runs on Rails, her website is Perl CGI (and now the more clever amongst you know how old I am).
  • Deploy the Rails website via Elastic Beanstalk: I launched a version of the 64-bit Amazon Linux AMI with the relevant software already installed. You don't have to do this, but it got me up and running quickly. I'm assuming if you're reading this, you know how to deploy a Rails app, so how you do it isn't really that big a concern. At the end of step one, you should have a rails app running on Nginx on an EC2 instance. You should be able to ssh into that instance, and you should be able to hit the website via your browser, either via the Beanstalk URL, or using the actual address of the EC2 instance. Also, either provision an Elastic IP, or know the IP address of your instance. You'll need that to set up your DNS entries.
  • Get Nginx to let go of port 80: If you aren't deploying via Beanstalk, this is easy: ssh into your instance, and sudo edit /etc/nginx/nginx.conf (this assumes the Amazon Linux AMI. If your nginx config file isn't there, you can run sudo nginx -t to get it to tell you where it is). Find the line that says 
    listen 80; 
    and change it to 
    listen 1080; 
    Reload the config by typing: 
    sudo nginx -s reload 
    If you are deploying via Beanstalk, (as I did) this is somewhat more complicated:. First, in the nginx.conf file above, comment out the entire "server" directive. You won't need it. Second, Beanstalk is a super handy deployment tool. But it wants to do things the way it wants to do them, and one of the things it wants to do is serve content on Port 80. Since you want it to serve on a different port, you have to undo that step of the deployment each time. Here's how I did that:
  • I deployed that content via the Beanstalk CLI
  • This is a slightly different method than any Beanstalk documentation will tell you to use to modify your deployment; again, this is because you're doing something somewhat non-standard.
  • Set up your Apache site: The way to set up an and serve an Apache site is documented, like, everywhere in the world so go ahead and find those instructions and do that, or whatever. One important bit: make sure your Apache has mod_proxy installed. At the end of it, you should be able to start Apache, and since Ngnix is no longer listening on port 80, it should start right up. You should now be able to go to your site in a browser and hit the Apache-served site.
  • Now a Miracle Occurs™ (If you're skimming this article looking for the solution to your problem, STOP HERE: THIS IS IT): We're going to use Apache as both a proxy and a web server. The way we're going to do that is this. sudo edit /etc/httpd/conf/httpd.conf. You will need to do and/or check several things:
    • Make sure you are loading all of the mod_proxy modules. You should have a bunch of lines that look like this: 
      LoadModule proxy_module modules/mod_proxy.so
    • Add a listener on port 2080. This will be your actual Apache website. Port 80 is going to become your proxy. 
      Listen 80 Listen 2080
      (That "Listen 80" line should have already been there)
    • Add virtual servers for each of your sites via port 80. You can copy and paste the code below, mutatis mutandis:
NameVirtualHost *:80
<VirtualHost *:80>
    ServerName [the domain of your Apache Site].com
    ServerAlias www.[the domain of your Apache Site].com

    ProxyRequests Off
    <Proxy *>
        Order deny,allow
        Deny from all
        Allow from 192.168.0
    </Proxy>

    ProxyPass / http://localhost:2080/
    <Location />
        Order allow,deny
        Allow from all
    </Location>
    ErrorLog /var/log/[yourapachesite]-error.log
    CustomLog /var/log/[yourapachesite]-access.log common
</VirtualHost>
<VirtualHost *:80>
    ServerName [Your Nginx Site].com
    ServerAlias www.[Your Nginx Site].com

    ProxyRequests Off
    <Proxy *>
        Order deny,allow
        Deny from all
        Allow from 192.168.0
    </Proxy>

    ProxyPass / http://localhost:1080/
    <Location />
        Order allow,deny
        Allow from all
    </Location>
</VirtualHost>

What you've done here is told Apache that port 80 is a proxy server; it proxies requests for your Nginx domain to port 1080 of the localhost (your EC2 instance), where the Nginx is waiting to serve that site. It proxies requests to your Apache domain to port 2080, where the same Apache is waiting to serve that site. Note that the server in the ProxyPass line is literally "localhost." You're just telling Apache to proxy the local server to get content to serve back to the user. You may or may not be tempted to fill in your actual domain with the port number attached. Like, don't do this: first off it won't work because you haven't opened those ports via EC2 Security groups, and if you do do that, Apache will get really freaking confused.
  • Point both Domains to your EC2 IP Address: Do that. If you're reading this article, you probably know how to do that. If you don't know how to do that, find the instructions from your specific domain registrar about how to that. Note only that whatever those domains are have to match exactly with the domain names configured as virtual servers in the step above, and if you are going to configure any other subdomains, those need to be represented as server aliases so that Apache knows where to send them when they come in.
  • Profit: Once the DNS records propagate, you should be up and running.
Did you this help you? Let me know if so, or tell me the error you ran into in the comments and I will attempt to assist you further.
0 Comments

    Archives

    September 2015
    August 2015

    Categories

    All

    RSS Feed

© COPYRIGHT 2015. ALL RIGHTS RESERVED.
  • Home
  • About
  • Blog
  • Whitepapers
  • Pii Software
  • Contact
  • Videos
  • Link Page