CentOS 7 上安装 Nginx+PHP7-FPM+MariaDB

本文记录在CentOS 7 下 安装Nginx+PHP7-FPM+MariaDB 的流程。

1.安装Nginx最新版

首先安装最新版的Nginx,新增一个Nginx官方的 yum 源

vi /etc/yum.repos.d/nginx.repo

将以下内容粘贴进去

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1

保存后,查询下是否已配置好yum源(已查询到最新的1.12版本)

[root@izwz9he4m3a5t0qeoy0k1jz ~]# yum list |grep nginx
nginx.x86_64                              1:1.12.2-1.el7               @epel    
nginx-all-modules.noarch                  1:1.12.2-1.el7               @epel    
nginx-filesystem.noarch                   1:1.12.2-1.el7               @epel    
nginx-mod-http-geoip.x86_64               1:1.12.2-1.el7               @epel    
nginx-mod-http-image-filter.x86_64        1:1.12.2-1.el7               @epel    
nginx-mod-http-perl.x86_64                1:1.12.2-1.el7               @epel    
nginx-mod-http-xslt-filter.x86_64         1:1.12.2-1.el7               @epel    
nginx-mod-mail.x86_64                     1:1.12.2-1.el7               @epel    
nginx-mod-stream.x86_64                   1:1.12.2-1.el7               @epel    
collectd-nginx.x86_64                     5.8.0-3.el7                  epel     
munin-nginx.noarch                        2.0.33-1.el7                 epel     
nextcloud-nginx.noarch                    10.0.4-2.el7                 epel     
nginx.x86_64                              1:1.12.2-2.el7               epel     
nginx-all-modules.noarch                  1:1.12.2-2.el7               epel     
nginx-filesystem.noarch                   1:1.12.2-2.el7               epel     
nginx-mod-http-geoip.x86_64               1:1.12.2-2.el7               epel     
nginx-mod-http-image-filter.x86_64        1:1.12.2-2.el7               epel     
nginx-mod-http-perl.x86_64                1:1.12.2-2.el7               epel     
nginx-mod-http-xslt-filter.x86_64         1:1.12.2-2.el7               epel     
nginx-mod-mail.x86_64                     1:1.12.2-2.el7               epel     
nginx-mod-stream.x86_64                   1:1.12.2-2.el7               epel     
nginx1w.x86_64                            1.12.1-1.w7                  webtatic 
nginx1w-module-headers-more.x86_64        1.12.1-1.w7                  webtatic 
nginx1w-module-http-geoip.x86_64          1.12.1-1.w7                  webtatic 
nginx1w-module-http-image-filter.x86_64   1.12.1-1.w7                  webtatic 
nginx1w-module-http-perl.x86_64           1.12.1-1.w7                  webtatic 
nginx1w-module-http-xslt.x86_64           1.12.1-1.w7                  webtatic 
nginx1w-module-mail.x86_64                1.12.1-1.w7                  webtatic 
nginx1w-module-pagespeed.x86_64           1.12.1-1.w7                  webtatic 
nginx1w-module-stream.x86_64              1.12.1-1.w7                  webtatic 
owncloud-nginx.noarch                     9.1.5-1.el7                  epel     
pcp-pmda-nginx.x86_64                     3.11.8-7.el7                 base     
python2-certbot-nginx.noarch              0.23.0-1.el7                 epel

无误后进行安装

yum -y install nginx

安装完成启动Nginx,设为开机启动

systemctl start nginx
systemctl enable nginx

2.安装php-fpm

先添加 PHP7-FPM 的 webtatic 仓库

rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

安装php-fpm以及相关扩展

yum -y install php70w-fpm php70w-cli php70w-gd php70w-mcrypt php70w-mysql php70w-pear php70w-xml php70w-mbstring php70w-pdo php70w-json php70w-pecl-apcu php70w-pecl-apcu-devel

附上常用扩展 可直接使用yum install 安装

[root@izwz9he4m3a5t0qeoy0k1jz ~]# php -m
[PHP Modules]
apcu
bz2
calendar
Core
ctype
curl
date
dom
exif
fileinfo
filter
ftp
gd
gettext
gmp
hash
iconv
igbinary
imagick
json
libxml
mbstring
mcrypt
mysqli
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
readline
redis
Reflection
session
shmop
SimpleXML
sockets
SPL
sqlite3
standard
sysvmsg
sysvsem
sysvshm
tokenizer
wddx
xml
xmlreader
xmlwriter
xsl
zip
zlib

[Zend Modules]

启动PHP,设为开机启动

systemctl start php-fpm
systemctl enable php-fpm

3.配置PHP

编辑php的配置文件

vi /etc/php-fpm.d/www.conf

修改PHP运行的用户,与用户组

user = nginx
group = nginx

修改PHP监听端口

listen = 127.0.0.1:9000

取消以下行的注释,配置运行环境

env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

创建session目录,并设置权限

mkdir -p /var/lib/php/session
chown nginx:nginx -R /var/lib/php/session/

4.安装MariaDB

安装MariaDB服务端与客户端

yum -y install mariadb mariadb-server

启动MariaDB,设为开机启动

systemctl start mariadb
systemctl enable mariadb

配置MariaDB,设置root密码(一路Y就好)

mysql_secure_installation

5.开启http防火墙

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

至此LNMP环境配置完成~

下面附上站点的Nginx配置文件:

[root@izwz9he4m3a5t0qeoy0k1jz ~]# cat /etc/nginx/conf.d/default.conf
server {
  listen        80;
  server_name   www.test.com;
  root          /data/wwwroot/www.test.com/public_html;
  index         index.php index.html;

  location / {
    try_files $uri $uri/ /index.php?$query_string;
  }

  location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi.conf;
  }
}

 

Leave A Comment

Please be polite. We appreciate that. Your email address will not be published and required fields are marked