Tuesday, July 30, 2013

A step by step tutorial on installing and configuring PHP-FPM, Nginx, & MySQL on an Amazon EC2 instance running Amazon Linux AMI. The procedure has been tested on Amazon Linux AMI, but should also apply in general to Fedora/Red Hat/CentOS distributions. A brief introduction to FastCGI, PHP-FPM, and Nginx was posted in the article Understanding PHP, PHP-FPM and Nginx.

1. Install Linux updates, set time zones, followed by GCC and Make


sudo yum -y update
sudo ln -sf /usr/share/zoneinfo/Singapore \
      /etc/localtime
sudo yum install -y gcc make

2. Install Nginx and PHP-FPM


sudo yum install -y nginx php-fpm

3. Install PHP extensions


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

4. Install PHP-APC


sudo yum install -y php-pecl-apc
sudo yum install -y pcre-devel

5. Install MySQL


sudo yum -y install mysql-server mysql

6. Nginx Configuration


sudo nano /etc/nginx/conf.d/default.conf

If you want the location of your web root in the default Amazon Linux AMI directory (/var/www/html):


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;
  }

If you want the location of your web root in the default Nginx directory (/usr/share/nginx/html):


location / {
    root   /usr/share/nginx/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;
  }

7. PHP-FPM Configuration


sudo nano /etc/php-fpm.d/www.conf


[...]
;listen = 127.0.0.1:9000
listen = /var/run/php-fpm/php-fpm.sock
;listen.owner = nobody
listen.owner = nginx
;listen.group = nobody
listen.group = nginx
;listen.mode = 0666
listen.mode = 0664

user = nginx
group = nginx
[...]

8. Auto Start Nginx, PHP-FPM, and MySQL in EC2 Amazon Linux


sudo chkconfig nginx on
sudo chkconfig mysqld on
sudo chkconfig php-fpm on

9. Auto Start Nginx, PHP-FPM, and MySQL in EC2 Amazon Linux


sudo service php-fpm start
sudo service nginx start

if everything is well, you go to the root of your address, it will show you the start page of nginx.

9 comments:

  1. Your information was very clear. Thank you for sharing.
    AWS Online Training

    ReplyDelete
  2. I like your post very much. It is very much useful for my research. I hope you to share more info about this. Keep posting!! AWS Training

    ReplyDelete
  3. Manually hosting a website on AWS is difficult since there is a lot to do. You have to first install the stack and then configure it. There are tools that can make the process easier like PaaS and server provisioning tools.

    ReplyDelete
  4. Hi,Thanks for sharing mind blowing articles...

    More: https://www.kellytechno.com/Hyderabad/Course/amazon-web-services-training

    AWS Training in Hyderabad

    ReplyDelete
  5. I would really like to read some personal experiences like the way, you've explained through the above article. I'm glad for your achievements and would probably like to see much more in the near future. Thanks for share.
    AWS training in chennai | AWS training in anna nagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery

    ReplyDelete
  6. if ur interested in learning AWS course visit our website
    AWS Training in Hyderabad

    ReplyDelete
  7. Great post! As someone interested in cloud computing, Start a AWS journey.

    ReplyDelete