CentOS6.9原始碼編譯安裝nginx+php7+mysql環境

再現理想發表於2019-02-14

這篇筆記記錄了在CentOS6.9中原始碼編譯安裝nginx1.14,php7,mysql5.6的過程,並附上啟動指令碼,記錄了整合的過程,mysql5.6並未使用官方二進位制包,而是自己通過原始碼編譯安裝的

相關筆記:
CentOS7原始碼編譯安裝nginx+php7.2+mysql5.7並使用systemctl管理
CentOS7yum安裝nginx+php7+mysql
CentOS6.9yum安裝nginx+php7+mysql環境
安裝nginx
1.安裝nginx所需依賴

yum install wget gcc gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-devel

安裝完後的大致結果

......
已安裝:
  gcc.x86_64 0:4.4.7-23.el6 gcc-c++.x86_64 0:4.4.7-23.el6 openssl-devel.x86_64 0:1.0.1e-57.el6 pcre-devel.x86_64 0:7.8-7.el6 wget.x86_64 0:1.12-10.el6 zlib-devel.x86_64 0:1.2.3-29.el6

作為依賴被安裝:
  cloog-ppl.x86_64 0:0.15.7-1.2.el6                cpp.x86_64 0:4.4.7-23.el6                    glibc-devel.x86_64 0:2.12-1.212.el6          glibc-headers.x86_64 0:2.12-1.212.el6         
  kernel-headers.x86_64 0:2.6.32-754.9.1.el6       keyutils-libs-devel.x86_64 0:1.4-5.el6       krb5-devel.x86_64 0:1.10.3-65.el6            libcom_err-devel.x86_64 0:1.41.12-24.el6      
  libgomp.x86_64 0:4.4.7-23.el6                    libkadm5.x86_64 0:1.10.3-65.el6              libselinux-devel.x86_64 0:2.0.94-7.el6       libsepol-devel.x86_64 0:2.0.41-4.el6          
  libstdc++-devel.x86_64 0:4.4.7-23.el6            mpfr.x86_64 0:2.4.1-6.el6                    ppl.x86_64 0:0.10.2-11.el6                  

完畢!
[root@jmsite ~]#

2.建立一個不能登入的nginx啟動使用者

groupadd www-data
useradd -s /sbin/nologin -g www-data www-data

3.建立原始碼儲存目錄,下載nginx原始碼,當前穩定版為nginx-1.14.2

mkdir -p /usr/local/src
cd /usr/local/src
wget -c http://nginx.org/download/nginx-1.14.2.tar.gz

4.解壓

tar -zxvf nginx-1.14.2.tar.gz
cd /usr/local/src/nginx-1.14.2

5.編譯前配置

./configure --prefix=/usr/local/nginx 
--sbin-path=/usr/local/nginx/sbin/nginx 
--conf-path=/usr/local/nginx/conf/nginx.conf 
--pid-path=/var/run/nginx/nginx.pid 
--error-log-path=/var/log/nginx/error.log 
--http-log-path=/var/log/nginx/access.log 
--user=www-data 
--group=www-data 
--with-http_stub_status_module 
--with-http_ssl_module 
--with-http_gzip_static_module 
--with-pcre
# --with-http_stub_status_module 監控nginx狀態
# --with-http_ssl_module 支援ssl
# --with-http_gzip_static_module 靜態壓縮

6.編譯,安裝

make
make install

7.啟動並檢視nginx程式

[root@jmsite nginx-1.14.2]# /usr/local/nginx/sbin/nginx
[root@jmsite nginx-1.14.2]# ps aux | grep nginx
root       4275  0.0  0.1  46856  1192 ?        Ss   00:08   0:00 nginx: master process /usr/local/nginx/sbin/nginx
www-data   4276  0.0  0.1  47288  1772 ?        S    00:08   0:00 nginx: worker process      
root       4278  0.0  0.0 103336   900 pts/1    S+   00:08   0:00 grep nginx

