作爲一個輕量級的HTTP服務器,Nginx與Apache相比,小巧而精緻:在性能上,它佔用很少的系統資源,能支持更多的併發連接,達到更高的訪問效率;在功能上,Nginx是優秀的代理服務器和負載均衡服務器;在安裝配置上,Nginx安裝簡單、配置靈活。LNMP環境是指Linux下搭建Nginx+MySQL+PHP.
Linux下Web服務器架構之
源碼構建LNMP環境
1.構建的linux環境:
Linux junjie 2.6.25.19 #1 SMP Mon Feb 20 17:25:04 CST 2012 i686 i686 i386 GNU/Linux
2.準備工作:
準備一:關閉firewall和SELinux(略)
準備二:構建本地yum服務器(略)
準備三:下載以下軟件(我存放在linux的/root/lamp下)
下載地址:
nginx : http://nginx.org/
準備四:構建編譯組環境
使用# yum grouplist all |less查看已安裝的組環境(紅色必選)
[root@junjie ~]# yum grouplist all |less
Legacy Software Development
X Software Development
Development Libraries
Development Tools
發現這裏沒有安裝Development Tools,使用下面命令安裝
[root@www ~]# yum groupinstall "Development Tools"
#安裝依賴包:
#nginx會有幾個依賴包,我們首先安裝依賴包,若安裝過程中報錯,可以跳過,不會影#響nginx的正常運行:
[root@junjie ~]# yum -y install zlib-devel pcre-devel openssl-devel
源碼構建LNMP環境
1.源碼安裝nginx:
[root@junjie lnmp]# ldconfig -v |grep libevent
libevent-1.1a.so.1 -> libevent-1.1a.so.1.0.2
[root@junjie lnmp]# tar -zxvf libevent-2.0.18-stable.tar.gz -C /usr/src/
[root@junjie src]# cd libevent-2.0.18-stable/
[root@junjie libevent-2.0.18-stable]# ls
[root@junjie libevent-2.0.18-stable]# vim /etc/ld.so.conf.d/libevent.conf
[root@junjie libevent-2.0.18-stable]# ldconfig -v |grep libevent
libevent_pthreads-2.0.so.5 -> libevent_pthreads.so
libevent_extra-2.0.so.5 -> libevent_extra.so
libevent_openssl-2.0.so.5 -> libevent_openssl.so
libevent_core-2.0.so.5 -> libevent_core.so
libevent-1.1a.so.1 -> libevent-1.1a.so.1.0.2
安裝nginx:
[root@junjie libevent-2.0.18-stable]# cd /root/lnmp
[root@junjie lnmp]# tar -zxvf nginx-1.1.18.tar.gz -C /usr/src/
[root@junjie lnmp]# cd /usr/src/nginx-1.1.18/
[root@junjie nginx-1.1.18]# groupadd -r nginx
[root@junjie nginx-1.1.18]# useradd -r -g nginx -s /bin/false -M nginx
> --prefix=/usr \
> --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 \
> --lock-path=/var/lock/nginx.lock \
> --user=nginx \
> --group=nginx \
> --with-http_ssl_module \
> --with-http_flv_module \
> --with-http_stub_status_module \
> --with-http_gzip_static_module \
> --http-client-body-temp-path=/var/tmp/nginx/client/ \
> --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
> --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
> --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
> --http-scgi-temp-path=/var/tmp/nginx/scgi \
##pkill -9 nignx #關閉nginx
##/usr/sbin/nginx #啓動nginx
nginx: [emerg] mkdir() "/var/tmp/nginx/client/" failed (2: No such file or directory)
[root@junjie nginx-1.1.18]# mkdir -pv /var/tmp/nginx/client
mkdir: created directory `/var/tmp/nginx'
mkdir: created directory `/var/tmp/nginx/client'
[root@junjie nginx-1.1.18]# netstat -tupln |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 16561/nginx: master
[root@junjie nginx-1.1.18]# vim /etc/rc.d/init.d/nginx (請下載附件)
[root@junjie nginx-1.1.18]# chmod +x /etc/rc.d/init.d/nginx
[root@junjie nginx-1.1.18]# chkconfig --add nginx
[root@junjie nginx-1.1.18]# chkconfig nginx on
[root@junjie nginx-1.1.18]# chkconfig --list nginx
[root@junjie nginx-1.1.18]# service nginx stop
Stopping nginx: [ OK ]
[root@junjie nginx-1.1.18]# service nginx start
Starting nginx: [ OK ]
[root@junjie nginx-1.1.18]# service nginx restart
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
[root@junjie nginx-1.1.18]# ls /usr/html/
[root@junjie nginx-1.1.18]# vim /usr/html/index.html
5 <body bgcolor="white" text="black">
6 <center><h1>Welcome to my nginx!</h1></center>
2.源碼安裝mysql:
#解壓mysql的主程序文件,使用tar –zxvf,並使用-C指定解壓目錄
[root@junjie lnmp]# tar -zxvf mysql-5.0.95.tar.gz -C /usr/src/
[root@junjie src]# ln -s mysql-5.0.95/ mysql
[root@junjie mysql]# yum -y install ncurses-devel
[root@junjie mysql]# useradd -M -s /sbin/nologin mysql
[root@junjie mysql]# cp support-files/my-medium.cnf /etc/my.cnf
[root@junjie mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@junjie mysql]# chmod +x /etc/init.d/mysqld
[root@junjie mysql]# ln -s /usr/local/mysql/bin/* /usr/local/bin/
[root@junjie mysql]# ln -s /usr/local/mysql/lib/mysql/lib* /usr/lib/
[root@junjie mysql]# mysql_install_db --user=mysql
[root@junjie mysql]# chown -R root.mysql /usr/local/mysql/
[root@junjie mysql]# chown -R mysql.mysql /usr/local/mysql/var/
[root@junjie mysql]# service mysqld start
Starting MySQL.. [ OK ]
[root@junjie mysql]# netstat -tupln |grep 3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 14200/mysqld
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
| information_schema |
3 rows in set (0.00 sec)
3.源碼安裝PHP依賴程序包:
#安裝PHP前首先要安裝幾個源碼包依賴:libmcrypt mhash mcrypt
[root@junjie php]# tar -jxvf libmcrypt-2.5.8.tar.bz2 -C /usr/src/
[root@junjie php]# cd /usr/src/libmcrypt-2.5.8/
[root@junjie libmcrypt-2.5.8]# make install
[root@junjie php]# tar -jxvf mhash-0.9.9.9.tar.bz2 -C /usr/src/
[root@junjie php]# cd /usr/src/mhash-0.9.9.9/
[root@junjie mhash-0.9.9.9]# ./configure
[root@junjie mhash-0.9.9.9]# make install
[root@junjie mhash-0.9.9.9]# ln -s /usr/local/lib/libmhash.* /usr/lib/
[root@junjie mhash-0.9.9.9]# ln -s /usr/local/lib/libmcrypt* /usr/lib
[root@junjie php]# tar -zxvf mcrypt-2.6.8.tar.gz -C /usr/src/
[root@junjie php]# cd /usr/src/mcrypt-2.6.8/
[root@junjie mcrypt-2.6.8]# make install
[root@junjie mcrypt-2.6.8]#yum –y install libxml2-devel curl-devel libpng-devel openldap-devel
4.源碼安裝PHP:
[root@junjie mcrypt-2.6.8]# cd /root/lnmp/
#解壓mysql的主程序文件,使用tar –jxvf,並使用-C指定解壓目錄:
[root@junjie lnmp]# tar -jxvf php-5.4.0.tar.bz2 -C /usr/src/
[root@junjie lnmp]# cd /usr/src/php-5.4.0/
[root@junjie php-5.4.0]#./configure --prefix=/usr/local/php --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-fpm --enable-fastcgi --with-mcrypt --with-gd --with-openssl --with-mhash --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc -enable-zip --enable-soap
5.使能nginx調度phphemysql:
[root@junjie php-5.4.0]# cp php.ini-production /usr/local/php/php.ini
[root@junjie php-5.4.0]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@junjie php-5.4.0]# ln -s /usr/local/php/bin/php /usr/bin/
[root@junjie php-5.4.0]# vim /usr/local/php/etc/php-fpm.conf
143 listen = /var/run/php-fpm/php-fpm.sock
[root@junjie php-5.4.0]# mkdir /var/run/php-fpm
[root@junjie sbin]# cd /usr/src/php-5.4.0/
[root@junjie php-5.4.0]# vim /etc/nginx/nginx.conf
#修改44行內容,增加index.php,添加index.php的首頁文件選項
#添加46—52行
49 fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
[root@junjie php-5.4.0]# service nginx restart
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
6.測試LNMP環境:
#測試nginx與PHP的連接:#測試成功
#測試nginx與mysql的連接:#測試成功
[root@junjie html]# vim /usr/html/index.php
echo "scuess";
echo "fail";
Shutting down MySQL.. [ OK ]
《完》
至此源碼構建LNMP環境成功!!!
附件:nginx啓動腳本:
#nginx啓動腳本
#vim /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