源碼構建LNMP (Linux+Nginx+MySQL+PHP)


LNMP 簡介:

Linux是一類Unix計算機操作系統的統稱,是目前最流行的免費操作系統。代表版本有:debian、centos、ubuntu、fedora、gentoo等。
Nginx是一個高性能的HTTP和反向代理服務器,也是一個IMAP/POP3/SMTP代理服務器。
Mysql是一個小型關係型數據庫管理系統。
Php是一種 HTML 內嵌式的語言,是一種在服務器端執行的嵌入HTML文檔的腳本語言。
這四種軟件均爲免費 作爲 Web 服務器:相比 Apache,Nginx 使用更少的資源,支持更多的併發連接,體現更高的效率。作爲負載均衡服務器:Nginx 既可以在內部直接支持 Rails 和 PHP,也可以支持作爲 HTTP代理服務器對外進行服務。Nginx 用 C 編寫, 不論是系統資源開銷還是 CPU 使用效率都比 Perlbal 要好的多。作爲郵件代理服務器: Nginx 同時也是一個非常優秀的郵件代理服務器(最早開發這個產品的目的之一也是作爲郵件代理服務器),Last/fm 描述了成功並且美妙的使用經驗。Nginx對靜態頁面的支持相當出色,輕量且免費。Nginx不支持CGI,但是支持更靈活FastCGI。PHP5.2及之前的版本比較多的是使用 PHP-FPM來管理PHP FastCGI進程。PHP-FPM使用給PHP源碼打補丁後編譯的方式讓新手多少有些難上手,但從PHP 5.3.2開始內置PHP-FPM,只需編譯PHP時啓用PHP-FPM。Nginx 安裝非常的簡單,配置文件 非常簡潔(還能夠支持perl語法),Bugs非常少的服務器: Nginx 啓動特別容易,並且幾乎可以做到7*24不間斷運行,即使運行數個月也不需要重新啓動。你還能夠在 不間斷服務的情況下進行軟件版本的升級。軟件,組合到一起,成爲一個免費、高效的網站服務系統PHP,是英文超級文本預處理語言Hypertext Preprocessor的縮寫。PHP 是一種 HTML 內嵌式的語言,是一種在服務器端執行的嵌入HTML文檔的腳本語言,語言的風格有類似於C語言,被廣泛的運用。

環境:

Redhat 2.6.18-164.el5

nginx-1.1.18.tar.gz

mysql-5.0.95.tar.gz

php-5.4.0.tar.gz

nginx

安裝編譯環境

yum –y install gcc gcc-c++
Development Libraries
Development Tools
Legacy Software Development
X Software Development

必要的庫文件:

yum install pcre-devel (pcre 是一個正則表達式相關的包,支持nginx地址重寫)  install zlib-devel  openssl-devel -y
tar -zxvf libevent-2.0.16-stable.tar.gz -C /usr/src
./configure   
make && ake install

vim /etc/ld.so.conf.d/libevent.conf

添加: /usr/local/lib/

ldconfig -v |grep libevent 確定是否調用成功


tar -zxvf nginx-1.0.11 -C /usr/src

cd /usr/src/nginx-1.0.11

添加系統賬戶,用來運行nginx

groupadd -r nginx
useradd -r -g nginx -s /bin/false -M nginx   無家目錄

配置:

./configure \
--prefix=/usr/local \安裝路徑
--sbin-path=/usr/sbin/nginx \服務命令路徑
--conf-path=/etc/nginx/nginx.conf \配置文件路徑
--error-log-path=/var/log/nginx/error.log \錯誤日誌路徑
--http-log-path=/var/log/nginx/access.log \訪問日誌路徑
--pid-path=/var/run/nginx/nginx.pid \pid路徑
--lock-path=/var/lock/nginx.lock \鎖定文件路徑
--user=nginx \運行用戶,就是剛纔建立的那個
--group=nginx \組
--with-http_ssl_module \ssl模塊
--with-http_flv_module \流媒體模塊
--with-http_stub_status_module \獲取Nginx自上次啓動以來的工作狀態
--with-http_gzip_static_module \壓縮
--http-client-body-temp-path=/var/tmp/nginx/client/ \
    (mkdir -pv /var/tmp/nginx/client創建多級子目錄 )
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
--with-pcre

編譯安裝:

make && make install


nginx的啓動腳本

下面這個腳本來自網絡,測試可以使用:

cat /etc/rc.d/init.d/nginx

#!/bin/sh
#
# 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
# config:      /etc/sysconfig/nginx
# 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/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
   # make required directories
   user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    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


