搭建lnmp環境

helloworldcoding發表於2019-02-16

作業系統:linux Mint 18.2

編譯安裝php7.2


1.先安裝編譯工具
    sudo apt install gcc  automake autoconf libtool g++ 
    #在centos下,是yum install gcc automake autoconf libtool gcc-c++
2.安裝基礎庫
    sudo apt install libxml2-dev openssl libssl-dev curl libcurl4-gnutls-dev libjpeg-dev libpng12-dev libfreetype6 libfreetype6-dev libmcrypt4 libmcrypt-dev
    #在centos下:yum install gd zlib zlib-devel openssl openssl-devel libxml2 libxml2-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libmcrypt libmcrypt-devel
2.下載php7.2.tar.gz原始碼
3.解壓 tar -xzvf php7.2.tar.gz

4.編譯安裝

   ./configure --prefix=/usr/local/php7/ 
--with-pcre-regex=/opt/pcre-8.39/ 
--with-pcre-dir=/opt/pcre-8.39/ 
--with-openssl-dir=/opt/openssl-1.0.2h/ 
--with-openssl=/opt/openssl-1.0.2h/ 
--with-zlib-dir=/opt/zlib-1.2.8/ 
--with-zlib=/opt/zlib-1.2.8/ 
--with-curl=/opt/curl-7.50.1/ 
--with-mcrypt=/opt/libmcrypt-2.5.7/ 
--with-bz2 
--with-ldap=/opt/openldap-2.4.30/ 
--with-gd 
--with-pdo-mysql=shared,mysqlnd 
--with-freetype-dir 
--with-jpeg-dir 
--with-png-dir 
--with-gettext 
--with-iconv-dir 
--with-kerberos 
--with-libxml-dir 
--with-mhash 
--with-zlib-dir 
--without-pdo-sqlite 
--with-pear 
--with-xmlrpc 
--with-xsl 
--enable-opcache 
--enable-soap 
--enable-sockets 
--enable-sysvsem 
--enable-xml 
--enable-bcmath 
--enable-libxml 
--enable-inline-optimization 
--enable-mbregex 
--enable-zip 
--enable-ftp 
--enable-fpm 
--enable-mbstring 
--enable-gd-native-ttf 
--enable-gd-jis-conv 
--enable-calendar 
--enable-fpm 
--enable-pcntl 
--enable-shmop 
--enable-exif 
--enable-pcntl 
--enable-wddx 
--enable-intl 
--enable-libgcc
./buildconf
./configure --prefix=/usr/local/php7 --exec-prefix=/usr/local/php7 --bindir=/usr/local/php7/bin --sbindir=/usr/local/php7/sbin --includedir=/usr/local/php7/include --libdir=/usr/local/php7/lib/php --mandir=/usr/local/php7/php/man --with-config-file-path=/usr/local/php7/etc --with-mysql-sock=/var/lib/mysql/mysql.sock --with-mcrypt=/usr/include --with-mhash --with-openssl --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-gd --with-iconv --with-zlib --enable-zip --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-xml --enable-bcmath --enable-shmop  --enable-mbregex --enable-mbstring --enable-ftp --enable-gd-native-ttf --enable-pcntl --enable-sockets --with-xmlrpc --enable-soap --without-pear --with-gettext --enable-session --with-curl --with-openssl --with-jpeg-dir --with-freetype-dir --enable-opcache --enable-fpm --with-fpm-user=www --with-fpm-group=www --without-gdbm --enable-fileinfo --enable-sysvsem  --enable-sysvshm  --enable-sysvmsg --enable-pcntl --enable-maintainer-zts --enable-pthreads



configure: WARNING: unrecognized options: –with-mcrypt, –with-mhash–with-openssl, –enable-gd-native-ttf, –enable-pthreads

Please check your libxml2 installation

然後修改fpm配置檔案
cd /usr/local/php7/etc
sudo cp php-fpm.conf.default php-fpm.conf
但是執行php-fpm時報錯:
../sbin/php-fpm
[23-Jul-2017 15:24:07] WARNING: Nothing matches the include pattern `/usr/local/php7/etc/php-fpm.d/*.conf` from /usr/local/php7/etc/php-fpm.conf at line 125.
[23-Jul-2017 15:24:07] ERROR: failed to open error_log (/usr/local/php7/var/log/php-fpm.log): Permission denied (13)
[23-Jul-2017 15:24:07] ERROR: failed to post process the configuration
[23-Jul-2017 15:24:07] ERROR: FPM initialization failed

