Home > Notes > Nginx架构WEB服务器

Nginx架构WEB服务器

Tue, 02 Jun 2009 20:17:53 +0800

现在说一下LNMP架构的服务器的安装过程,作为自己的一个笔记,也为一些需要帮助的朋友提供点参考。
L方面使用Debian(其实我用FreeBSD),基本是个什么都没的干净系统(连GCC都没)。

1.首先我们安装GCC编译环境
因为后面的软件都需要编译,所以这个不装就太不像话了。
apt-get install build-essential

如果出现如下提示:
E: Couldn't find package build-essential
则需要更新包列表:
apt-get update

此开发环境包安装后,则已经安装上如下包了:
binutils bzip2 cpp cpp-4.3 dpkg-dev g++ g++-4.3 gcc gcc-4.3 libc6 libc6-dev libgmp3c2 libgomp1 libmpfr1ldbl libstdc++6-4.3-dev libtimedate-perl linux-libc-dev make patch perl perl-modules

Suggested packages:
  binutils-doc bzip2-doc cpp-doc gcc-4.3-locales debian-keyring g++-multilib g++-4.3-multilib gcc-4.3-doc libstdc++6-4.3-dbg gcc-multilib
  manpages-dev autoconf automake1.9 libtool flex bison gdb gcc-doc gcc-4.3-multilib libmudflap0-4.3-dev libgcc1-dbg libgomp1-dbg libmudflap0-dbg
  glibc-doc libc6-i686 libstdc++6-4.3-doc make-doc diff-doc perl-doc libterm-readline-gnu-perl libterm-readline-perl-perl

2.安装MYSQL
获取MysQL
wget http://mysql.west.mirrors.airband.net/Downloads/MySQL-5.1/mysql-5.1.34.tar.gz

tar -zxvf mysql-5.1.34.tar.gz
cd mysql-5.1.34
./configure --prefix=/usr/local/mysql --with-charset=utf8 \
--with-extra-charsets=complex \
--enable-thread-safe-client \
--enable-assembler \
--with-big-tables \
--with-readline \
--with-ssl \
--with-embedded-server \
--enable-local-infile \
--with-plugins=innobase

make && make install

如果有以下错误
checking for termcap functions library... configure: error: No curses/termcap library found
说明 curses/termcap 库没有安装
apt-cache search curses | grep lib

安装 libncurses5-dev

wget http://ftp.gnu.org/pub/gnu/ncurses/ncurses-5.6.tar.gz
tar zxvf ncurses-5.6.tar.gz
cd ncurses-5.6
./configure --prefix=/usr --with-shared --without-debug
make
make install clean
或者这样更方便
apt-get install libncurses5-dev

之后重新安装MYSQL部分

完成后加入运行mysql用户和组
groupadd mysql
useradd -g mysql mysql

更改权限以及拷贝默认配置文件
chmod +w /usr/local/mysql
chown -R mysql:mysql /usr/local/mysql

cp support-files/my-medium.cnf /usr/local/mysql/my.cnf

*
建立MYSQL数据库
/usr/local/mysql/bin/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql

设置为开机启动MYSQL
在/etc/rc.local中加入以下启动语句
/bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/my.cnf &

如果有错误,查看mysql的错误日志即可。

3.安装PHP
因为要与Nginx配合,所以PHP以FastCGI的方式安装和运行。
同样,PHP也有相关的包要装。

首先获取PHP:
wget http://us.php.net/distributions/php-5.2.9.tar.gz

因为PHP需要一些库,我们先安装,具体如下:
libiconv libxml2 libxslt freetype libmcrypt jpeg6 libpng curl gd
OpenSSL-dev

wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.tar.gz
tar zxvf libiconv-1.13.tar.gz
cd libiconv-1.13/
./configure --prefix=/usr/local
make && make install && make clean
cd ..

wget http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz
tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8
./configure
make && make install
cd libltdl
./configure --enable-ltdl-install
make && make install
cd ..
make clean

wget http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz
tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9
./configure
make && make install && make clean
cd ..

//注
ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1

wget http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gz
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8
/sbin/ldconfig
./configure
make && make install && make clean
cd ..

安装libxml2
apt-get install libxml2-dev

