Install NGINX/PHP-FPM on Centos/rhel

Source: Internet
Author: User
Tags apc apc configuration apcu mcrypt php mongodb sqlite 403 forbidden error

Install Nginx, PHP 5.5.15 and php-fpm on Fedora 20/19/18/17, CentOS 7/6.5/5.10, Red Hat (RHEL) 7/6.5/5.101. Change to root user.
Su
2. Install needed repositoriesCentOS 7/6.5/5.10 and Red Hat (RHEL) 7/6.5/5.10 Remi repository
 ## Remi Dependency on CentOS 7 and Red Hat  (RHEL)  7  # #rpm  -uvh http://dl.fedoraproject.org/pub/epel/beta/7/x86_64/epel-release-7-0.2.noarch.rpm  ## CentOS 7 and Red Hat  (RHEL)  7 # #rpm  -uvh http:// rpms.famillecollet.com/enterprise/remi-release-7.rpm  ## remi dependency on  centos 6 and red hat  (RHEL)  6 # #rpm  -uvh http:// Download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm ## centos 6 and  Red Hat  (RHEL)  6 # #rpm  -uvh http://rpms.famillecollet.com/enterprise/ remi-release-6.rpm  ## remi dependency on centos 5 and red  hat  (RHEL)  5 # #rpm  -uvh http://dl.fedoraproject.org/pub/epel/5/i386/ Epel-release-5-4.noarch.rpm ## centos 5 and&nbsP red hat  (RHEL)  5 ## rpm -uvh http://rpms.famillecollet.com/enterprise/ remi-release-5.rpm
CentOS 7/6.5/5.10 and Red Hat (RHEL) 7/6.5/5.10 Nginx repository

Create File/etc/yum.repos.d/nginx.repo and add following content to repo file:
Centos

[Nginx]name=nginx repobaseurl=http://nginx.org/packages/centos/$releasever/$basearch/gpgcheck=0enabled=1
3. Install Nginx, PHP 5.5.15 and PHP-FPMCentOS 7/6.5/5.10 and Red Hat (RHEL) 7/6.5/5.10
Yum--enablerepo=remi,remi-php55 install Nginx php-fpm Php-common
4. Install PHP 5.5.15 Modules
  • Opcache (Php-opcache) , Haven Zend Opcache provides faster php execution through opcode caching and optimization.

  • APCU (PHP-PECL-APC) –APCU userland Caching

  • CLI (PHP-CLI) –command-line interface for PHP

  • PEAR (php-pear) –php Extension and application Repository framework

  • PDO (php-pdo) –a Database access abstraction module for PHP applications

  • MySQL (php-mysqlnd) –a module for PHP applications this use MySQL databases

  • PostgreSQL (php-pgsql) –a PostgreSQL database module for PHP

  • MongoDB (Php-pecl-mongo) –php MongoDB Database driver

  • SQLite (php-sqlite) –extension for the SQLite V2 embeddable SQL Database Engine

  • Memcache (Php-pecl-memcache) –extension to work with the Memcached caching daemon

  • Memcached (php-pecl-memcached) –extension to work with the Memcached caching daemon

  • GD (PHP-GD) –a module for PHP applications for using the GD graphics library

  • XML (Php-xml) –a module for PHP applications which use XML

  • mbstring (php-mbstring) –a module for PHP applications which need multi-byte string handling

  • MCrypt (php-mcrypt) –standard PHP module provides MCrypt library support

Select what NEED:APCU, CLI, PEAR, PDO, MySQL, PostgreSQL, MongoDB, SQLite, Memcache, Memcached, GD, mbstring, MCrypt, Xml

More info on PHP APC from PHP APC Configuration and Usage Tips and Tricks

CentOS 6.5/5.10 and Red Hat (RHEL) 6.5/5.10
Yum--enablerepo=remi,remi-php55 install Php-opcache php-pecl-apcu php-cli php-pear php-pdo php-mysqlnd php-pgsql Php-pecl-mongo php-sqlite php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml
5. Stop httpd (Apache) server, Start Nginx HTTP Server and PHP-FPM (FastCGI Process Manager)Stop httpd (Apache)
/ETC/INIT.D/HTTPD stop
# # OR # #
Service httpd Stop