新增使用者和使用者組(ubuntu,mint)
sudo addgroup www
sudo adduser www
對deepin 來說 sudo adduser www 就可以建立www使用者和www使用者組了。
然後改變php目錄的使用者和使用者組
sudo chown -R www:www /usr/local/php7/
拷貝原始碼中的php.ini-develop檔案到/usr/local/php7/etc/php.ini
把/usr/local/php7/etc/php-fpm.conf.default 複製為php-fpm.conf
把/usr/local/php7/etc/php-fpm.d/www.conf.default 複製為www.conf

然後再執行php-fpm
sudo ./php-fpm
執行成功,ps axu | grep php如下:

wm@wm-Lenovo /usr/local/php7/sbin $ ps axu | grep php
root     11492  0.0  0.1 177576 11420 ?        Ss   15:40   0:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf)
www      11493  0.0  0.1 177576  8748 ?        S    15:40   0:00 php-fpm: pool www
www      11494  0.0  0.1 177576  8744 ?        S    15:40   0:00 php-fpm: pool www
wm       11496  0.0  0.0  15964  1016 pts/1    S+   15:41   0:00 grep --color=auto php

nginx編譯安裝
1.下載nginx原始碼,並解壓
2.下載zlib和pcre原始碼,並解壓。
3.configure的引數,參考nginx官方

./configure
    --sbin-path=/usr/local/nginx/nginx
    --conf-path=/usr/local/nginx/nginx.conf
    --pid-path=/usr/local/nginx/nginx.pid
    --with-http_ssl_module
    --with-pcre=../pcre-8.40
    --with-zlib=../zlib-1.2.11
    --user=www
    --group=www

make && make install
與php安裝一樣,也修改/usr/local/nginx的使用者和使用者組為www:www

mysql編譯安裝

https://dev.mysql.com/downloads/mysql/
https://dev.mysql.com/downloads/file/?id=471659
-- https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.19.tar.gz
編譯安裝需要參考如下:
https://dev.mysql.com/doc/refman/5.7/en/source-installation.html
https://dev.mysql.com/doc/refman/5.7/en/installing-source-distribution.html

http://www.cnblogs.com/goozgk/p/5645041.html
# mkdir -p /usr/local/mysql /mydata/data
# groupadd -r mysql
# useradd -g mysql -r -d /data/mydata mysql
# chown -R mysql.mysql /usr/local/mysql /mydata/data

# wget http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
# tar xf mysql-boost-5.7.13.tar.gz
# cd mysql-5.7.13
# yum install -y openssl openssl-devel

# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql 
-DMYSQL_DATADIR=/mydata/data 
-DSYSCONFDIR=/etc 
-DWITH_INNOBASE_STORAGE_ENGINE=1 
-DWITH_ARCHIVE_STORAGE_ENGINE=1 
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 
-DWITH_READLINE=1 
-DWITH_LIBWRAP=0 
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock 
-DWITH_SSL=system 
-DWITH_ZLIB=system 
-DWITH_BOOST=/home/alex/Downloads/boost_1_59_0 
-DDEFAULT_CHARSET=utf8 
-DDEFAULT_COLLATION=utf8_general_ci
如果編譯出現各種奇葩失敗問題,乾脆 sudo apt-get install mysql-server 得了。

localhost 和127.0.0.1的問題
連線MySQL資料庫有兩種方式:TCP/IP(一般理解的埠的那種)和Unix套接字(一般叫socket或者sock)。大部分情況下,可以用localhost代表本機127.0.0.1,但是在MySQL連線時,二者不可混用,而且MySQL中許可權設定中localhost與127.0.0.1也是分開設定的。當設定為127.0.0.1時,系統通過TCP/IP方式連線資料庫;當設定為localhost時,系統通過socket方式連線資料庫。
解決:首先要看本機MySQL的socket套接字檔案在哪裡,檢視命令是:
mysqld –verbose –help | grep socket
輸出結果顯示套接字檔案的位置,比如:這臺伺服器顯示的是
socket /var/run/mysqld/mysqld.sock
然後修改php的配置檔案php.ini與之對應起來就好了。
找到這一項:
mysql.default_socket =
一般來說這一項都是空的,改成:
mysql.default_socket = /var/run/mysqld/mysqld.sock
這裡應寫上一步查詢到的檔案,根據你的情況設定。至此php配置就修改好了,如果是CLI(命令列)方式或者CGI方式的話,立即就生效,如果是FASTCGI方式,需要重啟一下fastcgi程式。