這個函數的作用是測試修改過的Nginx的配置文件是否有問題

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

正常:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

如果腳本里沒有,可以手動測一下:

/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf


賦予腳本執行權限

chmod +x /etc/rc.d/init.d/nginx

加入服務,開機啓動

chkconfig --add nginx
chkconfig nginx on

重啓一下,看是否正常:

service nginx restart

image

查看是否啓動成功

netstat -tupln |grep nginx

訪問會出現如下頁面:

image


mysql

yum -y install ncurses-devel

tar -zxvf mysql-5.0.95.tar.gz -C /usr/src

添加系統賬號mysql:不允許登陸,無家目錄

useradd -M -s /sbin/nologin mysql

cd /usr/src/mysql-5.0.95

配置

./configure --prefix=/usr/local/mysql \
--without-debug \
--with-extra-charsets=utf8,gbk \中文支持
--enable-assembler \彙編模式
--with-mysqld-ldflags=-all-static \靜態方式
--with-client-ldflags=-all-static \
--with-unix-socket-path=/tmp/mysql.sock \使用unix socket
--with-ssl

編譯安裝

make && make install

複製配置文件

cp support-files/my-large.cnf /etc/my.cnf  可根據內存選擇large,medium之類

copy啓動腳本

cp support-files/mysql.server /etc/rc.d/init.d/mysqld

賦予腳本執行權限

chmod +x /etc/rc.d/init.d/mysqld

可執行文件和動態鏈接庫文件的軟連接:

ln -s /usr/local/mysql/bin/* /usr/local/bin/
ln -s /usr/local/mysql/lib/mysql/lib* /usr/lib/

強制以mysql身份初始化數據庫

mysql_install_db --user=mysql

安裝目錄權限,注意這兩條命令優先順序:

chown -R root.mysql /usr/local/mysql/
chown -R mysql.mysql /usr/local/mysql/var/


確保啓動成功

服務項

chkconfig --add mysqld

開機啓動項

chkconfig mysqld on
service mysqld start

netstat -tupln |grep mysql


php

apache是把php作爲自己的模塊來調用的,而Nginx+PHP需要PHP生成可執行文件纔可以,nginx支持FastCGI技術,PHP-FPM是FastCGI的管理器,用來管理PHP FastCGI進程。作爲PHP的插件存在。PHP 5.3.2開始內置PHP-FPM,只需編譯PHP時啓用PHP-FPM

安裝庫文件,依賴的軟件包:

yum –y install libxml2-devel curl-devel libpng-devel openldap-devel
tar -jxvf libmcrypt-2.5.8.tar.bz2
cd libmcrypt-2.5.8  ./configure  make && make install
tar -jxvf mhash-0.9.9.9.tar.bz2
cd mhash-0.9.9.9  ./configure make && make install


ln -s /usr/local/lib/libmcrypt* /usr/lib
ln -s /usr/local/lib/libmhash.* /usr/lib/


tar -zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8 ./configure make && make install

image

解壓縮:

tar -zxvf php-5.4.0.tar.gz -C /usr/local

cd/usr/src/ php-5.4.0

配置

./configure \
--prefix=/usr/local/php \
--with-openssl \
--enable-fpm \
--with-mysql=/usr/local/mysql/ \
--with-zlib \
--enable-xml \
--disable-rpath \
--enable-safe-mode \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--with-curl \
--with-curlwrappers \
--enable-fastcgi \
--with-mcrypt \
--with-gd \
--with-mhash \
--enable-sockets \
--with-ldap \
--with-ldap-sasl \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-config-file-path=/etc/php \
--enable-mbstring=all \
--with-mysqli=/usr/local/mysql/bin/mysql_config

編譯安裝

make && make install

配置文件

cp php.ini-production /usr/local/php/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
ln -s /usr/local/php/bin/php /usr/bin/



做nginx和php的關聯,--enable-sockets,默認是通過9000端口把動態頁面交給php處理的


vim /usr/local/php/etc/php-fpm.conf

將127.0.0.1:9000改爲如下:

listen = /var/run/php-fpm/php-fpm.sock

mkdir /var/run/php-fpm



添加index.php

vim /etc/nginx/nginx.conf

index  index.php index.html index.htm;



打開如下幾行,並做修改

location ~ \.php$ {
fastcgi_pass        unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index       index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
include fastcgi_params;
include fastcgi.conf;
}

啓動:

/usr/local/php/sbin/php-fpm start

重啓nginx

service nginx restart

創建php頁面

cd /usr/html/     cp index.html index.php

vim index.php 添加:<?php phpinfo(); ?>

image


訪問:http://192.168.101.63

image



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章