LNMP編譯安裝

文章轉(zhuǎn)載來源:http://www.178linux.com/75185

系統(tǒng):CentOS 7.3
IP: 172.16.0.11
版本:nginx-1.10.3 php-5.6.30 mysql-5.6.30

一. 安裝開發(fā)包組
yum -y groupinstall "Development Tools" "Server Platform Development"
二.編譯安裝nginx-1.10.3
(1) 安裝依賴包
yum -y install openssl-devel pcre-devel zlib-devel
(2) 創(chuàng)建nginx用戶和組
useradd -r nginx
(3) 編譯安裝
tar xf nginx-1.10.3.tar.gz
cd nginx-1.10.3/
./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --user=nginx --group=nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_ssl_module --with-http_gzip_static_module --with-debug --with-http_stub_status_module
make && make install
(4) 啟動nginx
/usr/local/nginx/sbin/nginx
(5) nginx開機啟動(啟動腳本)

vim /etc/rc.d/init.d/nginx
#! /bin/bash
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
#
# processname: nginx
# config:      /etc/nginx/nginx.conf
# pidfile:     /var/run/nginx.pid

# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/etc/nginx/nginx.conf"

[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

lockfile=/var/lock/nginx.lock


start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    sleep 1
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

(6)授權(quán)
chmod +x /etc/rc.d/init.d/nginx
(7)設(shè)置開機自啟動
chkconfig --add nginx chkconfig nginx on
(8)啟動nginx
service nginx start

三.安裝mysql-5.6.30
(1) 下載mysql(建議安裝mysql二進(jìn)制包,源碼包安裝很慢)
cd /usr/local/src
wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.30.tar.gz
(2) 安裝編譯源碼所需的工具和庫
yum -y install cmake ncurses-devel
(3) 設(shè)置MySQL用戶和組
useradd -r mysql
(4) 編譯安裝
cd /usr/local/src
tar xf mysql-5.6.30.tar.gz
cd mysql-5.6.30/
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DMYSQL_DATADIR=/data/mysqldb -DMYSQL_TCP_PORT=3306 -DENABLE_DOWNLOADS=1 -DEXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=1 -DWITH_READLINE=1
(注意是使用cmake)
make -j 4 && make install
(5) 初始化數(shù)據(jù)庫
mkdir -p /data/mysqldb #創(chuàng)建數(shù)據(jù)庫數(shù)據(jù)目錄
chown -R mysql.mysql /usr/local/mysql # 給程序目錄授權(quán),否則socket文件無法寫入到該目錄
chown -R mysql.mysql /data/mysqldb/ #給數(shù)據(jù)目錄授權(quán)
cd /usr/local/mysql/
./scripts/mysql_install_db --user=mysql --datadir=/data/mysqldb
參考: /usr/local/mysql/scripts/mysql_install_db --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
(6) 準(zhǔn)備mysql配置文件
cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf (注:如果/etc/my.cnf文件存在,則覆蓋。)

vim /etc/my.cnf
[mysqld]
...
datadir = /data/mysqldb
innodb_file_per_table = ON
skip_name_resolve = ON
...

(7) 復(fù)制mysql啟動腳本
cp support-files/mysql.server /etc/rc.d/init.d/mysqld
sed -i 's@^basedir=@basedir=/usr/local/mysql@' /etc/init.d/mysqld
sed -i 's@^datadir=@datadir=/data/mysqldb@' /etc/init.d/mysqld
(8) 啟動mysql服務(wù)并設(shè)置開機自啟動
service mysqld start
chkconfig --add mysqld chkconfig --level 35 mysqld on
(9) 設(shè)置環(huán)境變量
echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh
(10) 輸出mysql的頭文件至系統(tǒng)頭文件路徑/usr/include(非必須)
ln -s /usr/local/mysql/include/ /usr/include/mysql
(11) 輸出mysql的庫文件給系統(tǒng)庫查找路徑(非必須)
echo "/usr/local/mysql/lib/" >/etc/ld.so.conf.d/mysql.conf
ldconfig

四. php安裝
(1) PHP添加libmcrypt拓展
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum -y install libxml2-devel libmcrypt-devel bzip2-devel curl-devel
(2) 解決依賴
yum -y install libxml2-devel libcurl-devel libjpeg-devel libpng-devel freetype freetype-devel php-pear
(3) 編譯安裝php-5.6
tar xf php-5.6.30.tar.bz2
cd php-5.6.30/
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-gd --enable-gd-native-ttf --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-mcrypt=/usr/local/libmcrypt/ --with-gettext
make && make install
(4) 復(fù)制php配置文件
cp php.ini-production /usr/local/php/etc/php.ini
(5) 復(fù)制php-fpm配置文件
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
(6) 復(fù)制php-fpm啟動腳本到init.d,設(shè)置開機啟動
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
cd
chmod +x /etc/rc.d/init.d/php-fpm
chkconfig --add php-fpm chkconfig php-fpm on
(7) 啟動php-fpm
service php-fpm start
(8) 修改nginx配置文件使之支持php

vim /etc/nginx/nginx.conf
解除注釋:
location ~ \.php$ {
    root           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 server部分location的注釋,并要注意fastcgi_param行的參數(shù),改為$document_root$fastcgi_script_name,或者使用絕對路徑  
測試一下是否有錯:
/usr/local/nginx/sbin/nginx -t
平滑重啟nginx
/usr/local/nginx/sbin/nginx -s reload

五. 測試

vim /usr/local/nginx/html/index.php
<?php
    phpinfo()
?>

<?php
    \$conn = mysql_connect('127.0.0.1','root','123456');
    if (\$conn)
       echo "OK";
    else
       echo "Failure";
?>

瀏覽器測試即可

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容