檢視nginx版本

[root@jmsite ~]# /usr/local/nginx/sbin/nginx -v
nginx version: nginx/1.14.2

8.建立nginx啟動指令碼(檢視官方指令碼)

vim /etc/init.d/nginx

寫入以下指令碼資訊

#! /bin/sh
# chkconfig: 2345 55 25
# Description: Startup script for nginx webserver on Debian. Place in /etc/init.d and
# run `update-rc.d -f nginx defaults`, or use the appropriate command on your
# distro. For CentOS/Redhat run: `chkconfig --add nginx`

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=nginx
NGINX_BIN=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/$NAME.conf
PIDFILE=/var/run/nginx/$NAME.pid
if [ -s /bin/ss ]; then
    StatBin=/bin/ss
else
    StatBin=/bin/netstat
fi


case "$1" in
    start)
        echo -n "Starting $NAME... "

        if $StatBin -tnpl | grep -q nginx;then
            echo "$NAME (pid `pidof $NAME`) already running."
            exit 1
        fi

        $NGINX_BIN -c $CONFIGFILE

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    stop)
        echo -n "Stoping $NAME... "

        if ! $StatBin -tnpl | grep -q nginx; then
            echo "$NAME is not running."
            exit 1
        fi

        $NGINX_BIN -s stop

        if [ "$?" != 0 ] ; then
            echo " failed. Use force-quit"
            exit 1
        else
            echo " done"
        fi
        ;;

    status)
        if $StatBin -tnpl | grep -q nginx; then
            PID=`pidof nginx`
            echo "$NAME (pid $PID) is running..."
        else
            echo "$NAME is stopped."
            exit 0
        fi
        ;;

    force-quit|kill)
        echo -n "Terminating $NAME... "

        if ! $StatBin -tnpl | grep -q nginx; then
            echo "$NAME is is stopped."
            exit 1
        fi

        kill `pidof $NAME`

        if [ "$?" != 0 ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
        ;;

    restart)
        $0 stop
        sleep 1
        $0 start
        ;;

    reload)
        echo -n "Reload service $NAME... "

        if $StatBin -tnpl | grep -q nginx; then
            $NGINX_BIN -s reload
            echo " done"
        else
            echo "$NAME is not running, can`t reload."
            exit 1
        fi
        ;;

    configtest)
        echo -n "Test $NAME configure files... "

        $NGINX_BIN -t
        ;;

    *)
        echo "Usage: $0 {start|stop|restart|reload|status|configtest|force-quit|kill}"
        exit 1
        ;;

esac

9.設定nginx開機啟動

# 修改指令碼執行許可權
chmod 755 /etc/init.d/nginx
# nginx加入服務
chkconfig --add nginx
# nginx 設定為開機啟動
chkconfig nginx on
#測試一下
service nginx status

nginx詳細配置請移步(nginx的configure引數,配置檔案,虛擬主機配置,訊號控制)
安裝mysql
1.建立mysql使用者,不允許登陸和不建立主目錄

groupadd mysql
useradd -s /sbin/nologin -g mysql -M mysql

2.建立mysql相應目錄,並設定許可權

mkdir -p /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql
mkdir -p /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql
mkdir -p /var/run/mysqld
chown -R mysql:mysql /var/run/mysqld

注:MySQL從5.5版本開始,通過./configure進行編譯配置方式已經被取消,取而代之的是cmake工具。因此,我們首先要在系統中原始碼編譯安裝cmake工具。
3.安裝依賴

yum install make cmake bison-devel  ncurses-devel

4.進入原始碼存放目錄,下載,解壓mysql

cd /usr/local/src/
wget -c https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.37.tar.gz
tar -zxvf mysql-5.6.37.tar.gz
cd mysql-5.6.37

5.編譯前配置(檢視官方文件)

