1. 程式人生 > >centos 7.5構建Nginx/PHP-FPM

centos 7.5構建Nginx/PHP-FPM

1. Install Nginx, PHP 7.2.12 and PHP-FPM

1.1 Change to root user.


sudo -i
## OR ##
su -

1.2 Install needed repositories


## Remi Dependency on CentOS 7 and Red Hat (RHEL) 7 ##
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

## CentOS 7 and Red Hat (RHEL) 7 ##
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

1.3 Install Nginx, PHP 7.2.12 and PHP-FPM


yum --enablerepo=remi,remi-php72 install nginx php-fpm php-common

1.4 Install PHP 7.2.12 modules

  • OPcache (php-opcache) – The Zend OPcache provides faster PHP execution through opcode caching and optimization.
  • APCu (php-pecl-apcu) – 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 that use MySQL databases
  • PostgreSQL (php-pgsql) – A PostgreSQL database module for PHP
  • MongoDB (php-pecl-mongodb) – PHP MongoDB database driver
  • Redis (php-pecl-redis) – Extension for communicating with the Redis key-value store
  • 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

yum --enablerepo=remi,remi-php72 install php-opcache php-pecl-apcu php-cli php-pear php-pdo php-mysqlnd php-pgsql php-pecl-mongodb php-pecl-redis php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml

1.5 Start Nginx HTTP server and PHP-FPM (FastCGI Process Manager)

Start Nginx


## Fedora 29/28/27 and CentOS/RHEL 7.5 ##
systemctl start nginx.service

Start PHP-FPM


## Fedora 29/28/27 and CentOS/RHEL 7.5 ##
systemctl start php-fpm.service

1.6 Autostart Nginx and PHP-FPM on boot

Autostart Nginx on boot


## Fedora 29/28/27 and CentOS/RHEL 7.5 ##
systemctl enable nginx.service

Autostart PHP-FPM on boot


## Fedora 29/28/27 and CentOS/RHEL 7.5 ##
systemctl enable php-fpm.service

1.7 Configure Nginx and PHP-FPM

配置nginx支援php
 cp /etc/nginx/nginx.conf  /etc/nginx/nginx.confbak#備份原有配置檔案
vi /etc/nginx/nginx.conf #編輯
user nginx nginx; #修改nginx執行賬號為:nginx組的nginx使用者
:wq  #儲存退出
cp /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.confbak #備份原有配置檔案
vi /etc/nginx/conf.d/default.conf #編輯

index index.php index.html index.htm; #增加index.php

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
 #
 location ~ \.php$ {
 root html;
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include fastcgi_params;
 }
#取消FastCGI server部分location的註釋,並要注意fastcgi_param行的引數,改為$document_root$fastcgi_script_name,或者使用絕對路徑
systemctl restart nginx.service #重啟nginx

把 test.php 上傳/usr/share/nginx/html.

cd /usr/share/nginx/html

vi test.php  #新增以下程式碼 <?php phpinfo(); ?>

:wq! #儲存退出