一、安装前的准备工作
1 、 yum update #更新系统
2 、 yum install gcc gcc-c++ autoconf automake cmake bison m4 libxml2 libxml2-devel libcurl-devel libjpeg-devel libpng-devel libicu-devel #安装 php 、 MySQL 、 Nngix 所依赖的包
3 、下载以下包 #我把所有原始档都下载在 root 目录,读者可自行修改原始档存放目录
3.1 libmcrypt-2.5.8.tar.gz
3.2 mcrypt-2.6.8.tar.gz
3.3 mhash-0.9.9.9.tar.gz
3.4 zlib-1.2.8.tar.gz
解压并安装如:

?

1
2
3
4

#tar -zvxf libmcrypt-2.5.8.tar.gz
#cd libmcrypt-2.5.8
#./configure
#make && make insatll

4 、在安装站群软件时如果提示有什么依赖包没有安装的可以再执行 yum install * -y (*表示相关包)
二、编译安装 Nginx
1 、去官网 http://nginx.org/en/download.html 下载最 nginx-1.10.1.tar.gz 的稳定版本
2 、编译步骤如下
1 、通过 winSCP 上传 nginx-1.10.1.tar.gz 到/root 目录下
1.1 groupadd -r nginx #新建 nginx 组
1.2 useradd -r -g nginx -s /bin/false nginx #新建无登入许可权的 nginx 使用者
1.3 id nginx #检视 nginx 组及使用者
2 、 tar -zvxf nginx-1.10.1.tar.gz
3 、 cd nginx-1.10.1
4 、可通过./configure –help 检视编译配置引数,也可参考 http://nginx.org/en/docs/configure.html,下列引数要写在一行中

?

1
2
3
4
5
6
7

./configure
    –prefix=/usr/local/nginx
    –modules-path=/usr/local/nginx/modules
    –with-http_ssl_module
    –pid-path=/usr/local/nginx/nginx.pid
    –user=nginx
    –group=nginx

5 、 make && make install #编译并安装
6 、启动 nginx
6.1 cd /usr/local/nginx
6.2 sbin/nginx #启动,可使用 sbin/nginx -? 检视 nginx 相关操作命令
7 、在/usr/lib/systemd/system 目录下新建 nginx.service 档案,这样就可以通过 systemctl stop|start|reload nginx.service 来操作 nginx,也可参考 https://www.nginx.com/resources/wiki/start /topics/examples/systemd/,内容如下:

?

1
2
3
4
5
6
7
8
9
10
11
12
13

[Unit]
   Description=The NGINX HTTP and reverse proxy server
   After=syslog.target network.target remote-fs.target nss-lookup.target
   [Service]
   Type=forking
   PIDFile=/usr/local/nginx/nginx.pid
   ExecStartPre=/usr/local/nginx/sbin/nginx -t
   ExecStart=/usr/local/nginx/sbin/nginx
   ExecReload=/usr/local/nginx/sbin/nginx -s reload
   ExecStop=/usr/local/nginx/sbin/nginx -s stop
   PrivateTmp=true
   [Install]
   WantedBy=multi-user.target

三、编译安装 MySQL
1 、去官网 http://dev.mysql.com/Downloads/MySQL-5.7/mysql-boost-5.7.14.tar.gz 下载带 boost 的 5.7.14 版本
2 、编译步骤如下
1 、用 winSCP 上传 mysql-boost-5.7.14.tar.gz 到/root 目录下
2 、 groupadd mysql
3 、 useradd -r -g mysql -s /bin/false mysql
4 、用 cmake 编译 mysql, 相关引数可以参考 https://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html,下列引数要写在一行

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14

cmake
    -DCMAKE_INSTALL_PREFIX=/usr/local/mysql
    -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock
    -DSYSCONFDIR=/usr/local/mysql/etc
    -DSYSTEMD_PID_DIR=/usr/local/mysql
    -DDEFAULT_CHARSET=utf8
    -DDEFAULT_COLLATION=utf8_general_ci
    -DWITH_INNOBASE_STORAGE_ENGINE=1
    -DWITH_ARCHIVE_STORAGE_ENGINE=1
    -DWITH_BLACKHOLE_STORAGE_ENGINE=1
    -DWITH_PERFSCHEMA_STORAGE_ENGINE=1
    -DMYSQL_DATADIR=/usr/local/mysql/data
    -DWITH_BOOST=boost
    -DWITH_SYSTEMD=1

5 、 make && make install
6 、配置 mysql 并初始化资料库
6.1 cd /usr/local/mysql #进入编译目录
6.2 chown -R mysql . #修改目录所有者
6.3 chgrp -R mysql . #修改目录组
6.4 cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld #配置 mysqld 服务
6.5 cp /usr/local/mysql/support-files/my-default.cnf /usr/local/mysql/my.cnf #配置 my.cnf
6.5.1 复制以下内容到 my.cnf 档案中的 [mysqld] 下