php-fpm服務啟動指令碼
先建立自啟動檔案:/etc/init.d/php-fpm
內容如下:

#! /bin/sh

### BEGIN INIT INFO
# Provides:          php-fpm
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts php-fpm
# Description:       starts the PHP FastCGI Process Manager daemon
### END INIT INFO

prefix=/usr/local/php
exec_prefix=${prefix}

php_fpm_BIN=${exec_prefix}/sbin/php-fpm
php_fpm_CONF=${prefix}/etc/php-fpm.conf
php_fpm_PID=${prefix}/var/run/php-fpm.pid


php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID"


wait_for_pid () {
    try=0

    while test $try -lt 35 ; do

        case "$1" in
            `created`)
            if [ -f "$2" ] ; then
                try=``
                break
            fi
            ;;

            `removed`)
            if [ ! -f "$2" ] ; then
                try=``
                break
            fi
            ;;
        esac

        echo -n .
        try=`expr $try + 1`
        sleep 1

    done

}

case "$1" in
    start)
        echo -n "Starting php-fpm "

        $php_fpm_BIN --daemonize $php_opts

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

        wait_for_pid created $php_fpm_PID

        if [ -n "$try" ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;

    stop)
        echo -n "Gracefully shutting down php-fpm "

        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi

        kill -QUIT `cat $php_fpm_PID`

        wait_for_pid removed $php_fpm_PID

        if [ -n "$try" ] ; then
            echo " failed. Use force-quit"
            exit 1
        else
            echo " done"
        fi
    ;;

    status)
        if [ ! -r $php_fpm_PID ] ; then
            echo "php-fpm is stopped"
            exit 0
        fi

        PID=`cat $php_fpm_PID`
        if ps -p $PID | grep -q $PID; then
            echo "php-fpm (pid $PID) is running..."
        else
            echo "php-fpm dead but pid file exists"
        fi
    ;;

    force-quit)
        echo -n "Terminating php-fpm "

        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi

        kill -TERM `cat $php_fpm_PID`

        wait_for_pid removed $php_fpm_PID

        if [ -n "$try" ] ; then
            echo " failed"
            exit 1
        else
            echo " done"
        fi
    ;;

    restart)
        $0 stop
        $0 start
    ;;

    reload)

        echo -n "Reload service php-fpm "

        if [ ! -r $php_fpm_PID ] ; then
            echo "warning, no pid file found - php-fpm is not running ?"
            exit 1
        fi

        kill -USR2 `cat $php_fpm_PID`

        echo " done"
    ;;

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

esac

修改php-fpm的許可權,設定啟動

# 設定許可權
chmod 755 /etc/init.d/php-fpm
# php-fpm加入服務
chkconfig --add php-fpm 
# php-fpm 234級別下設定為啟動
chkconfig php-fpm on
# 檢視php-fpm服務當前配置
chkconfig --list php-fpm
php-fpm         0:off   1:off   2:on    3:on    4:on    5:on    6:off

如果是ubuntu下( chkconfig 已經不能在ubuntu下使用,應該:)
# 設定許可權
chmod 755 /etc/init.d/php-fpm
sudo update-rc.d php-fpm defaults

// 新增
update-rc.d ServiceName defaults
// 刪除
update-rc.d ServiceName remove

最後應該是sudo service php-fpm force-stop一下,不然直接service php-fpm restart 可能會報錯:

service 控制nginx

#!/bin/sh  
  
### BEGIN INIT INFO  
# Provides:    nginx  
# Required-Start:  
# Required-Stop:  
# Default-Start:        2 3 4 5  
# Default-Stop:        0 1 6  
# Short-Description: nginx  
# Description: nginx server  
### END INIT INFO  
  
. /lib/lsb/init-functions  # ubuntu
# . /etc/lib/functions # centos
  
PROGRAM=/usr/local/nginx/nginx  
  
  
test -x $PROGRAM || exit 0  
  
case "$1" in  
  start)  
    log_begin_msg "Starting Nginx server"  
    /usr/local/nginx/nginx  
    log_end_msg 0  
    ;;  
  stop)  
    PID=`cat /usr/local/nginx/nginx.pid`  
    log_begin_msg "Stopping Nginx server"  
    if [ ! -z "$PID" ]; then  
        kill -15 $PID  
    fi  
    log_end_msg 0  
    ;;  
  restart)  
    $0 stop  
    $0 start  
    ;;  
  *)  
    log_success_msg "Usage: service nginx {start|stop|restart}"  
    exit 1  
esac  
  
exit 0 

相關文章