Chinaunix首页 | 论坛 | 博客
  • 博客访问: 955323
  • 博文数量: 245
  • 博客积分: 11429
  • 博客等级: 上将
  • 技术积分: 2662
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-15 00:16
文章存档

2011年(56)

2010年(174)

2009年(15)

分类: LINUX

2010-08-17 16:58:47

       ubuntu 下nginx+mysql+php 安装详解              

 作者:pkfeiyang         博客主页:http://blog.chinaunix.net/u3/103362/article_119529.html

                            

1、  项目名称…………………………………………………………………………………………………………………………3

2、  项目说明…………………………………………………………………………………………………………………........4

2.1软硬件准备……………………………………………………………………………………………………………….4

  3、工程概要………………………………………………………………………………………………………………………….5

     3.1实现目的…………………………………………………………………………………………………………………….5

     3.2实施计划……………………………………………………………………………………………………………………..5

     3.3实施拓扑图…………………………………………………………………………………………………………………5

  4、实施步骤……………………………………………………………………………………………………………………………6

      4.1环境准备……………………………………………………………………………………………………………………7

      4.2安装nginx………………………………………………………………………………………………………………….7

      4.3安装mysql………………………………………………………………………………………………………………….8

      4.4安装php库及扩展模块…………………………………………………………………………………………….9

      4.5编辑配置文件…………………………………………………………………………………………………………10

      4.6启动服务…………………………………………………………………………………………………………………11

  5、测试…………………………………………………………………………………………………………………………………12

  6.常见错误解析…………………………………………………………………………………………………………………..13

      附录……………………………………………………………………………………………………………………………..14

        常用命令…………………………………………………………………………………………………………………..14

 

 

 

 

 

 

1、          项目名称

   lnmp的搭建

2、        项目说明

      此项目是web基于linux环境下,采用目前较流行nginx,与mysqlphp,进       量级,承受压力大的新型网站web服务

    nginx是一款轻量级的以及电子邮件(IMAP/POP3)代理服务器,并在协议下发行。软件由的程式设计师Igor Sysoev所开发,最初供俄国大型的入口网站及搜寻引擎Рамблер)使用。

其特点是占有少,并发能力强。

目前中国大陆使用nginx网站用户有:。另外知名的也使用nginx

 

     2.1软件环境:

        

软件名称

说明

mysql-5.0.22

 

nginx-0.8.29

 

php5.2.11

 

libevent1.4.13

 

php-fpm-0.65.2.11

 

eaccelerator-0.9.5.3.tar.bz2

 

第三方开发包

 

   2.2硬件环境

      

硬件名称

说明

操作系统

Ubuntu8.04.3

主机名

my.dd

Ip地址

192.168.0.86

 

3、        工程概况

3.1实现目的

    打造轻型web服务器,加强网站的承受能力。

 3.2实施计划

   

步骤

工作描述及目的

工作时间

1

准备工作,搭建所需环境

 

2

安装web服务

 

3

测试

 

 

        3.3拓扑图

          

4、        实施步骤

         4.1环境准备

           安装第三方开发包

        4.2ginx安装

            

apt-get install libpcre3 libpcre3-dev g++

安装nginx

tar zcf nginx-0.8.29.tar.gz

cd nginx-0.8.29

./configure  --prefix=/usr/local/nginx  --user=www --group=www --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module --with-http_stub_status_module

make && make install

  4.3ql5.1.40 server

a) 安装编译mysql需要使用的库

apt-get install libncurses5-dev

b) max参数指定安装全部存储引擎,包括innodbcluster(集群)

c) 编译安装

./configure --prefix=/usr/local/mysql --with-mysqld-user=mysql --with-extra-charsets=all --with-unix-socket-path=/usr/local/mysql/var/mysql.sock --localstatedir=/var/mysql/local --sharedstatedir=/var/mysql/share --with-pthread --enable-thread-safe-client --with-plugins=partition,archive,blackhole,csv,federated,heap,innobase,innodb_plugin,myisam,myisammrg,ndbcluster