?

1
2
3
4
5
6

user = mysql
        basedir = /usr/local/mysql
        datadir = /usr/local/mysql/data
        port = 3306
        server_id = /usr/local/mysql/mysqld.pid
        socket = /usr/local/mysql/mysql.sock

6.5 chkconfig mysqld on #设定 mysqld 开机自启
6.6 bin/mysqld –initialize-insecure –user=mysql –basedir=/usr/local/mysql –datadir=/usr/local/mysql/data #初始化资料库
6.7 bin/mysqld –user=mysql & #启动 mysql, 如果报 Please read “Security” section of the manual to find out how to run mysqld as root!,就在 my.cnf 中加入 user=root, 表示以 root 使用者启动
7 、修改 root 使用者登入密码并允许 root 使用者远端登入
7.1 mysql -u root –skip-password
7.2 ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘123456’;
7.3 允许 root 使用者远端登入
7.3.1 use mysql;
7.3.2 update user set host=’%’ where user=’root’ and host=’localhost’; #允许       (update user set host=’localhost’ where user=’root’; #禁用)
7.3.3 flush privileges;
7.3.4 service mysqld restart
8 、解决 service mysqld start|stop 报 MySQL server PID file could not be found! 或者 Couldn’t find MySQL server (/usr/local/mysql/bin/mysqld_safe), 其实可通过阅读此档案解决相关错误
8.1 chmod 777 /usr/local/mysql #因我设定 mysqld.pid 档案储存在/usr/local/mysql 目录,所以保证其有可写许可权
8.2 通过 winSCP 修改/etc/init.d/mysqld 档案
8.2.1 basedir=/usr/local/mysql #手动指定
8.2.2 datadir=/usr/local/mysql/data #手动指定
8.2.3 mysqld_pid_file_path=/usr/local/mysql/mysqld.pid #手动指定
8.2.4 把此档案中所有未注释的含有 mysqld_safe 的字元替换成 mysqld
四、编译安装 php
1 、去官网 http://php.net/downloads.php 下载 php7.0.10 版本
2 、编译步骤如下
1 、用 winSCP 上传 php-7.0.10.tar.gz 到/root 目录下
2 、 tar -zvxf php-7.0.10.tar.gz #解压
3 、配置编译 php 引数, 可使用./configure –help 命令检视所有编译配置专案, 下列引数要写在一行中

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14

./configure
   –prefix=/usr/local/php
   –exec-prefix=/usr/local/php
   –datadir=/usr/local/php
   –with-config-file-path=/usr/local/php/etc
   –with-mysqli=mysqlnd
   –with-pdo-mysql=mysqlnd
   –with-fpm-user=nginx
   –with-fpm-group=nginx
   –with-gd
   –with-iconv
   –enable-mbstring
   –enable-fpm
   –enable-mysqlnd

4 、 make && make install #编译并安装
5 、 cd /usr/local/php #进入编译目录
6 、修改相关配置档案
6.1 cp /usr/local/php/etc/php.ini.default /usr/local/php/etc/php.ini #php.ini 中相关配置依专案需要自行修改,配置 nginx 支援 php 参考 http://php.net/manual/zh /install.unix.nginx.php
6.2 cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf #去掉 [global] 项下 pid 前的;
6.3 cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf #大致在 23 、 24 行修改 user 和 group 如:user = nginx,group = nginx
7 、 chmod 777 /usr/local/php/var/run #预设 PID 档案是写在/usr/local/php/var/run 这个目录中,所以修改目录许可权
8 、 sbin/php-fpm #启动 php, 可通过 sbin/php-fpm -h 检视相关操作命令列表
9 、在/usr/lib/systemd/system 目录下新建 php-fpm.service 档案,这样就可以通过 systemctl stop|start|reload php-fpm.service 来操作 php-fpm,内容如下:

?

1
2
3
4
5
6
7
8
9
10
11
12
13

[Unit]
   Description=The PHP FastCGI Process Manager
   After=syslog.target network.target
   Before=nginx.service
   [Service]
   Type=forking
   PIDFile=/usr/local/php/var/run/php-fpm.pid
   ExecStart=/usr/local/php/sbin/php-fpm
   ExecStop=/bin/kill -QUIT `cat /usr/local/php/var/run/php-fpm.pid`
   ExecReload=/bin/kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`
   PrivateTmp=true
   [Install]
   WantedBy=multi-user.target

五、至此在我的 VirturBox 中 CentOS7.2 下成功搭建了 LNMP 环境