cmake 
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql 
-DMYSQL_DATADIR=/usr/local/mysql/data 
-DSYSCONFDIR=/etc 
-DWITH_MYISAM_STORAGE_ENGINE=1 
-DWITH_INNOBASE_STORAGE_ENGINE=1 
-DWITH_MEMORY_STORAGE_ENGINE=1 
-DWITH_READLINE=1 
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock 
-DMYSQL_TCP_PORT=3306 
-DENABLED_LOCAL_INFILE=1 
-DWITH_PARTITION_STORAGE_ENGINE=1 
-DEXTRA_CHARSETS=all 
-DDEFAULT_CHARSET=utf8 
-DDEFAULT_COLLATION=utf8_general_ci 
-DMYSQL_USER=mysql 
-DWITH_DEBUG=0 
-DWITH_SSL=system
......
CMake Warning:
  Manually-specified variables were not used by the project:

    MYSQL_USER
    WITH_MEMORY_STORAGE_ENGINE
    WITH_READLINE


-- Build files have been written to: /usr/local/src/mysql-5.6.37
#配置完畢,CMake Warning可以略過

6.編譯,安裝(-j4:表示cpu核心數,我的虛擬機器設定4核,所以-j4)

make -j4
make install

7.執行初始化配置指令碼,建立系統自帶的資料庫和表

/usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql

8.設定mysql啟動指令碼,將mysql.server複製到init.d目錄下

cp support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
chkconfig mysqld on
service mysqld start

9.如果出現如下錯誤提示則修改my.cnf中datadir為你的資料目錄