安装libxslt
wget ftp://xmlsoft.org/libxslt/libxslt-1.1.24.tar.gz
tar -zxf libxslt-1.1.24.tar.gz
cd libxslt-1.1.24
./configure --prefix=/usr/local/libxslt --with-libxml-prefix=/usr/local/libxml2
make && make install && make clean
cd ..

安装OpenSSL-dev
aptitude update
aptitude install openssl-dev

安装libzlib1g-dev
apt-get install libzlib1g-dev

安装freetype
wget http://superb-west.dl.sourceforge.net/sourceforge/freetype/freetype-2.3.9.tar.gz
tar zxvf freetype-2.3.9.tar.gz
cd freetype-2.3.9
./configure --prefix=/usr/local/freetype
make && make install && make clean
cd ..

安装libpng
wget http://superb-west.dl.sourceforge.net/sourceforge/libpng/libpng-1.2.36.tar.gz
tar zxvf libpng-1.2.36.tar.gz
cd libpng-1.2.36
cp scripts/makefile.std makefile
make && make install

安装jpeg6
mkdir /usr/local/jpeg6
mkdir /usr/local/jpeg6/bin
mkdir /usr/local/jpeg6/lib
mkdir /usr/local/jpeg6/include
mkdir /usr/local/jpeg6/man
mkdir /usr/local/jpeg6/man/man1
wget http://www.ijg.org/files/jpegsrc.v6b.tar.gz
tar zxvf jpegsrc.v6b.tar.gz
cd jpeg-6b
./configure --prefix=/usr/local/jpeg6/ --enable-shared --enable-static
make && make install && make clean
cd ..

安装GD库
wget http://www.boutell.com/gd/http/gd-2.0.33.tar.gz
tar -zxf gd-2.0.33.tar.gz
cd gd-2.0.33
./configure --prefix=/usr/local/gd2 \
--with-jpeg=/usr/local/jpeg6/ \
--with-png \
--with-zlib \
--with-freetype=/usr/local/freetype/
make && make install && make clean
cd ..

安装CURL
wget http://curl.download.nextag.com/download/curl-7.19.5.tar.gz
tar -zxf curl-7.19.5.tar.gz
cd curl-7.19.5
./configure --prefix=/usr/local/curl

配置并编译PHP(谢谢patch来源提供者):
wget http://blog.s135.com/soft/linux/nginx_php/phpfpm/php-5.2.9-fpm-0.5.10.diff.gz
gzip -cd php-5.2.9-fpm-0.5.10.diff.gz | patch -d php-5.2.9 -p1
./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/php5 \
--with-mysql=/usr/local/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-iconv-dir=/usr/local \
--with-freetype-dir=/usr/local/freetype \
--with-jpeg-dir=/usr/local/jpeg6 \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr/local/libxml2 \
--enable-xml \
--disable-rpath \
--enable-discard-path \
--enable-safe-mode \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl=/usr/local/curl \
--with-curlwrappers \
--enable-mbregex \
--enable-fastcgi \
--enable-fpm \
--enable-force-cgi-redirect \
--enable-mbstring \
--with-mcrypt \
--with-gd=/usr/local/gd2 \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-ldap \
--with-ldap-sasl \
--with-xmlrpc \
--with-xsl=/usr/local/libxslt \
--enable-xslt \
--enable-zip
make ZEND_EXTRA_LIBS='-liconv'
make install

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

4. 安装memcache以及eaccelerator

memcache

wget http://pecl.php.net/get/memcache-2.2.5.tgz
tar -zxf memcache-2.2.5.tgz
cd memcache-2.2.5
/usr/local/php5/bin/phpize
./configure --with-php-config=/usr/local/php5/bin/php-config
make && make install && make clean

eAccelerator
wget http://bart.eaccelerator.net/source/0.9.5.3/eaccelerator-0.9.5.3.tar.bz2
tar jxvf eaccelerator-0.9.5.3.tar.bz2
cd eaccelerator-0.9.5.3
/usr/local/php5/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php5/bin/php-config
make && make install
cd ..

修改php.ini,以加memcache和eaccelerator
修改为extension_dir = "/usr/local/php5/lib/php/extensions/no-debug-non-zts-20060613/"
extension = "memcache.so"
修改output_buffering 为 On

加入eAccelerator加速
mkdir /usr/local/ea_cache

[eaccelerator]
zend_extension="/usr/local/php5/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="32"
eaccelerator.cache_dir="/usr/local/ea_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="300"
eaccelerator.shm_prune_period="120"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"