make && make install

d) 配置文件

mkdir -p /var/mysql/share

mkdir -p /var/mysql/local

#此为全局设置,任选一种

cp /usr/local/mysql/share/mysql/my-huge.cnf /etc/my.cnf

#此为local设置,任选一种

cp /usr/local/mysql/share/mysql/my-huge.cnf /var/mysql/local/my.cnf

           chmod 644 /var/mysql/local/my.cnf

e) 安装系统数据库文件

/usr/local/mysql/bin/mysql_install_db

useradd mysql

chmod 755 /var/mysql/local -R

chown mysql:root /var/mysql -R

chown mysql:root /usr/local/mysql -R

f) 设置mysql自动启动

cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/

update-rc.d mysql.server defaults

g) 启动mysql

/usr/local/mysql/share/mysql/mysql.server start

h) 设置root用户本地localhost访问的密码,不设置默认为空

/usr/local/mysql/bin/mysqladmin -u root password cehrs.com

i) 增加root从任意机器操作数据库的所有权限,仅方便开发测试,不允许发布环境使用

/usr/local/mysql/bin/mysql -u root

GRANT ALL ON *.* TO 'root'@'%' IDENTIFIED BY '123456';

 

4.4hp安装

  

A)的第三方库

tall libxml2 xml2 xml-core  libpng3   libtiff4 libfreetype6   mcrypt libmcrypt4 libgd2-xpm libjpeg62  curl   libmhash2   libxslt1.1   libltdl3  libgmp3c2 libc-client2007 libevent1

C)php-fpm所需第三方类库

export LE_VER=1.4.13-stable

wget "~provos/libevent-$LE_VER.tar.gz"

tar -zxvf "libevent-$LE_VER.tar.gz"

cd "libevent-$LE_VER"

./configure && make

DESTDIR=$PWD make install

D)fpmphpfastcgi 管理器)补丁

export PHP_VER=5.2.11

wget "~$PHP_VER.tar.gz"

tar -zxvf "php-fpm-0.6~$PHP_VER.tar.gz"

php-fpm-0.6-$PHP_VER/generate-fpm-patc

 

E)php所需的mysql客户端类库

cp /usr/local/mysql/lib/libmysqlclient* /lib/

F)phpfastcgi模式,fpm补丁)

tar zxf php-5.2.11.tar.gz

cd php-5.2.11

patch -p1 < ../fpm.patch

./buildconf --force

./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/php5  --with-fpm --with-fpm-conf=/usr/local/php5/php-fpm.conf --with-libevent

make && make install

cp php.ini-recommended /usr/local/php5/php.ini

mkdir -p /usr/local/php5/ext

cd /usr/local/php-5.11/ext/curl (php的源码中执行下面命令)

/usr/local/php5/bin/phpize

./configure --with-php-config=/usr/local/php5/bin/php-config  --with-curl

make && make install