[root@jmsite mysql-5.6.37]# service mysqld start
Starting MySQL.181220 05:27:46 mysqld_safe Directory `/var/lib/mysql` for UNIX socket file don`t exists.
 ERROR! The server quit without updating PID file (/var/lib/mysql/jmsite.cn.pid).
[root@jmsite mysql-5.6.37]# vim /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
......
[root@jmsite mysql-5.6.37]# service mysqld start
Starting MySQL. SUCCESS!

10.將mysql加入環境變數,並立刻生效

vim /etc/profile
#尾部加入下面兩行
PATH=$PATH:/usr/local/mysql/bin/
export PATH
#執行source使環境變數立即生效
source /etc/profile

11.測試一下

[root@jmsite mysql-5.6.37]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.6.37 Source distribution

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type `help;` or `h` for help. Type `c` to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql> 

12.開啟log

tail -f /var/log/mysqld.log
......
2018-12-20 05:13:34 16977 [Note] RSA private key file not found: /usr/local/mysql/data//private_key.pem. Some authentication plugins will not work.
2018-12-20 05:13:34 16977 [Note] RSA public key file not found: /usr/local/mysql/data//public_key.pem. Some authentication plugins will not work.
......

13.發現沒有公鑰和私鑰,開始生成

cd /usr/local/mysql/data
openssl genrsa -out private_key.pem 1024
openssl rsa -in private_key.pem -pubout > public_key.pem

安裝php
安裝php依賴(gcc gcc-c++ openssl openssl-devel等依賴在上面安裝nginx時已安裝)

yum install libxml2 libxml2-devel curl-devel openjpeg openjpeg-devel openjpeg-libs libjpeg libjpeg-devel libpng freetype libpng-devel freetype-devel

下載,解壓原始碼

cd /usr/local/src/
wget -c http://cn2.php.net/get/php-7.2.13.tar.gz
tar -xzvf php-7.2.13.tar.gz
cd php-7.2.13

編譯前配置

./configure --prefix=/usr/local/php72 
--with-config-file-path=/usr/local/php72/etc 
--with-config-file-scan-dir=/usr/local/php72/etc/php.d 
--with-mhash 
--disable-debug 
--disable-rpath 
--enable-mysqlnd 
--with-mysqli 
--with-pdo-mysql 
--enable-fpm 
--with-fpm-user=www-data 
--with-fpm-group=www-data 
--with-gd 
--with-iconv 
--with-zlib 
--enable-bcmath 
--enable-xml 
--enable-shmop 
--enable-sysvsem 
--enable-inline-optimization 
--enable-mbregex 
--enable-mbstring 
--enable-ftp 
--with-openssl 
--enable-pcntl 
--enable-sockets 
--with-xmlrpc 
--enable-zip 
--enable-soap 
--without-pear 
--with-gettext 
--enable-session 
--with-curl 
--with-jpeg-dir 
--with-png-dir 
--with-freetype-dir 
--enable-opcache

編譯,安裝

make -j4
make install

設定環境變數

vim /etc/profile
#檔案末尾加入如下兩行程式碼
PATH=$PATH:/usr/local/php72/bin/:/usr/local/php72/sbin/
export PATH
#使之立即生效
source /etc/profile
#測試一下
[root@jmsite php-fpm.d]# php -v
PHP 7.2.13 (cli) (built: Dec 20 2018 07:41:00) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

設定php.ini和php-fpm.conf,www.conf

#進入原始碼目錄
cd /usr/local/src/php-7.2.13
#複製示例配置
cp php.ini-development /usr/local/php72/etc/php.ini
#或
cp php.ini-production /usr/local/php72/etc/php.ini
#進入php.ini目錄
cd /usr/local/php72/etc
#開啟配置檔案
vim /usr/local/php72/etc/php.ini
#更改pdo_mysql.default_socket為上面安裝mysql時.sock設定的位置
pdo_mysql.default_socket = /var/lib/mysql/mysql.sock
#如果不設定,php通過pdo連線mysql時會報SQLSTATE[HY000] [2002] No such file or directory
#複製fpm示例配置
cp php-fpm.conf.default php-fpm.conf
#進入php-fpm.d目錄
cd /usr/local/php72/etc/php-fpm.d
#複製www.conf
cp www.conf.default www.conf

複製php-fpm啟動指令碼

cp /usr/local/src/php-7.2.13/sapi/fpm/init.d.php-fpm /etc/init.d/php72-fpm
#設定許可權
chmod 755 /etc/init.d/php72-fpm
#作為一項新的系統服務新增
chkconfig --add php72-fpm
#設定開機啟動
chkconfig php72-fpm on
#測試一下
[root@jmsite ~]# service php72-fpm start
Starting php-fpm  done
[root@jmsite ~]# service php72-fpm status
php-fpm (pid 1516) is running...

驗證安裝的nginx,php,mysql
1.編輯nginx配置檔案

vim /usr/local/nginx/conf/nginx.conf
#更改執行使用者
user  www-data;
#編輯server段,預設檔案新增index.php
        location / {
            root   html;
            index  index.php index.html index.htm;
        }
#匹配php的配置塊取消註釋並更改/scripts為$document_root
        location ~ .php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

2.儲存並退出,重啟載入nginx配置

service nginx reload

3.nginx預設的web目錄下新建index.php

vim /usr/local/nginx/html/index.php

4.輸入如下php程式碼

<?php
$dbms=`mysql`;     //資料庫型別
$host=`localhost`; //資料庫主機名
$dbName=`mysql`;    //使用的資料庫
$user=`root`;      //資料庫連線使用者名稱
$pass=``;          //對應的密碼
$dsn="$dbms:host=$host;dbname=$dbName";
try {
    $dbh = new PDO($dsn, $user, $pass); //初始化一個PDO物件
    echo "連線成功<br/>";
    foreach ($dbh->query(`SELECT db from db`) as $row) {
        print_r($row);
    }

    $dbh = null;
} catch (PDOException $e) {
    die ("Error!: " . $e->getMessage() . "<br/>");
}
?>

5.瀏覽器訪問你的站點,如果看到下圖的內容,說明你成功了!!!

原文地址:https://www.jmsite.cn/blog-155.html


相關文章