用源代碼搭建lnmp環境

 

Lnmp簡介
LNMP代表的就是:Linux系統下Nginx+MySQL+PHP這種網站服務器架構。
Nginx是一個小巧而高效的Linux下的Web服務器軟件,Nginx性能穩定、功能豐富、運維簡單、處理靜態文件速度快且消耗系統資源極少。
作爲 Web 服務器:相比 Apache,Nginx 使用更少的資源,支持更多的併發連接,體現更高的效率。
作爲負載均衡服務器:Nginx 既可以在內部直接支持 Rails 和 PHP,也可以支持作爲 HTTP代理服務器對外進行服務。Nginx 用C編寫,不論是系統資源開銷還是CPU使用效率都比Perlbal要好的多。   
作爲郵件代理服務器:Nginx同時也是一個非常優秀的郵件代理服務器(最早開發這個產品的目的之一也是作爲郵件代理服務器),Last/fm 描述了成功並且美妙的使用經驗。
Nginx 安裝非常的簡單,配置文件非常簡潔(還能夠支持perl語法)。Nginx支持平滑加載新的配置,還能夠在不間斷服務的情況下進行軟件版本的升級。
實驗環境
 
  1. [root@localhost ~]# uname -r  
  2. 2.6.18-164.el5  
  3. [root@localhost ~]# cat /etc/redhat-release   
  4. Red Hat Enterprise Linux Server release 5.4 (Tikanga)  
 
一、安裝nginx
1.解決依賴關係
編譯安裝nginx需要事先需要安裝開發包組"Development Tools"和 "Development Libraries"。同時,還需要專門安裝pcre-devel包:
[root@localhost ~]# yum grouplist |less

[root@localhost ~]# yum list all |grep pcre
This system is not registered with RHN.
RHN support will be disabled.
pcre.i386                              6.6-2.el5_1.7         installed         
pcre-devel.i386                        6.6-2.el5_1.7         rhel-server 
[root@localhost ~]# yum install pcre-devel –y  ##安裝pcre-devel
 
2、解壓縮
[root@localhost ~]# tar -zxvf nginx-1.0.11.tar.gz -C /usr/local/src
3.首先添加用戶nginx,實現以之運行nginx服務進程
[root@localhost ~]# groupadd -r nginx
[root@localhost ~]# useradd -r -g nginx -s /bin/false -M nginx
4.編譯和安裝
 [root@localhost ~]# cd /usr/local/src/nginx-1.0.11/
[root@localhost nginx-1.0.11]# ./configure \
 --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 \
 --with-pcre
[root@localhost nginx-1.0.11]# make && make install
 