Start Nginx
/etc/init.d/nginx Start # # Use restart after update## OR # #service nginx Start # # Use restart after update
Start PHP-FPM
/ETC/INIT.D/PHP-FPM Start # # Use restart after update## OR # #service php-fpm Start # # Use restart after update
6. Autostart Nginx and php-fpm on boot, also prevent httpd (Apache) autostarting on bootPrevent httpd (Apache) autostarting on boot
/sbin/chkconfig httpd off
Autostart Nginx on Boot

Centos/red Hat (RHEL) 6/5

/sbin/chkconfig--add nginx/sbin/chkconfig--levels 235 nginx on
Autostart php-fpm on Boot

Centos/red Hat (RHEL)

/sbin/chkconfig--add PHP-FPM
/sbin/chkconfig--levels 235 PHP-FPM on

7. Configure Nginx and PHP-FPMCreate directory layout for your site

I Use the Here testsite.local site, but this could of course is your real site, like www.if-not-true-then-false.c Om.

# # Public_html directory and Logs directory # #mkdir-P/srv/www/testsite.local/public_htmlmkdir/srv/www/testsite.local /logschown-r apache:apache/srv/www/testsite.local

Alternative setup to add logs under /var/log directory.

# # Public_html directory and Logs directory # #
Mkdir-p/srv/www/testsite.local/public_html
Mkdir-p/var/log/nginx/testsite.local
Chown-r apache:apache/srv/www/testsite.local
Chown-r Nginx:nginx/var/log/nginx

Note: I use Apache user and group here, because PHP-FPM runs as Apache-default (Apache choosed to is able to access some dir as HTTPD). If you use the some other user on your PHP-FPM conf then the change this accordingly.

Create and Configure virtual host directories under /etc/nginx
Mkdir/etc/nginx/sites-availablemkdir/etc/nginx/sites-enabled

Add following lines to /etc/nginx/nginx.conf file, after ' include/etc/nginx/conf.d/*.conf ' line ( Inside HTTP block).

# # Load Virtual host conf files. ##
include/etc/nginx/sites-enabled/*;

Create testsite.localVirtual Host File

Add following content to /etc/nginx/sites-available/testsite.local file. This is very basic Virtual host CONFIG.

server {    server_name testsite.local;    access_log / srv/www/testsite.local/logs/access.log;    error_log /srv/www/testsite.local/logs/ error.log;    root /srv/www/testsite.local/public_html;      Location / {        index index.html index.htm  index.php;    }     location ~ \.php$ {         include /etc/nginx/fastcgi_params;         fastcgi_pass  127.0.0.1:9000;         fastcgi_index index.php;        fastcgi_param  script_filename /srv/www/testsite.local/public_html$fastcgi_script_name;         }}
Link your virtual host to /etc/nginx/sites-enabled
cd/etc/nginx/sites-enabled/
Ln-s/etc/nginx/sites-available/testsite.local
/sbin/service nginx Restart

Add your testsite.local "domain" to/etc/hosts file

/etc/hosts file Nginx on same machine
127.0.0.1. Row should look like example following:

127.0.0.1 localhost.localdomain localhost testsite.local

And if you use the another machine where is running your Nginx server, then add it's public IP as following:

10.0.2.19 WordPress

Note: This was just very simple basic configuration, but if you want configure and optimize Nginx and php-fpm more then check fol lowing Guide, Nginx and php-fpm Configuration and optimizing Tips and Tricks

8. Test your Nginx and PHP-FPM setup

Create /srv/www/testsite.local/public_html/index.php file with following content:


Phpinfo ();
?>

Access following address, with your browser. http://testsite.local/

Note:
If you get 403 Forbidden error and then you probably has problem with SELinux and then run simply following command:

Chcon-r-T httpd_sys_content_t/srv/www/testsite.local/public_html # Or Some apps might need httpd_sys_rw_content_t # #ch Con-r-T httpd_sys_rw_content_t/srv/www/testsite.local/public_html
Enable Remote Connection to Nginx Web Server (Open Port Iptables Firewall)1. Centos/red Hat (RHEL) 6/51.1 Edit/etc/sysconfig/iptables File:
Nano-w/etc/sysconfig/iptables
1.2 Add following INPUT rule:
-A input-m state--state new-m tcp-p TCP--dport 80-j ACCEPT
1.3 Restart Iptables Firewall:
/sbin/service iptables restart## OR ##/etc/init.d/iptables Restart


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.