搭建 LNMP 环境

本文是在Centos 6.8服务器上进行搭建PHP + Nginx + Mysql环境的指导性文章;

PHP7

下载PHP7的源码包

1
wget http://tw2.php.net/get/php-7.1.9.tar.gz/from/this/mirror -O php7.1.9.tar.gz

说明,-o参数指定存在下载文件的路径

解压

1
tar zxvf php7.1.9.tar.gz

说明,可以加-C参数指定安装目录;

安装依赖库

1
yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel mysql pcre-devel dpkg dpkg-dev bzip2-devel curl-devel db4-devel libXpm-devel gmp-devel libc-client-devel openldap-devel unixODBC-devel postgresql-devel libxslt-devel libxml2-devel

生成 Makefile,为下一步的编译做准备

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
./configure --prefix=/usr/local/php7 \
--with-curl \
--with-freetype-dir \
--with-gd \
--with-gettext \
--with-iconv-dir \
--with-kerberos \
--with-libdir=lib64 \
--with-libxml-dir \
--with-mysqli \
--with-openssl \
--with-pcre-regex \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-pear \
--with-png-dir \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--enable-fpm \
--enable-bcmath \
--enable-libxml \
--enable-inline-optimization \
--enable-gd-native-ttf \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-xml \
--enable-zip

编译

1
make

安装

1
make install

移动配置文件

1
2
3
4
cd /usr/local/php7/etc

cp /home/php/php-7.1.9/php.ini-development ../lib/php.ini
cp php-fpm.conf.default php-fpm.conf

通过软链接,加入系统命令

1
ln -s /usr/local/php7/bin/php /usr/local/bin/php7

确认安装成功

1
php7 -v

Nginx

下载nginx源码包,nginx 官方下载地址

笔者在 home 目录进行解压缩

1
tar zxvf nginx-1.13.5.tar.gz

配置、编译、安装

1
2
./configure
make && make install

通过软链接,加入系统命令

1
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx

检查是否安装成功

1
nginx -t

显示如下图

此处输入链接的描述

配置支持 PHP

修改phpphp.ini信息

1
2
3
4
5
6
7
8
extension_dir = /usr/local/php7/lib/php/extensions


enable_dl = On
cgi.fix_pathinfo=1
cgi.force_redirect = 0
fastcgi.impersonate = 1
cgi.rfc2616_headers = 1

修改nginx配置文件/usr/local/nginx/conf/nginx.con

1
2
3
4
5
6
7
location ~ \.php$ {                                                        
root /usr/local/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}

验证
启动内置 fastcgi 管理器

1
/usr/local/php7/bin/php-cgi -b 127.0.0.1:9000 -c /usr/local/php7/etc/php.ini&

重新加载nginx配置与

1
nginx -s reload

通过浏览器访问nginx所在服务器 http://192.168.31.168/phpinfo.php; 出现php相关信息页面,成功;

关闭nginx服务器与fastcgi 管理器

1
2
nginx -s stop
killall -9 php-cgi

启用 PHP-Fpm

为什么用php-fpm替换php-cgi,参考文章如何通俗地解释 CGI、FastCGI、php-fpm 之间的关系?; 笔者也还没怎么理清呢;

至于步骤,只需要把这部分的操作替换上面启动内置 fastcgi 管理器即可;

准备用户及用户组

1
2
groupadd www
useradd -g www www

通过cat /etc/passwd查看当前系统有哪些用户;通过cat /etc/group命令来查看当前系统有哪些用户组;

文件配置

1
cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf

同时修改文件/usr/local/php7/etc/php-fpm.d/www.conf内容

1
2
user = www
group = www

启动 php-fpm

1
/usr/local/php7/sbin/php-fpm

查看启动

1
netstat -lnt | grep 9000

关闭php-fpm

1
pkill php-fpm

Mysql

Mysql 官网页面,选择 Linux Generuc 版本,下载最新版本 (mysql-5.7.19-linux-glibc2.12-x86_64.tar.gz);

解压

1
2
3
cd /home
tar zxvf mysql-5.7.19-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.19-linux-glibc2.12-x86_64 mysql

目录权限设置
如果mysql目录下没有data目录,手动建一个。

1
2
cd /home/mysql
mkdir data

mysql及其下所有的目录所有者和组均设为mysql

1
chown mysql:mysql -R .

通过软链接,加入系统命令

1
ln -s /home/mysql/bin/mysql /usr/local/bin/mysql

初始化

1
/home/mysql/bin/mysqld --initialize --user=mysql --datadir=/home/mysql/data --basedir=/home/mysql

初始化成功,显示如下:
此处输入链接的描述

注意最后一行,这也是和之有版本不同的地方,它给了root一个初始密码,后面要登录的时候要用到这个密码。

配置
由于在5.7.18开始,二进制包不再包含示例文件my-default.cnf,参考5.7.17版本中的文件,在/etc/my.cnf中复制以下内容:

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
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It‘s a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin

# These are commonly set, remove the # and set as required.
basedir = /home/mysql
datadir = /home/mysql/data
port = 3306
# server_id = .....
socket = /home/mysql/tmp/mysql.sock

# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[client]
socket = /home/mysql/tmp/mysql.sock

注意,tmp目录不存在,请创建之。注意权限 chown -R mysql:mysql tmp

运行

  1. 运行服务器程序

    1
    /home/mysql/bin/mysqld_safe&

    在这个启动脚本里已默认设置--user=mysql;在脚本末尾加&表示设置此进程为后台进程,区别就是在控制台输入bg,即可将当前进程转入后台,当前shell可进行其他操作。

  2. 停止 mysql

    1
    /home/mysql/bin/mysqladmin -uroot -p shutdown

客户端连接测试

1
mysql -uroot -p

输入初始化时提供的默认密码,然后进行mysql客户端界面,修改密码

1
alter user 'root'@'localhost' identified by '111111';

设置mysql以服务运行并且开机启动
/home/mysql/support-files/mysql.server 拷贝为/etc/init.d/mysql并设置运行权限

1
2
cp /home/mysql/support-files/mysql.server /etc/init.d/mysql
chmod +x /etc/init.d/mysql

mysql注册为开机启动的服务

1
chkconfig --add mysql

当然也可以手动进行服务的开启和关闭:

1
2
/etc/init.d/mysql start
/etc/init.d/mysql stop