热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

CentOS5系统编译安装Nginx/PHP/MySQL/postgresql环境详解

作者:蓝闽(http://blog.lanmin.net),欢迎转载转载请注明来自:http://blog.lanmin.net/article/npm.html虽然查看有关“网站”的文章网站已经有很多编译安装nginx/php/mysql的教程,但是都是囫囵吞枣,只告诉人操作,不告诉别人为什么这么操作,让很多

虽然查看有关“网站”的文章">网站已经有很多编译安装nginx/php/mysql的教程,但是都是囫囵吞枣,只告诉人操作,不告诉别人为什么这么操作,让很多人看了一愣一愣的,只知道模仿。为让大家深入学习,才有了这篇《centos5上编译安装nginx/php/mysql详细教程》

第一步、准备编译环境和一些常用库。

sudo -s
LANG=C
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers

解释:
sudo 是使用超级用户权限,-s是执行命令,LANG=C是设置;
LANG环境变量是用来设置系统语言的变量,C代表英文环境 ASCII 编码格式;
yum = Yellow dog Updater, Modified,主要功能是更方便的添加/删除/更新RPM包.
它能自动解决包的倚赖性问题.
它能便于管理大量系统的更新问题。

第二步、所需软件和所需库介绍和下载地址

1、php   
这个我想不用介绍了,我们用5.2最新的版本5.2.14,php5.3还需要经历更多的生产环境的考验。

官方网站:www.php.net     下载地址:http://www.php.net/get/php-5.2.17.tar.gz/from/cn.php.net/mirror

2、mysql
这个一不介绍了,我们用5.5虽已经发布,但仍属于开发版,我们选择最新的稳定版5.1.55。尽管mysql希望更多的人使用5.5,
但经过别人的测试,5.1和5.5性能相差不大,5.5主要是添加了本来要在6.0中发布的功能。
官网网站:www.mysql.com  下载地址:http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.55.tar.gz/from/http://mirror.khlug.org/mysql/

3、nginx
nginx是轻量级web服务器,网站介绍的文章也已经很多了,自己去搜。nginx现在最新的开发版已经是0.9了,不过我们使用最新的稳定版0.8.54
官方网站:http://nginx.org/en,下载地址:http://nginx.org/download/nginx-0.8.54.tar.gz

4、libiconv库
libiconv库是GNU的一个free的编码转换库,它能方便在unicode和其他编码中方便的转换。更多只是请查看:libiconv介绍
官方网站:http://www.gnu.org/software/libiconv/    下载地址:http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.1.tar.gz

5、mcrypt/mhash/libmcrypt
这几个包是用户Php加密的,通过mcrypt可以实现复杂的、可还原的加密、解密操作。mhash是哈希加密的库,mcrypt依赖于mhash。
更多知识请看:mcrypt简介、安装及php中使用
下载地址:http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz?modtime=1171868460&big_mirror=0
http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gz?modtime=1194463373&big_mirror=0
http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz?modtime=1175740843&big_mirror=0

6、php-fpm

PHP-FPM是为PHP内核编写的补丁,用来根据需要处理FastCGI进程的启动,停止以及重启。这是非常关键的功能,因为nginx只能通过 FastCGI实现同PHP的接口。nginx不能像Apache那样把所有的PHP环境加载到自身。nginx通过FastCGI运行PHP比 Apache包含PHP环境有明显的优势:
◆较低的内存占用率(因为nginx单独运行时不用加载整个PHP环境)。
◆轻松地管理用户权限( 因为PHP可以在你的服务器进程中管理不同的用户)。
◆当PHP崩溃时,nginx可以照常运行。
PHP-FPM这个补丁会把FastCGI进程管理功能嵌入到PHP中。所以当你使用FPM补丁编译PHP并且选定了enalbe-fpm选项的时候,nginx会根据需要控制PHP进程的启动和停止
官网地址:http://php-fpm.org/  下载时要注意,你用php是按个版本的就要用对应版本的补丁,因此我们要下载5.2.14的补丁文件
下载地址:http://php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz
注意到是一个diff.gz的文件,gz是打包,diff是补丁文件,windows下会节约出一个.diff的文件。
更多只是请参看:diff补丁文件介绍及使用方法

7、PCRE
PCRE全称为Perl Compatible Regular Expression,意思是Perl兼容正则表达式。php中preg_开头的正则表达式都是需要用到这个库
官网:http://www.pcre.org/    下载地址:ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.12.tar.gz
更多知识请参考:PCRE简介及PCRE正则表达式使用说明

8、memcache扩展
memcache是php调用memcached(一个内存key-value缓存工具)的php扩展,关于memached网上有很多,这里就不详细介绍了。
官网:http://pecl.php.net/package/memcache   最新稳定版下载:http://pecl.php.net/get/memcache-2.2.6.tgz

9、PDO_MYSQL扩展
PDO_MYSQL是mysql的pdo驱动,通过PDO访问mysql时需要使用
官网:http://pecl.php.net/package/PDO_MYSQL  下载:http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz

10、imagick扩展及ImageMagick
ImageMagick (TM) 是一个免费的创建、编辑、合成图片的软件。它可以读取、转换、写入多种格式的图片。图片切割、颜色替换、各种效果的应用,图片的旋转、组合,文本,直线,多边形,椭圆,曲线,附加到图片伸展旋转。官方地址:http://www.imagemagick.org/script/index.php
imagick是ImageMagick的php扩展,php就能处理图片了。
官网:http://pecl.php.net/package/imagick   最新的稳定版是3.0.1地址:http://pecl.php.net/get/imagick-3.0.1.tgz
细心朋友会发展,我们的php扩展都是从pecl.php.net下载,pecl.php.net就是Php扩展的仓库,你可以找到各种Php的扩展。

11、eAccelerator
eAccelerator是一个自由开放源码php加速器,优化和动态内容缓存,提高了性能php脚本的缓存性能,使得PHP脚本在编译的状态下,对服务器的开销几乎完全消除。 它还有对脚本起优化作用,以加快其执行效率。使您的PHP程序代码执效率能提高1-10倍;
官方主页: http://eaccelerator.net    最新版本下载:http://bart.eaccelerator.net/source/0.9.6.1/eaccelerator-0.9.6.1.tar.bz2

12、PostgreSQL
postgresql数据库是一款非常优秀的关系型数据库”的文章">数据库,是遵循BSD开源协议的开源数据库。在事务、视图、大数据量存储方面直逼商业软件oracle,同时它的强项正式mysql的弱项。在mysql被sun收购,sun被oracle收购之后,很多php应用也开始使用postgresql。笔者从2007年在生产项目中多次使用postgresql数据库,实际应用的表现也是非常不多的。
官网:www.postgresql.org    源码下载:http://wwwmaster.postgresql.org/redir/198/h/source/v8.4.7/postgresql-8.4.7.tar.gz


三、编译安装及配置

1、编译安装mysql

groupadd mysql
useradd -g mysql mysql
tar zvf mysql-5.1.55.tar.gz
cd mysql-5.1.55/
./configure --prefix=/usr/local/mysql/ --enable-assembler --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile --with-plugins=innobase --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static
make && make install
chmod +w /usr/local/mysql
chown -R mysql:mysql /usr/local/webserver/mysql
cd ../

更多Mysql参数优化请参考:mysql编译安装参数优化

①、创建MySQL数据库存放目录
mkdir -p /data/mysql/3306/data/
mkdir -p /data/mysql/3306/data/binlog/
chown -R mysql:mysql /data/mysql/
②、以mysql用户帐号的身份建立数据表:
/usr/local/mysql/bin/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql/3306/data --user=mysql
③、创建my.cnf配置文件:
vim /data/mysql/3306/my.cnf
输入如下内容:
[client]
default-character-set = utf8
port    = 3306
socket  = /tmp/mysql.sock
[mysqld]
default-character-set = utf8
user    = mysql
port    = 3306
socket  = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /data/mysql/3306/data
log-error = /data/mysql/3306/mysql_error.log
pid-file = /data/mysql/3306/mysql.pid
open_files_limit    = 10240
back_log = 600
max_cOnnections= 5000
max_connect_errors = 6000
table_cache = 614
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 300
thread_cOncurrency= 8
query_cache_size = 128M
query_cache_limit = 2M
query_cache_min_res_unit = 2k
default-storage-engine = MyISAM
default_table_type = MyISAM
thread_stack = 192K
transaction_isolation = READ-COMMITTED
tmp_table_size = 246M
max_heap_table_size = 246M
long_query_time = 3
log_long_format
log-bin = /data/mysql/3306/binlog
binlog_cache_size = 4M
binlog_format = MIXED
max_binlog_cache_size = 8M
max_binlog_size = 512M
expire_logs_days = 7
key_buffer_size = 256M
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_max_extra_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover

skip-name-resolve
master-connect-retry = 10
slave-skip-errors = 1032,1062,126,1114,1146,1048,1396

#master-host     =   192.168.1.2
#master-user     =   username
#master-password =   password
#master-port     =  3306

server-id = 1

innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 512M
innodb_data_file_path = ibdata1:256M:autoextend
innodb_file_io_threads = 4
innodb_thread_cOncurrency= 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 16M
innodb_log_file_size = 128M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 0


[mysqldump]
quick
max_allowed_packet = 32M


④、创建管理MySQL数据库的shell脚本:
vim /data/mysql/3306/mysql
输入如下内容:
#!/bin/sh

mysql_port=3306
mysql_username="admin"
mysql_password="12345678"

function_start_mysql()
{
    printf "Starting MySQL...\n"
    /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/${mysql_port}/my.cnf 2>&1 > /dev/null &
}

function_stop_mysql()
{
    printf "Stoping MySQL...\n"
    /usr/local/mysql/bin/mysqladmin -u ${mysql_username} -p${mysql_password} -S /tmp/mysql.sock shutdown
}

function_restart_mysql()
{
    printf "Restarting MySQL...\n"
    function_stop_mysql
    sleep 5
    function_start_mysql
}

function_kill_mysql()
{
    kill -9 $(ps -ef | grep 'bin/mysqld_safe' | grep ${mysql_port} | awk '{printf $2}')
    kill -9 $(ps -ef | grep 'libexec/mysqld' | grep ${mysql_port} | awk '{printf $2}')
}

if [ "$1" = "start" ]; then
    function_start_mysql
elif [ "$1" = "stop" ]; then
    function_stop_mysql
elif [ "$1" = "restart" ]; then
function_restart_mysql
elif [ "$1" = "kill" ]; then
function_kill_mysql
else
    printf "Usage: /data/mysql/${mysql_port}/mysql {start|stop|restart|kill}\n"
fi

⑤、赋予shell脚本可执行权限:
chmod +x /data/mysql/3306/mysql

⑥、启动MySQL:
/data/mysql/3306/mysql  start
后面可以加参数stop/restart/kill

⑦、通过命令行登录管理MySQL服务器(提示输入密码时直接回车):
/usr/local/mysql/bin/mysql -u root -p -S /tmp/mysql.sock

⑧、输入以下SQL语句,创建一个具有root权限的用户(admin)和密码(12345678):
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' IDENTIFIED BY '12345678';
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'127.0.0.1' IDENTIFIED BY '12345678';

⑨、(可选)停止MySQL:
/data/mysql/3306/mysql stop

3、安装PostgreSql

可以参考我另一篇博客

CentOS52下编译安装PostgreSQL8.4详解

4、安装php所需库文件

知识提示:ldconfig
为了让动态链接库为系统所共享,还需运行动态链接库的管理命令--ldconfig.此执行程序存放在/sbin目录下.

ldconfig命令的用途,主要是在默认搜寻目录(/lib和/usr/lib)以及动态库配置文件/etc/ld.so.conf内所列的目录下,搜 索出可共享的动态链接库(格式如前介绍,lib*.so*),进而创建出动态装入程序(ld.so)所需的连接和缓存文件.缓存文件默认为/etc /ld.so.cache,此文件保存已排好序的动态链接库名字列表.

ldconfig通常在系统启动时运行,而当用户安装了一个新的动态链接库时,就需要手工运行这个命令.

tar zxvf libiconv-1.13.1.tar.gz
cd libiconv-1.13.1/
./configure --prefix=/usr/local
make
make install
cd ../

tar zxvf libmcrypt-2.5.8.tar.gz
cd libmcrypt-2.5.8/
./configure
make
make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make
make install
cd ../../

tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure
make
make install
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
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config

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

5、安装php(带fastcgi模式)

需要先把postgresql的库的支持共享
cd /etc/ld.so.conf.d
echo "/usr/local/pgsql/lib" >> pgsql.conf
/sbin/ldconfig

tar zxf php-5.2.17.tar.gz
//把fpm中补丁程序覆盖到php的源码上
gzip -cd php-5.2.17-fpm-0.5.17.diff.gz | patch -d php-5.2.17 -p1
cd php-5.2.17/
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pgsql=/usr/local/pgsql --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap
make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-dist /usr/local/webserver/php/etc/php.ini
cd ../

6、编译安装php5的扩展模块

知识提示:phpize简介:phpize 是属于 php-devel 中的东西,主要是设定 php 外挂模块的一些设定
所以安装 php-devel 相关套件就会有 phpize 可以使用 (档案预设存放于/安装目录/phpize )
phpize 命令是用来准备 PHP 外挂模块的编译环境的。

tar zxvf memcache-2.2.6.tgz
cd memcache-2.2.6/
/usr/local/php/bin/phpize
./configure --with-php-cOnfig=/usr/local/php/bin/php-config
make
make install
cd ../

tar jxvf eaccelerator-0.9.6.1.tar.bz2
cd eaccelerator-0.9.6.1/
/usr/local/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-cOnfig=/usr/local/php/bin/php-config
make
make install
cd ../

tar zxvf PDO_MYSQL-1.0.2.tgz
cd PDO_MYSQL-1.0.2/
/usr/local/php/bin/phpize
./configure --with-php-cOnfig=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql
make
make install
cd ../

tar zxvf ImageMagick.tar.gz
cd ImageMagick-6.5.1-2/
./configure
make
make install
cd ../

tar zxvf imagick-3.0.1.tgz
cd imagick-3.0.1/
/usr/local/php/bin/phpize
./configure --with-php-cOnfig=/usr/local/php/bin/php-config
make
make install
cd ../

7、配置php.ini、php-fcgi和eAccelerator加速器

①、修改php.ini

手工修改:查找/usr/local/php/etc/php.ini中的extension_dir = "./"
修改为extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
并在此行后增加以下几行,然后保存:
extension = "memcache.so"
extension = "pdo_mysql.so"
extension = "imagick.so"

再查找output_buffering = Off
修改为output_buffering = On

再查找; cgi.fix_pathinfo=0
修改为cgi.fix_pathinfo=0,防止Nginx文件类型错误解析漏洞。

      再查找:display_errors = On
      修改为display_errors = Off,生产环境把错误信息关闭,保证服务器安全。

②、配置eAccelerator加速PHP:

mkdir -p /usr/local/eaccelerator_cache
vim /usr/local/php/etc/php.ini
把如下文件如入末尾(vim里按G,可以直接到文尾):
[eaccelerator]
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="64"
eaccelerator.cache_dir="/usr/local/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_Only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"

③、创建php-fpm配置文件

cd /usr/local/php/etc
mv php-fpm.conf php-fpm.conf_bak
vim php-fpm.conf
加入如下内容:

  All relative paths in this config are relative to php's install prefix

 

    Pid file
    /usr/local/php/logs/php-fpm.pid

    Error log file
    /usr/local/php/logs/php-fpm.log

    Log level
    notice

    When this amount of php processes exited with SIGSEGV or SIGBUS ...
    10

    ... in a less than this interval of time, a graceful restart will be initiated.
    Useful to work around accidental curruptions in accelerator's shared memory.
    1m

    Time limit on waiting child's reaction on signals from master
    5s

    Set to 'no' to debug fpm
    yes

 

 

   

      Name of pool. Used in logs and stats.
      default

      Address to accept fastcgi requests on.
      Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket'
      127.0.0.1:9000

     

        Set listen(2) backlog
        -1

        Set permissions for unix socket, if one used.
        In Linux read/write permissions must be set in order to allow connections from web server.
        Many BSD-derrived systems allow connections regardless of permissions.
       
       
        0666
     

      Additional php.ini defines, specific to this pool of workers.
     
        /usr/sbin/sendmail -t -i
        0
     

      Unix user of processes
      www

      Unix group of processes
      www

      Process manager settings
     

        Sets style of controling worker process count.
        Valid values are 'static' and 'apache-like'
        static

        Sets the limit on the number of simultaneous requests that will be served.
        Equivalent to Apache MaxClients directive.
        Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi
        Used with any pm_style.
        32

        Settings group for 'apache-like' pm style
       

          Sets the number of server processes created on startup.
          Used only when 'apache-like' pm_style is selected
          20

          Sets the desired minimum number of idle server processes.
          Used only when 'apache-like' pm_style is selected
          5

          Sets the desired maximum number of idle server processes.
          Used only when 'apache-like' pm_style is selected
          35

       

     

      The timeout (in seconds) for serving a single request after which the worker process will be terminated
      Should be used when 'max_execution_time' ini option does not stop script execution for some reason
      '0s' means 'off'
      0s

      The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file
      '0s' means 'off'
      0s

      The log file for slow requests
      logs/slow.log

      Set open file desc rlimit
      65535

      Set max core size rlimit
      0

      Chroot to this directory at the start, absolute path
     

      Chdir to this directory at the start, absolute path
     

      Redirect workers' stdout and stderr into main error log.
      If not set, they will be redirected to /dev/null, according to FastCGI specs
      yes

      How much requests each process should execute before respawn.
      Useful to work around memory leaks in 3rd party libraries.
      For endless request processing please specify 0
      Equivalent to PHP_FCGI_MAX_REQUESTS
      1024

      Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect.
      Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi (5.2.2+)
      Makes sense only with AF_INET listening socket.
      127.0.0.1

      Pass environment variables like LD_LIBRARY_PATH
      All $VARIABLEs are taken from current environment
     
        $HOSTNAME
        /usr/local/bin:/usr/bin:/bin
        /tmp
        /tmp
        /tmp
        $OSTYPE
        $MACHTYPE
        2
     

   

 

注意:如果您安装 Nginx + PHP 用于程序调试,请将以下的0改为1,以便显示PHP错误信息,否则,Nginx 会报状态为500的空白错误页;
如果服务器资源足够可以通过32设置更多的进程,4G,可以设置128个进程

创建www用户和www组

groupadd www
useradd -g www www

启动php-cgi进程,监听127.0.0.1的9000端口,进程数为32,用户为www

ulimit -SHn 65535
/usr/local/php/sbin/php-fpm start
注意:/usr/localr/php/sbin/php-fpm还有其他参数,包括:start|stop|quit|restart|reload|logrotate,修改php.ini后不重启php-cgi,重新加载配置文件使用reload。

8、安装、配置、运行nginx

①、安装nginx所需的pcre库

tar zxf pcre-8.10.tar.gz
cd pcre-8.10/
./configure
make && make install
cd ../

②、安装nginx

tar zxvf nginx-0.8.54.tar.gz
cd nginx-0.8.54/
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
cd ../

③、配置nginx

创建Nginx日志目录
mkdir -p /data/nginx/logs
chmod +w /data/nginx/logs
chown -R www:www /data/nginx/logs

创建网站目录
mkdir -p /data/www/web1
mkdir -p /data/www/web2
chmod +w /data/www
chown -R www:www /data/www

修改nginx配置文件
cd /usr/local/nginx/conf
mv nginx.conf nginx.conf_bak
vim nginx.conf
添加如下内容:

user  www www;

worker_processes 4;

error_log  /data/nginx/logs/nginx_error.log  crit;

pid        /usr/local/nginx/nginx.pid;

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

events
{
  use epoll;
  worker_connections 65535;
}

http
{
  include       mime.types;
  default_type  application/octet-stream;

  #charset  gb2312;
  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k;
  large_client_header_buffers 4 32k;
  client_max_body_size 8m;
  sendfile on;
  tcp_nopush     on;

  keepalive_timeout 60;

  tcp_nodelay on;

  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;

  gzip on;
  gzip_min_length  1k;
  gzip_buffers     4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types       text/plain application/x-Javascript text/css application/xml;
  gzip_vary on;

  #limit_zone  crawler  $binary_remote_addr  10m;

  server
  {
    listen       80;
    server_name  www.web1.com;
    index index.html index.htm index.php;
    root  /data/www/web1;

    #limit_conn   crawler  20;   
    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  web1log  '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
    access_log  /data/nginx/logs/web1.log web1log;
      }

  server
  {
    listen       80;
    server_name  www.web2.com;
    index index.html index.htm index.php;
    root  /data/www/web2;

    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 /status {
        stub_status on;
        access_log   off;
    }

    log_format  web2log  '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
    access_log  /data/nginx/logs/web2.log  web2log;
  }

}

可以通过修改worker_processes来增减nginx的进程数,一般网站2-4个进程就够了,如果资源够可以开更多进程,一个进程10+M的内存;
通过在web2网站中,添加 location /status,就可以通过www.web2.com/status  查看nginx的连接数

④、创建fcgi.conf文件

vim 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;

⑤、启动nginx

建立快捷方式

ln -s /usr/local/nginx/sbin/nginx /sbin/nginx
这样就不用每次加上路径,而是可以直接执行nginx,就可以启动nginx
ulimit -SHn 65535
nginx

ps aux | grep nginx  就可以查到nginx的进程

⑥、nginx变更

修改nginx.conf文件后,可以通过如下命令检查是否修改正确
/usr/local/nginx/sbin/nginx -t
或者直接nginx -t(这是nginx0.8新增的功能),之前是nginx -t -c /usr/local/nginx/conf/nginx.conf
提示successfully,说明OK

平滑变更配置
/usr/local/nginx/sbin/nginx -s reload
或者nginx -s reload
nginx0.8之前更麻烦,需要如下
kill -HUP `cat /usr/local/nginx/nginx.pid`   或者先获取主进程号,再kill -HUP
ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'

⑦、nginx日志切割

创建切割的shell脚本
vim /usr/local/nginx/sbin/cut_nginx_log.sh
输入如下内容:
#!/bin/bash
# This script run at 03:00

# The Nginx logs path
logs_path="/data/nginx/logs/"

mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
mv ${logs_path}web1.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/web1_$(date -d "yesterday" +"%Y%m%d").log
mv ${logs_path}web2.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/web2_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /usr/local/nginx/nginx.pid`

设置crontab每天凌晨3点切割日志
crontab -e
输入如下内容
03 00 * * * /bin/bash  /usr/local/nginx/sbin/cut_nginx_log.sh

9、系统设置

①、设置开启懂nginx/php/mysql
vim /etc/rc.local
文尾添加如下内容
ulimit -SHn 65535
/bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/3306/my.cnf 2>&1 > /dev/null &
/usr/local/php/sbin/php-fpm start
/usr/local/nginx/sbin/nginx
 

②、优化linux内核参数
vim /etc/sysctl.conf
文尾加入如下参数
# Add
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog =  32768
net.core.somaxcOnn= 32768

net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216

net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2

net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800

#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 1024  65535

使配置立即生效:
/sbin/sysctl -p


推荐阅读
author-avatar
刺猬xiaojie
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有