mv /usr/local/php5/lib/php/extensions/no-debug-non-zts-20060613/* /usr/local/php5/ext

vi /usr/local/php5/php.ini

     ;设置动态库的存储路径

     extension_dir = "/usr/local/php5/ext"

     ;载入curl扩展库的so

     extension=curl.so

下面都要安装curl的安装方式进行:

    生成如下的模块

 

extension=bz2.so

extension=calendar.so

extension=curl.so

extension=exif.so

extension=ftp.so

extension=gd.so

extension=gettext.so

extension=gmp.so

extension=imap.so

extension=ldap.so

extension=mbstring.so

extension=mcrypt.so

extension=mhash.so

extension=mysql.so

extension=openssl.so

extension=soap.so

extension=sockets.so

extension=xmlrpc.so

extension=xsl.so

extension=zip.so

extension=zlib.so

 

安装eaccelerator加速器

 

 wget

 

    tar -xjvf eaccelerator-0.9.5.3.tar.bz2

 

  cd eaccelerator-0.9.5.3

 

    export PHP_PREFIX="/usr/local/php5/"

  /usr/local/php5/bin/phpize

  ./configure     --enable-eaccelerator=shared --with-php-config=$PHP_PREFIX/bin/php-config

  make

    make test

make install

 

修改php.ini

   [eAccelerator]

extension=eaccelerator.so

eaccelerator.shm_size=64

eaccelerator.cache_dir="/var/www/tmp"

eaccelerator.enable=1

eaccelerator.optimizer=1

eaccelerator.check_mtime=1

eaccelerator.debug=0

eaccelerator.filter=

eaccelerator.shm_max=0

eaccelerator.shm_ttl=0

eaccelerator.shm_prune_period=0

eaccelerator.shm_only=0

eaccelerator.compress=1

eaccelerator.compress_level=9

创建目录:mkdir /var/www/tmp

           Chmod 0777 /var/www/tmp

4.5编辑配置文件

 

         } Nginx的配置文件

root@my:/usr/local/nginx/conf# cat /usr/local/nginx/conf/nginx.conf

user  www www;          编辑用户

worker_processes 50;

 

pid /var/run/nginx.pid;

# [ debug | info | notice | warn | error | crit ]

#error_log  /dev/null;

error_log  /var/www/logs/nginx_error.log error;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

#Specifies the value for maximum file descriptors that can be opened by this process.

worker_rlimit_nofile 51200;

 

events{

       use epoll;

 

       #maxclient = worker_processes * worker_connections / cpu_number

       worker_connections 5120;

}

 

http{

       include       mime.types;

       default_type  application/octet-stream;

       charset  utf-8;

 

       log_format  main  '$remote_addr - $remote_user [$time_local] $request '

                         '"$status" $body_bytes_sent "$http_referer" '

                         '"$http_user_agent" "$http_x_forwarded_for"';

 

       #access_log  /dev/null;

 

       #General Options

       server_names_hash_bucket_size 128;

       client_header_buffer_size 32k;

       large_client_header_buffers 4 32k;

 

       ignore_invalid_headers   on;

       recursive_error_pages    on;

       server_name_in_redirect off;

     

       sendfile                 on;

 

       #timeouts

       keepalive_timeout 60;

     

       #TCP Options

       tcp_nopush  on;

       tcp_nodelay on;

 

       #fastcgi options

       fastcgi_connect_timeout 300;

       fastcgi_send_timeout 300;

       fastcgi_read_timeout 300;

      

       fastcgi_buffer_size 128k;

       fastcgi_buffers 4 128k;

       fastcgi_busy_buffers_size 128k;

       fastcgi_temp_file_write_size 128k;

       fastcgi_intercept_errors on;

 

       #size limits

       client_max_body_size       50m;

       client_body_buffer_size    256k;

 

       #gzip  compression

       gzip on;

       gzip_min_length  0;

       gzip_buffers     16 8k;

       gzip_http_version 1.0;

       gzip_comp_level 9;

       gzip_types       text/plain text/css image/x-icon image/png;

       gzip_vary on;

 

       #temp files

       proxy_temp_path            /var/www/tmp/proxy_temp;

       fastcgi_temp_path           /var/www/tmp/fastcgi_temp;

       client_body_temp_path       /var/www/tmp/client_body_temp;

       #client_body_temp_path  /usr/local/nginx8/client_body_temp 1 2;

       #proxy_temp_path        /usr/local/nginx8/proxy_temp 1 2;

       #fastcgi_temp_path      /usr/local/nginx8/fastcgi_temp 1 2;

      

       #upstream

       # upstream  bbs.linuxtone.org  {

       #  ip_hash;

       #  server   127.0.0.1:8080;

       #  server   61.164.122.252:8080;

       #  server   220.189.245.68;

       #}

         server {

 

                   root  /var/www/wwwroot;

 

                   server_name     localhost;

                   listen          80;

 

                   access_log  /var/www/logs/localhost.access.log;

 

                   location / {

                            index  index.html index.htm index.php;

                   }

 

                   #error_page  404  /404.html;

 

                   # redirect server error pages to the static page /50x.html

                   #

                   #error_page   500 502 503 504  /50x.html;

                   #location = /50x.html {

                   #       root   /var/www/wwwroot;

                   #}

 

                   # pass the *.php scripts to @php_fpm_bin@ listening on tcp port @php_fpm_port@

                   #

                   include       php5.conf;

         }

 

       # The following includes are specified for virtual hosts

       #

       include  vhosts/.conf;

 

 

cat php5.conf

location  ~ \.php {

        fastcgi_pass  127.0.0.1:9000;

        fastcgi_index index.php;

        include fastcgi_params;

        set $path_info "";

        set $real_script_name $fastcgi_script_name;

        if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {

            set $real_script_name $1;

            set $path_info $2;

        }

        fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;

        fastcgi_param SCRIPT_NAME $real_script_name;

        fastcgi_param PATH_INFO $real_script_name$path_info;

 }

创建域名访问的文件

Mkdir vhosts

编辑域名文件

 

root@my:/usr/local/nginx/conf/vhosts# cat bbs.dd.com.cn.conf

       #bbs.cehrs.com.cn

       server{

                   listen       80;

                   server_name  bbs.dd.com.cn;

                   root  /var/www/wwwroot/bbs;

                   index  index.html index.htm index.php;

                   access_log  /var/www/logs/bbs.dd.com.cn.access.log;

                   #error_page 404 /404.html;

                   #error_page   500 502 503 504  /50x.html;

                   #rewrite ^/bbs/(.*)

                   include       php5.conf ;

}

 

4.6启动服务

启动nginx

/etc/init.d/nginx start

/etc/init.d/php-fpm start

Php-fpm

/etc/init.d/php-fpm start

5、         测试

nginxweb访问路径里编辑文件

  如:

   Vim testPhp

  

输入ip地址  访问

 php不能正常显示时:查看nginx的配置文件,是否和nginx整合

 

 

6、              常见错误解析

 

报错:

This command must be executed one directory ABOVE the

./generate-fpm-patch: 12: php-fpm/generate-fpm-patch: not found

php-fpm source tree, with

cp: cannot copy a directory, `../../.', into itself, `sapi/fpm'

root@my:/usr/local/php-fpm-0.6-5.2.11# php-fpm/generate-fpm-patch

-su: php-fpm/generate-fpm-patch: No such file or directory

 

解决:路径的问题

      从新查看路径,文件位置,从新安排目录即可

 

buildconf: checking installation...

buildconf: autoconf not found.

           You need autoconf version 2.13 or newer installed

           to build PHP from CVS.

make: *** [buildmk.stamp] Error 1

 

 

解决:是缺包

安装autoconf 即可

报错:

Configuring fpm

checking dynamic linker characteristics... GNU/Linux ld.so

checking for libevent >= 1.4.11... no

configure: warning: Could not find libevent 1.4.11.

configure: warning: The use of earlier versions of libevent is not recommended

configure: warning: and can result in unspecified or unsupported behaviour.

checking for minimum libevent version >= 1.4.3... no

configure: warning: Syntax:

configure: warning: --with-libevent=yes|path - link to libevent.a (static library)

configure: warning: --with-libevent=shared,path - link to libevent.so (shared library)

configure: error: Libevent minimum version >= 1.4.3 could not be found.

root@my:/usr/local/php-5.2.11#

 

解决:重新编译libevent 使用如下的configure 

./configure --prefix=/usr/local/  要和php的路径相同

 

报错3

/usr/local/mysql/share/mysql/mysql.server: 94: source: not found

Starting MySQL

/usr/local/mysql/share/mysql/mysql.server: 348: log_success_msg: not found

解决:

错误:

Starting php-fpm Jan 15 19:37:24.592987 [ERROR] fpm_unix_conf_wp(), line 114: cannot get gid for group 'nobody', pool 'default'

 Done

 

解决修改:vim /usr/local/fpm-php5/php-fpm.conf

Unix user of processes

                        nobody/value>

 

                        Unix group of processes

                        nobody

改为,nginx使用的用户

                                 

                        Unix user of processes

                        >www</value>

 

                        Unix group of processes

                        www

 

附录

常用命令及其他

 

1 nginx自启动脚本

vi /etc/init.d/nginx

#!/bin/bash

# nginx Startup script for the Nginx HTTP Server

# this script create it by jackbillow at 2007.10.15.

# it is v.0.0.2 version.

# if you find any errors on this scripts,please contact jackbillow.

# and send mail to jackbillow at gmail dot com.

#

# chkconfig: - 85 15

# description: Nginx is a high-performance web and proxy server.

#              It has a lot of features, but it's not for everyone.

# processname: nginx

# pidfile: /var/run/nginx.pid

# config: /usr/local/nginx8/conf/nginx.conf

nginxd=/usr/local/nginx8/sbin/nginx

nginx_config=/usr/local/nginx8/conf/nginx.conf

nginx_pid=/var/run/nginx.pid

RETVAL=0

prog="nginx daemon"

# Source function library.

. /lib/lsb/init-functions

# Source networking configuration.

#. /etc/sysconfig/network

# Check that networking is up.

#[ ${NETWORKING} = "no" ] && exit 0

[ -x $nginxd ] || exit 0

# Start nginx daemons functions.

start() {

 

if [ -e $nginx_pid ];then

   echo "nginx already running...."

   exit 1

fi

   echo -n $"Starting $prog: "

   $nginxd -c ${nginx_config}

   RETVAL=$?

   echo

}

# Stop nginx daemons functions.

stop() {

        echo -n $"Stopping $prog: "

        $nginxd -s stop

        RETVAL=$?

        echo

}

# reload nginx service functions.

reload() {

    echo -n $"Reloading $prog: "

    #kill -HUP `cat ${nginx_pid}`

    $nginxd -s reload

    RETVAL=$?

    echo

}

# See how we were called.

case "$1" in

start)

        start

        ;;

stop)

        stop

        ;;

reload)

        reload

        ;;

restart)

        stop

        start

        ;;

status)

        status $prog

        RETVAL=$?

        ;;

*)

        echo $"Usage: $prog {start|stop|restart|reload|status|help}"

        exit 1

esac

exit $RETVAL

update-rc.d nginx defaults

 

/etc/init.d/nginx start

/etc/init.d/php-fpm start

 

 

配置文件:

Nginx的配置文件

root@my:/usr/local/nginx/conf# cat /usr/local/nginx/conf/nginx.conf

user  www www;

 

worker_processes 50;

 

pid /var/run/nginx.pid;

# [ debug | info | notice | warn | error | crit ]

#error_log  /dev/null;

error_log  /var/www/logs/nginx_error.log error;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

#Specifies the value for maximum file descriptors that can be opened by this process.

worker_rlimit_nofile 51200;

 

events{

       use epoll;

 

       #maxclient = worker_processes * worker_connections / cpu_number

       worker_connections 5120;

}

 

http{

       include       mime.types;

       default_type  application/octet-stream;

       charset  utf-8;

 

       log_format  main  '$remote_addr - $remote_user [$time_local] $request '

                         '"$status" $body_bytes_sent "$http_referer" '

                         '"$http_user_agent" "$http_x_forwarded_for"';

 

       #access_log  /dev/null;

 

       #General Options

       server_names_hash_bucket_size 128;

       client_header_buffer_size 32k;

       large_client_header_buffers 4 32k;

 

       ignore_invalid_headers   on;

       recursive_error_pages    on;

       server_name_in_redirect off;

     

       sendfile                 on;

 

       #timeouts

       keepalive_timeout 60;

     

       #TCP Options

       tcp_nopush  on;

       tcp_nodelay on;

 

       #fastcgi options

       fastcgi_connect_timeout 300;

       fastcgi_send_timeout 300;

       fastcgi_read_timeout 300;

      

       fastcgi_buffer_size 128k;

       fastcgi_buffers 4 128k;

       fastcgi_busy_buffers_size 128k;

       fastcgi_temp_file_write_size 128k;

       fastcgi_intercept_errors on;

 

       #size limits

       client_max_body_size       50m;

       client_body_buffer_size    256k;

 

       #gzip  compression

       gzip on;

       gzip_min_length  0;

       gzip_buffers     16 8k;

       gzip_http_version 1.0;

       gzip_comp_level 9;

       gzip_types       text/plain text/css image/x-icon image/png;

       gzip_vary on;

 

       #temp files

       proxy_temp_path            /var/www/tmp/proxy_temp;

       fastcgi_temp_path           /var/www/tmp/fastcgi_temp;

       client_body_temp_path       /var/www/tmp/client_body_temp;

       #client_body_temp_path  /usr/local/nginx8/client_body_temp 1 2;

       #proxy_temp_path        /usr/local/nginx8/proxy_temp 1 2;

       #fastcgi_temp_path      /usr/local/nginx8/fastcgi_temp 1 2;

      

       #upstream

       # upstream  bbs.linuxtone.org  {

       #  ip_hash;

       #  server   127.0.0.1:8080;

       #  server   61.164.122.252:8080;

       #  server   220.189.245.68;

       #}

         server {

 

                   root  /var/www/wwwroot;

 

                   server_name     localhost;

                   listen          80;

 

                   access_log  /var/www/logs/localhost.access.log;

 

                   location / {

                            index  index.html index.htm index.php;

                   }

 

                   #error_page  404  /404.html;

 

                   # redirect server error pages to the static page /50x.html

                   #

                   #error_page   500 502 503 504  /50x.html;

                   #location = /50x.html {

                   #       root   /var/www/wwwroot;

                   #}

 

                   # pass the *.php scripts to @php_fpm_bin@ listening on tcp port @php_fpm_port@

                   #

                   include     php5.conf;

         }

 

       # The following includes are specified for virtual hosts

       #

       include        vhosts/.conf;

       include        vhosts/ca.dd.com.cn.conf;

       include        vhosts/res.dd.com.cn.conf;

       include        vhosts/bbs.dd.com.cn.conf;

       include        vhosts/cms.dd.com.cn.conf;

       include  vhosts/crms.dd.com.cn.conf;

 

cat php5.conf

location  ~ \.php {

        fastcgi_pass  127.0.0.1:9000;

        fastcgi_index index.php;

        include fastcgi_params;

        set $path_info "";

        set $real_script_name $fastcgi_script_name;

        if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {

            set $real_script_name $1;

            set $path_info $2;

        }

        fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;

        fastcgi_param SCRIPT_NAME $real_script_name;

        fastcgi_param PATH_INFO $real_script_name$path_info;

 }

创建域名访问的文件

Mkdir vhosts

编辑域名文件

 

root@my:/usr/local/nginx/conf/vhosts# cat bbs.cehrs.com.cn.conf

       #bbs.ddcom.cn

       server{

                   listen       80;

                   server_name  bbs.dd.com.cn;

                   root  /var/www/wwwroot/bbs;

                   index  index.html index.htm index.php;

                   access_log  /var/www/logs/bbs.dd.com.cn.access.log;

                   #error_page 404 /404.html;

                   #error_page   500 502 503 504  /50x.html;

                   #rewrite ^/bbs/(.*)

                   include     php5.conf ;

         }

 

访问直接在浏览器里输入ip

 

 

 

 

阅读(2458) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-08-20 10:47:18

很好啊, 收藏了 推荐一个博客,提供很多免费电子书下载: http://free-ebooks.appspot.com