在CentOS7上配置LNMP环境:PHP篇

上篇介绍了Nginx的安装配置及Let's Encrypt证书的自动签发与自动续期。这里主要介绍PHP的安装及在Nginx中的转发配置。

安装PHP

1、安装依赖

编译安装PHP之前需要安装PHP依赖包:

1
[root@opstrip opt]# yum -y install gd-devel libjpeg-devel libpng-devel libiconv-devel freetype-devel libxml2 libxml2-devel curl-devel libxslt-devel libmcrypt-devel mhash mcrypt php-mysql

完成后需在 https://downloads.mysql.com/archives/community/ 下载安装mysql-develmysql-sharedmysql-client三个rpm包。
对于64位系统的主机,还需如下操作:

1
2
[root@opstrip opt]# ln -s /usr/lib64/libmysqlclient.so.16.0.0 /usr/lib/libmysqlclient.so
[root@opstrip opt]# ln -s /usr/lib64/libmysqlclient_r.so.16.0.0 /usr/lib/libmysqlclient_r.so

http://php.net/get/php-5.6.30.tar.gz/from/a/mirror 下载php-5.6.30.tar.gz并上传至虚拟主机,此处为/opt

1
2
[root@opstrip opt]# tar xf php-5.6.30.tar.gz
[root@opstrip opt]# cd php-5.6.30

2、编译安装PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 编译PHP
[root@opstrip php-5.6.30]# ./configure --prefix=/usr \
--with-config-file-path=/etc/php-fpm \
--with-openssl \
--with-pcre-regex \
--with-kerberos \
--with-libdir=lib \
--with-libxml-dir \
--with-mysql \
--with-mysqli \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-gd \
--with-iconv \
--with-zlib \
--with-zlib-dir=/usr/lib/php/extensions/no-debug-zts-20131226/ \
--with-xmlrpc \
--with-xsl \
--with-pear \
--with-gettext \
--with-curl \
--with-png-dir \
--with-jpeg-dir \
--with-freetype-dir \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mysqlnd \
--enable-zip \
--enable-inline-optimization \
--enable-shared \
--enable-libxml \
--enable-xml \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--enable-gd-native-ttf \
--enable-pcntl \
--enable-soap \
--enable-session \
--enable-opcache \
--enable-fpm \
--enable-maintainer-zts \
--enable-fileinfo
[root@opstrip php-5.6.30]# make && make install # 编译并安装PHP
[root@opstrip php-5.6.30]# php -v # 查看Nginx版本以确认安装成功
PHP 5.6.30 (cli) (built: Apr 6 2017 15:50:41)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies

配置PHP

3、安装后的配置

1
[root@opstrip opt]# cp /opt/php-5.6.30/php.ini-development /etc/php.ini

编辑php.ini文件,搜索并添加如下内容:

1
2
3
extension_dir = "/usr/lib/php/extensions/no-debug-zts-20131226/"
session.save_path = "/usr/tmp"
date.timezone = PRC

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[root@opstrip opt]# mkdir -p /etc/php-fpm.d
[root@opstrip opt]# cp /usr/etc/php-fpm.conf.default /etc/sysconfig/php-fpm.conf
[root@opstrip opt]# cp /usr/etc/php-fpm.d/www.conf.default /etc/php-fpm.d/www.conf
[root@opstrip opt]# cp /opt/php-5.6.30/sapi/fpm/php-fpm.service.in /usr/lib/systemd/system/php-fpm.service
[root@opstrip opt]# vi /usr/lib/systemd/system/php-fpm.service
# It's not recommended to modify this file in-place, because it
# will be overwritten during upgrades. If you want to customize,
# the best way is to use the "systemctl edit" command.
[Unit]
Description=The PHP FastCGI Process Manager
After=network.target
[Service]
Type=simple
PIDFile=/usr/var/run/php-fpm.pid
ExecStart=/usr/sbin/php-fpm --nodaemonize --fpm-config /etc/sysconfig/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@opstrip opt]# systemctl enable php-fpm # 使php-fpm开机启动
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
[root@opstrip opt]# systemctl start php-fpm # 启动php-fpm服务
[root@opstrip opt]# ps -ef|grep php-fpm # 查看php-fpm进程
root 14563 1 0 11:23 ? 00:00:00 php-fpm: master process (/etc/sysconfig/php-fpm.conf)
www 14564 14563 0 11:23 ? 00:00:00 php-fpm: pool www
www 14565 14563 0 11:23 ? 00:00:00 php-fpm: pool www
root 14567 14426 0 11:23 pts/2 00:00:00 grep --color=auto php-fpm

配置基于PHP的虚拟主机

4、PHP虚拟主机配置

安装好php-fpm后,就可以放PHP网站了。先整个wordpress玩玩。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 获取网站的Html代码
[root@opstrip opt]# wget https://cn.wordpress.org/wordpress-4.7.3-zh_CN.tar.gz
[root@opstrip opt]# tar xf wordpress-4.7.3-zh_CN.tar.gz
[root@opstrip opt]# mv wordpress wwwroot
[root@opstrip opt]# vi /etc/nginx/conf.d/wordpress.conf
server {
listen 80;
#listen [::]:80 ssl ipv6only=on;
server_name wp.opstrip.com;
root /opt/wwwroot/;
index index.php;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
# HTTPS server
server {
listen 443 ssl;
#listen [::]:443 ssl ipv6only=on;
server_name wp.opstrip.com;
ssl on;
ssl_certificate /etc/letsencrypt/live/opstrip.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/opstrip.com/privkey.pem;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
#ssl_ciphers HIGH:!aNULL:!MD5;
#ssl_prefer_server_ciphers on;
root /opt/wwwroot/;
index index.php;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@opstrip opt]# systemctl reload nginx

本地ping下wp.opstrip.com,如果DNS已经生效用浏览器打开 http://wp.opstrip.com 就开始安装wordpress了。具体可参考官方文档:zh-cn:安装 WordPress,根据需要配置数据库参数。因为使用的是RDS,所以需要在安装前从EC2登录到RDS创建数据库并授权给相应数据库账户。

--LNMP配置介绍完毕。

文章目录
  1. 1. 安装PHP
    1. 1.1. 1、安装依赖
    2. 1.2. 2、编译安装PHP
  2. 2. 配置PHP
    1. 2.1. 3、安装后的配置
  3. 3. 配置基于PHP的虚拟主机
    1. 3.1. 4、PHP虚拟主机配置