5.nginx提供sysv服務腳本
[root@localhost ~]# vim /etc/rc.d/init.d/nginx
 
  1. #!/bin/sh  
  2. #  
  3. # nginx - this script starts and stops the nginx daemon  
  4. #  
  5. # chkconfig:   - 85 15   
  6. # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \  
  7. #               proxy and IMAP/POP3 proxy server  
  8. # processname: nginx  
  9. # config:      /etc/nginx/nginx.conf  
  10. # config:      /etc/sysconfig/nginx  
  11. # pidfile:     /var/run/nginx.pid  
  12.    
  13. # Source function library.  
  14. . /etc/rc.d/init.d/functions  
  15.    
  16. # Source networking configuration.  
  17. . /etc/sysconfig/network  
  18.    
  19. # Check that networking is up.  
  20. "$NETWORKING" = "no" ] && exit 0  
  21.    
  22. nginx="/usr/sbin/nginx" 
  23. prog=$(basename $nginx)  
  24.    
  25. NGINX_CONF_FILE="/etc/nginx/nginx.conf" 
  26.    
  27. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx  
  28.    
  29. lockfile=/var/lock/subsys/nginx  
  30.    
  31. make_dirs() {  
  32.    # make required directories  
  33.    user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`  
  34.    options=`$nginx -V 2>&1 | grep 'configure arguments:'`  
  35.    for opt in $options; do 
  36.        if [ `echo $opt | grep '.*-temp-path'` ]; then  
  37.            value=`echo $opt | cut -d "=" -f 2`  
  38.            if [ ! -d "$value" ]; then  
  39.                # echo "creating" $value  
  40.                mkdir -p $value && chown -R $user $value  
  41.            fi  
  42.        fi  
  43.    done  
  44. }  
  45.    
  46. start() {  
  47.     [ -x $nginx ] || exit 5  
  48.     [ -f $NGINX_CONF_FILE ] || exit 6  
  49.     make_dirs  
  50.     echo -n $"Starting $prog: " 
  51.     daemon $nginx -c $NGINX_CONF_FILE  
  52.     retval=$?  
  53.     echo  
  54.     [ $retval -eq 0 ] && touch $lockfile  
  55.     return $retval  
  56. }  
  57.    
  58. stop() {  
  59.     echo -n $"Stopping $prog: " 
  60.     killproc $prog -QUIT  
  61.     retval=$?  
  62.     echo  
  63.     [ $retval -eq 0 ] && rm -f $lockfile  
  64.     return $retval  
  65. }  
  66.    
  67. restart() {  
  68.     configtest || return $?  
  69.     stop  
  70.     sleep 1  
  71.     start  
  72. }  
  73.    
  74. reload() {  
  75.     configtest || return $?  
  76.     echo -n $"Reloading $prog: " 
  77.     killproc $nginx -HUP  
  78.     RETVAL=$?  
  79.     echo  
  80. }  
  81.    
  82. force_reload() {  
  83.     restart  
  84. }  
  85.    
  86. configtest() {  
  87.   $nginx -t -c $NGINX_CONF_FILE  
  88. }  
  89.    
  90. rh_status() {  
  91.     status $prog  
  92. }  
  93.    
  94. rh_status_q() {  
  95.     rh_status >/dev/null 2>&1  
  96. }  
  97.    
  98. case "$1" in 
  99.     start)  
  100.         rh_status_q && exit 0  
  101.         $1  
  102.         ;;  
  103.     stop)  
  104.         rh_status_q || exit 0  
  105.         $1  
  106.         ;;  
  107.     restart|configtest)  
  108.         $1  
  109.         ;;  
  110.     reload)  
  111.         rh_status_q || exit 7  
  112.         $1  
  113.         ;;  
  114.     force-reload)  
  115.         force_reload  
  116.         ;;  
  117.     status)  
  118.         rh_status  
  119.         ;;  
  120.     condrestart|try-restart)  
  121.         rh_status_q || exit 0  
  122.             ;;  
  123.     *)  
  124.         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
  125.         exit 2  
  126. esac 
6.添加至服務列表
[root@localhost ~]# chmod +x /etc/rc.d/init.d/nginx  
[root@localhost ~]# chkconfig --add nginx     
[root@localhost ~]# chkconfig nginx on   ##設置開機啓動
[root@localhost ~]# service nginx start ##啓動
7.查看端口
[root@localhost ~]# netstat -tupln |grep nginx
tcp   0   0 0.0.0.0:80    0.0.0.0:*       LISTEN      5712/nginx.conf    
##可以看見,80端口已經開啓
8.nginx的測試
 

 
二、安裝mysql
1.解壓縮mysql-5.5.15-linux2.6-i686
[root@localhost ~]# tar zxvf mysql-5.5.15-linux2.6-i686.tar.gz -C /usr/local/
2.新建用戶以安全方式運行進程
[root@localhost ~]# groupadd -r mysql
[root@localhost ~]# useradd -g mysql -r -s /sbin/nologin -M -d /mydat/data mysql
[root@localhost ~]# mkdir -pv /mydata/data
[root@localhost ~]# chown -R mysql:mysql /mydata/data
3.安裝並初始化數據庫
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ln -sv mysql-5.5.15-linux2.6-i686 mysql
##建立一個軟連接
[root@localhost local]# cd mysql
[root@localhost mysql]# chown -R mysql:mysql  .
[root@localhost mysql]# scripts/mysql_install_db --user=mysql
--datadir=/mydata/data/
##--user定義數據庫的所屬主,--datadir定義數據庫安裝到哪裏
##./bin/mysqladmin -u root password 'new-password'
##./bin/mysqladmin -u root -h localhost.localdomain password 'new-password'
##Alternatively you can run:
##./bin/mysql_secure_installation
[root@localhost mysql]# chown -R root .
4.mysql拷貝配置文件
[root@localhost ~]# cd /usr/local/mysql
[root@localhost mysql]# cp support-files/my-large.cnf /etc/my.cnf
[root@localhost mysql]# vim /etc/my.cnf
38 # Try number of CPU's*2 for thread_concurrency
39 thread_concurrency = 2
##thread_concurrency的值爲你的CPU個數的2倍
40 datadir = /mydata/data
##mysql數據文件的存放位置
5.mysql提供sysv服務腳本
[root@localhost mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
6.添加至服務列表
[root@localhost mysql]# chkconfig --add mysqld
[root@localhost mysql]# service mysqld start
Starting MySQL.................                            [ OK ]
[root@localhost mysql]# chkconfig mysqld on
##設置開機啓動
 
 
三、安裝php
1.解壓縮php-5.3.7
[root@localhost ~]# tar -jxvf php-5.3.7.tar.bz2 -C /usr/local/src
2.編譯並安裝
[root@localhost ~]# cd /usr/local/src/php-5.3.7/
[root@localhost php-5.3.7]# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl  --enable-fpm  --enable-sockets  --enable-sysvshm 
--with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring  --with-freetype-dir
--with-jpeg-dir --with-png-dir --with-zlib-dir --with-libxml-dir=/usr --enable-xml
--with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl
[root@localhost php-5.3.7]# make && make test && make install
3.php提供配置文件
[root@localhost php-5.3.7]# cp php.ini-production /etc/php.ini
4.php-fpm提供Sysv init腳本,並將其添加至服務列表
[root@localhost php-5.3.7]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[root@localhost php-5.3.7]# chmod +x /etc/rc.d/init.d/php-fpm
[root@localhost php-5.3.7]# chkconfig --add php-fpm
[root@localhost php-5.3.7]# chkconfig php-fpm on
5.php-fpm提供配置文件並編輯配置文件
[root@localhost]# cp /usr/local/php/etc/php-fpm.conf.default
/usr/local/php/etc/php-fpm.conf
[root@localhost]# vim /usr/local/php/etc/php-fpm.conf
修改的行如下(配置pm.的相關選項爲你所需要的值,並啓用pid文件)
25 pid = /usr/local/php/var/run/php-fpm.pid
161 pm.max_children = 50
166 pm.start_servers = 20
171 pm.min_spare_servers = 5
176 pm.max_spare_servers = 35
6.接下來就可以啓動php-fpm了
[root@localhost]# service php-fpm start
使用如下命令來驗正(如果此命令輸出有中幾個php-fpm進程就說明啓動成功了):
[root@localhost]# ps aux | grep php-fpm
 
 
四、整合nginx和php
1.編輯nginx配置文件,配置如下
[root@localhost ~]# vim /etc/nginx/nginx.conf
43         location / {
 44             root   html;
 45             index index.php index.html index.htm;
 46         }
 
65         location ~ \.php$ {
 66             root           html;
 67             fastcgi_pass   127.0.0.1:9000;
 68             fastcgi_index index.php;
 69             fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
 70             include        fastcgi_params;
 71         }
2.編輯fastcgi_params,內容如下
[root@localhost ~]# vim /etc/nginx/fastcgi_params
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE    nginx;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param REQUEST_METHOD     $request_method;
fastcgi_param CONTENT_TYPE       $content_type;
fastcgi_param CONTENT_LENGTH     $content_length;
fastcgi_param SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param REQUEST_URI        $request_uri;
fastcgi_param DOCUMENT_URI       $document_uri;
fastcgi_param DOCUMENT_ROOT      $document_root;
fastcgi_param SERVER_PROTOCOL    $server_protocol;
fastcgi_param REMOTE_ADDR        $remote_addr;
fastcgi_param REMOTE_PORT        $remote_port;
fastcgi_param SERVER_ADDR        $server_addr;
fastcgi_param SERVER_PORT        $server_port;
fastcgi_param SERVER_NAME        $server_name;
 
 
五、測試
[root@localhost ~]# cd /usr/html/               
[root@localhost html]# mv index.html index.php
[root@localhost html]# vim index.php
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx!</h1></center>
</body>
</html>
<?php
phpinfo();
  ?> 
 

[root@localhost html]# vim index.php
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx!</h1></center>
</body>
</html>
<?php
$link=mysql_connect('127.0.0.1','root','');
if($link)
echo "hello ,it is ok !";
else "sorry ,it is fail !";
?>
 

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