在CentOS7上配置LNMP环境:PHP篇
上篇介绍了Nginx的安装配置及Let’s Encrypt证书的自动签发与自动续期。这里主要介绍PHP的安装及在Nginx中的转发配置。
安装PHP
1、安装PHP
编译安装PHP之前需要安装PHP依赖包:
[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-devel 、 mysql-shared 、 mysql-client 三个rpm包。
对于64位系统的主机,还需如下操作:
[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 。
[root@opstrip opt]# tar xf php-5.6.30.tar.gz [root@opstrip opt]# cd php-5.6.30
2、编译安装PHP
# 编译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
3、安装后的配置
[root@opstrip opt]# cp /opt/php-5.6.30/php.ini-development /etc/php.ini
编辑 php.ini 文件,搜索并添加如下内容:
extension_dir = "/usr/lib/php/extensions/no-debug-zts-20131226/" session.save_path = "/usr/tmp" date.timezone = PRC
[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
4、PHP 虚拟主机 配置
安装好php-fpm后,就可以放PHP网站了。先整个wordpress玩玩。
# 获取网站的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
上篇介绍了Nginx的安装配置及Let’s Encrypt证书的自动签发与自动续期。这里主要介绍PHP的安装及在Nginx中的转发配置。 本文来自无奈人生安全网
安装PHP
1、安装PHP
编译安装PHP之前需要安装PHP依赖包:
[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-devel 、 mysql-shared 、 mysql-client 三个rpm包。
本文来自无奈人生安全网
对于64位系统的主机,还需如下操作:
[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.sowww.wnhack.com
在 http://php.net/get/php-5.6.30.tar.gz/from/a/mirror 下载 php-5.6.30.tar.gz 并上传至虚拟主机,此处为 /opt 。 内容来自无奈安全网
[root@opstrip opt]# tar xf php-5.6.30.tar.gz [root@opstrip opt]# cd php-5.6.30无奈人生安全网
2、编译安装PHP
# 编译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
无奈人生安全网
3、安装后的配置
[root@opstrip opt]# cp /opt/php-5.6.30/php.ini-development /etc/php.ini copyright 无奈人生
编辑 php.ini 文件,搜索并添加如下内容:
内容来自无奈安全网
extension_dir = "/usr/lib/php/extensions/no-debug-zts-20131226/"
session.save_path = "/usr/tmp"
date.timezone = PRC
www.wnhack.com
[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 无奈人生安全网
4、PHP 虚拟主机 配置
安装好php-fpm后,就可以放PHP网站了。先整个wordpress玩玩。 本文来自无奈人生安全网
# 获取网站的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本文来自无奈人生安全网