Thursday, August 29, 2013

installing apache, tomcat, and mysql on ec2

I'm a bit late jumping on the ec2 boat, but finally got around to checking out what ec2 is like and decided I'd try my hand at JSP while I'm at it. So with that in mind, I decided to go with a linux distro for my ec2 instance. For the most part, I followed these instructions to set up tomcat and mysql: http://coenraets.org/blog/2011/11/set-up-an-amazon-ec2-instance-with-tomcat-and-mysql-5-minutes-tutorial/ With that, I was up and running, I used MySQL Workbench (connecting over SSH) to setup a toy database that I could muck around with. Then I started hacking away on Eclipse EE, trying to do some simple JSP/Servlet stuff.
Inevitably, there were some additional tweaks I ended up needing to do after the 5-min tutorial setup, so I'll outline what I did here:
  1. Open up MySQL port. To ensure that my code could access the database (and not just MySQL Workbench), I had to open up the mysql port (3306). To do this, go to the EC2 Dashboard; under Network and Security, click on Security Groups. Select the appropriate security group for your instance and, under 'Inbound', create a new rule for the mysql port. After click 'Add Rule', it should appear in the list of TCP Ports.
  2. Automatically startup services. To automatically have tomcat and mysqld start up whenever the ec2 instance restarts, run these commands:
    sudo chkconfig --level 35 tomcat6 on
    sudo chkconfig --level 35 mysqld on
    Note: the different levels correspond to runlevels. To check that we have these services running with the right flags, we can run: chkconfig --list
  3. Secure MySQL. To make MySql slightly more secure, add a password for root (by default, there's no password), remove the test database, and remove anonymous access to the database.
    # create a new password for root
    sudo mysqladmin -u root password 'new-password' 
    # login with the new password
    sudo mysql -u root -p                           
    # remove test database
    mysql> DROP DATABASE test;
    # remove anonymous access to database
    mysql> DELETE FROM mysql.user WHERE user='' ;    
    mysql> FLUSH PRIVILEGES;
    
  4. Get phpMyAdmin working. Sometimes it's just easier to do DB stuff through a browser instead of a dedicated app like MySQL Workbench. To get phpMyAdmin up and running, I need to install Apache and php first. Having both Apache and Tomcat installed isn't problematic, since I have Apache handle the 80 port and Tomcat handle the 8080 port (which is the default for Tomcat anyways).

    To install Apache:
    sudo yum install php
    sudo yum install httpd
    
    To have the Apache service automatically run on startup:
    sudo chkconfig --level 35 httpd on
    Then I just went to https://gist.github.com/aronwoost/1105007 and followed the instructions in these sections: 'Setup phpMyAdmin', 'Make mod_write work', and 'Start apache'.

    First, to allow access from external IP's:
    sudo chmod 600 /etc/httpd/conf.d/phpmyadmin.conf
    sudo nano /etc/httpd/conf.d/phpmyadmin.conf
    #  Web application to manage MySQL
    #  #
    #  Order Deny,Allow
    #  Deny from all
      Allow from 127.0.0.1
    #
    Alias /phpmyadmin /usr/share/phpmyadmin
    Alias /phpMyAdmin /usr/share/phpmyadmin
    Alias /mysqladmin /usr/share/phpmyadmin
    
    Then, we add the blowfish_secret so that cookie authentication will work
    sudo chmod 600 /usr/share/phpmyadmin/config.inc.php
    sudo nano /usr/share/phpmyadmin/config.inc.php
    ...
    $cfg['blowfish_secret'] = 'put-a-magic-string-here'; 
    /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */
    ...
    sudo chmod 644 /usr/share/phpmyadmin/config.inc.php
    
    Now we modify the httpd configuration to allow mod_rewrite to work:
    cd /etc/httpd/conf
    sudo nano httpd.conf
    
    A few lines below that, look for AllowOverride None and replace with AllowOverride All. And now start up Apache:
    sudo service httpd restart
    Now phpmyadmin is up and running at: 
    http://Your_EC2_IP_Address/phpmyadmin
    or
    http://ec2-*.*.compute.amazonaws.com/phpmyadmin

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.