一、安裝前的準備工作
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 環境