修改php-fpm配置文件,启动php-cgi进程
vi /usr/local/php5/etc/php-fpm.conf
内容如下:
<?xml version="1.0" ?>
<configuration>
  <section name="global_options">
    <value name="pid_file">/usr/local/php5/logs/php-fpm.pid</value>
    <value name="error_log">/usr/local/php5/logs/php-fpm.log</value>
    <value name="log_level">notice</value>
    <value name="emergency_restart_threshold">10</value>
    <value name="emergency_restart_interval">1m</value>
    <value name="process_control_timeout">5s</value>
    <value name="daemonize">yes</value>
  </section>

  <workers>
    <section name="pool">
      <value name="name">default</value>
      <value name="listen_address">127.0.0.1:9000</value>
      <value name="listen_options">
        <value name="backlog">-1</value>
        <value name="owner"></value>
        <value name="group"></value>
        <value name="mode">0666</value>
      </value>
      <value name="php_defines">
        <value name="sendmail_path">/usr/sbin/sendmail -t -i</value>
        <value name="display_errors">1</value>
      </value>
        <value name="user">www</value>
        <value name="group">www</value>
      <value name="pm">
        <value name="style">static</value>
        <value name="max_children">25</value>
        <value name="apache_like">
          <value name="StartServers">20</value>
          <value name="MinSpareServers">5</value>
          <value name="MaxSpareServers">35</value>
        </value>
      </value>
      <value name="request_terminate_timeout">0s</value>
      <value name="request_slowlog_timeout">0s</value>
      <value name="slowlog">logs/slow.log</value>
      <value name="rlimit_files">10240</value>
      <value name="rlimit_core">0</value>
      <value name="chroot"></value>
      <value name="chdir"></value>
      <value name="catch_workers_output">yes</value>
      <value name="max_requests">10240</value>
      <value name="allowed_clients">127.0.0.1</value>
      <value name="environment">
        <value name="HOSTNAME">$HOSTNAME</value>
        <value name="PATH">/usr/local/bin:/usr/bin:/bin</value>
        <value name="TMP">/tmp</value>
        <value name="TMPDIR">/tmp</value>
        <value name="TEMP">/tmp</value>
        <value name="OSTYPE">$OSTYPE</value>
        <value name="MACHTYPE">$MACHTYPE</value>
        <value name="MALLOC_CHECK_">2</value>
      </value>
    </section>
  </workers>

</configuration>

5. 安装Nginx
Nginx依赖一些库,先安装它们,但都取决于是否需要
.gzip 模块需要 zlib 库
.rewrite 模块需要 pcre 库
.ssl 功能需要 openssl 库

我需要rewrite,所以得装个pcre
wget http://superb-west.dl.sourceforge.net/sourceforge/pcre/pcre-7.9.tar.gz
tar zxf pcre-7.9.tar.gz
cd pcre-7.9
./configure
make && make install
cd ..

创建www用户和组,用于Nginx
好了,装Nginx

wget http://sysoev.ru/nginx/nginx-0.7.59.tar.gz
tar zxf nginx-0.7.59.tar.gz
cd nginx-0.7.59
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install

配置Nginx
cd /usr/local/nginx/conf

#vi nginx.conf
server段内
 location ~ .*\.(php|php5)?$
    {
      #fastcgi_pass  unix:/tmp/php-cgi.sock;
      fastcgi_pass  127.0.0.1:9000;
      fastcgi_index index.php;
      include fcgi.conf;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
      expires      30d;
    }

    location ~ .*\.(js|css)?$
    {
      expires      1h;
    }

    log_format access '$remote_addr - $remote_user [$time_local] "$request" '
              '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent" $http_x_forwarded_for';
    access_log  /data1/logs/access.log  access;

fcgi.conf的内容如下:
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;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
#fastcgi_param  REDIRECT_STATUS    200;

开机启动
在/etc/rc.local中加入
ulimit -SHn 10240
/usr/local/php5/sbin/php-fpm start
/usr/local/nginx/sbin/nginx

Tag:Nginx, UNIX, WEB, 服务器
Hots
Tags
Recent Post
Recent Comments
Links
Copyright Notes
You can reship all of these articles without permission but MUST mark the original link in your post. Please contact with me() if u have advice or other arrangements.
Copyright©2007-2011 lewphee.com All rights reserved.