HOWTO: Setup Nginx/PHP/MySQL/WordPress in Amazon EC2

Nginx is a lightning fast, lightweight web server, a great alternative for apache in terms of speed, scalability. The good news is nginx comes pre-installed in amazon linux. We will see how quickly we can setup wordpress running on top of php, mysql running Nginx server in Amazon linux.

nginxserver

Start Nginx webserver and make sure you open port 80 in your amazon running instance

service nginx start

Now you can check the Nginx test page by typing http://ec2-xx-xx-xx-xxx.compute-1.amazonaws.com

  • Nginx default page on the Amazon Linux AMI. It is located in /usr/share/nginx/html/index.html
  • Nginx configuration file /etc/nginx/nginx.conf
  • Nginx error log /var/log/nginx/error.log

Install PHP for Nginx

sudo yum install php-fpm php-devel php-mysql php-pdo php-pear php-mbstring php-cli php-odbc php-imap php-gd php-xml php-soap

Install MySQL server

sudo yum install mysql-server mysql

You also have create a new user for wordpress or any other CMS. This can be easily done in command line

mysql -u -p
CREATE USER 'test'@'localhost' IDENTIFIED BY 'test';
CREATE DATABASE wp;
GRANT ALL PRIVILEGES ON wp.* TO test@localhost;

Configure Nginx

sudo nano /etc/nginx/nginx.conf

Make the changes, should look like this

location / {
 root /var/www/html;
 index index.php index.html index.htm;
}
location ~ \.php$ {
 fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
 include fastcgi_params;
}

Setup PHP FPM

We now setup php fast cgi for nginx. Uncomment and edit these specific lines look like this

sudo nano /etc/php-fpm.d/www.conf
listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nginx
listen.group = nginx
listen.mode = 0664
user = nginx
group = nginx

Install WordPress

cd /usr/share/nginx/html/
sudo wget http://wordpress.org/latest.tar.gz
tar zxf latest.tar.gz
cd wordpress
cp wp-config-sample.php wp-config.php

Edit wp-config.php and enter the mysql database information

define('DB_NAME', 'wp');
/** MySQL database username */
define('DB_USER', 'test');
/** MySQL database password */
define('DB_PASSWORD', 'test');

Properly configure permissions for the wordpress uploads folder. It should run as www-root or nginx, if not you cannot upload any media files

sudo chown -R nginx:nginx /var/www/html/wordpress/*

All is done now..Fire up!

service php-fpm restart
service mysqld start
service nginx restart

Point your browser to wordpress and rest is easy!

Useful github guide: https://gist.github.com/sumardi/5559803