linux,nginx,mysql,php安装备忘录

技术交流16年前 (2008)发布
293 0

niginx安装流程

零,准备工作

软件下载

mkdir -p /home/dancebear/programe
cd /home/dancebear/programe
wget http://sysoev.ru/nginx/nginx-0.6.31.tar.gz
wget http://www.php.net/get/php-5.2.6.tar.gz/from/this/mirror
wget http://php-fpm.anight.org/downloads/head/php-5.2.6-fpm-0.5.8.diff.gz
wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.26-rc.tar.gz/from/http://mirror.x10.com/mirror/mysql/
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.12.tar.gz
#wget "http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz?modtime=1171868460&big_mirror=0"
wget http://mirror.optus.net/sourceforge/m/mc/mcrypt/libmcrypt-2.5.8.tar.gz
#wget "http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.7.tar.gz?modtime=1194463373&big_mirror=0"
wget http://mirror.optus.net/sourceforge/m/mc/mcrypt/mcrypt-2.6.7.tar.gz
wget http://pecl.php.net/get/memcache-2.2.3.tgz
#wget "http://downloads.sourceforge.net/mhash/mhash-0.9.9.tar.gz?modtime=1175740843&big_mirror=0"
wget http://mirror.optus.net/sourceforge/m/mh/mhash/mhash-0.9.9.tar.gz
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.7.tar.gz
wget http://xcache.lighttpd.net/pub/Releases/1.2.2/xcache-1.2.2.tar.gz

一、安装MySQL
编译安装MySQL 5.1.26-rc

/usr/sbin/groupadd  mysql
/usr/sbin/useradd -g mysql mysql
tar zxvf  mysql-5.1.26-rc.tar.gz
cd mysql-5.1.26-rc/
./configure  –prefix=/usr/local/mysql/ –without-debug  –with-unix-socket-path=/usr/local/mysql/mysql.sock  –with-client-ldflags=-all-static –with-mysqld-ldflags=-all-static  –enable-assembler –with-extra-charsets=gbk,gb2312,utf8 –with-pthread  –enable-thread-safe-client
make && make install
chmod +w  /usr/local/webserver/mysql
chown -R mysql:mysql  /usr/local/mysql
cp support-files/my-medium.cnf  /usr/local/mysql/my.cnf
cd ../

  附:以下为附加步骤,如果你想在这台服务器上运行MySQL数据库,则执行以下两步。如果你只是希望让PHP支持MySQL扩展库,能够连接其他服务器上的MySQL数据库,那么,以下两步无需执行。
  ①、以mysql用户帐号的身份建立数据表:

/usr/local/mysql/bin/mysql_install_db  –defaults-file=/usr/local/mysql/my.cnf  –basedir=/usr/local/mysql –datadir=/usr/local/mysql/data  –user=mysql –pid-file=/usr/local/mysql/mysql.pid –skip-locking  –port=3306  –socket=/tmp/mysql.sock

  ②、启动MySQL(最后的&表示在后台运行)

/bin/sh  /usr/local/mysql/bin/mysqld_safe  –defaults-file=/usr/local/mysql/my.cnf &

二、PHP安装(FastCGI)

#开始安装
#tar zxvf php-5.2.6.tar.gz
#打php-fpm补丁
gzip -cd php-5.2.6-fpm-0.5.8.diff.gz | patch -d php-5.2.6 -p1
#开始编译
#cd php-5.2.6/
#./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-iconv-dir=/usr/local –with-freetype-dir –with-jpeg-dir –with-png-dir  –with-zlib –with-libxml-dir=/usr –enable-xml –disable-debug –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
#make && make install
#cd ../

创建www用户和组

/usr/sbin/groupadd www -g 48
/usr/sbin/useradd -u 48 -g www www

创建php-fpm配置文件(php-fpm是为PHP打的一个FastCGI管理补丁,可以平滑变更php.ini配置而无需重启php-cgi):
  在/usr/local/php/etc/目录中创建php-fpm.conf文件:

rm  -f /usr/local/php/etc/php-fpm.conf
vi  /usr/local/php/etc/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  
  
&nb

sp;    &
nbsp;                  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.  
                                128  
  
                                Settings group for ‘apache-like’ pm style  
                                  
  
                                  &n

bsp;     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  
                                        250  
  
                                  
  
                          
  
                        Time limit on waiting execution of single request  
                        Should be used when ‘max_execution_time’ ini option does not terminate execution for some reason  
                        31s  
  
                        Set open file desc rlimit  
                        51200  
  
                        Set max core size rlimit  
                        0  
  
                        Chroot to this directory at the start  
                          
  
                        Chdir to this directory at the start  
                          
  
                        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  
                        51200  
  
                        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 variab

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

启动php-cgi进程,监听127.0.0.1的9000端口,进程数为128(如果服务器内存小于3GB,可以只开启25个进程),用户为www:

ulimit -SHn 51200
/usr/local/php/sbin/php-fpm start

使用php-fpm可以不使用以下步骤
三,安装fastcgi

看了下网上兄弟们的文章,lighttpd里面的spawn-fcgi可用,那就编译一下 lighttpd-1.4.19,不用安装,然后按照以下方法设置:

#cp spawn-fcgi /opt/webser/php/bin/
#chmod +x /opt/webser/php/bin/spawn-fcgi
运行fastcgi:

#/opt/webser/php/bin/spawn-fcgi -a 127.0.0.1 -p 8080 -C 64 -u www -f /opt/webser/php/bin/php-cgi
关于spawn-fcgi的运行参数,可以查看:

http://linux.die.net/man/1/spawn-fcgi

四、安装nginx

#/usr/sbin/groupadd www -g 68
#/usr/sbin/useradd -u 68 -g www www
#mkdir -p /home/data/www
#chmod +w /home/data/www
#chown -R www:www /home/data/www
在测试过程中,服务目录是/home/data/www,挺别致的吧,呵呵,

#tar zxvf pcre-7.2.tar.gz
#tar zxvf nginx-0.5.35.tar.gz
#cd nginx-0.5.35
#./configure –user=www –group=www –prefix=/opt/webser/nginx –with-pcre=/home/alpha/nginx-php/pcre-7.2 –with-http_stub_status_module
#make && make install
更多的关于nginx的configure参数可以查看:

http://wiki.codemongers.com/NginxChsInstall

五,nginx配置

编辑Nginx配置文件nginx.conf

vim /usr/local/nginx/conf/nginx.conf

配置文件内容

user  www www;

worker_processes 2;

error_log  /usr/local/niginx/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  51200;

events
{
     use epoll;

     worker_connections  51200;
}

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

     charset  utf-8;
      
     server_names_hash_bucket_size 128;
    
     sendfile on;
      tcp_nopush     on;

     keepalive_timeout 60;

     tcp_nodelay  on;

     fastcgi_connect_timeout 60;
     fastcgi_send_timeout  180;
     fastcgi_read_timeout 180;
     fastcgi_buffer_size 128k;
      fastcgi_buffers 4 128k;
     fastcgi_busy_buffers_size 128k;
      fastcgi_temp_file_write_size 128k;
     fastcgi_temp_path /dev/shm;

      gzip on;
     gzip_min_length  1k;
     gzip_buffers     4 8k;
      gzip_http_version 1.1;
     gzip_types       text/plain  application/x-javascript text/css text/html application/xml;

      server
     {
             listen       80;
             server_name   www.tt.com;
             index index.html index.htm index.php;
              root  /usr/local/vhost/tt.com/root;

             if (-d  $request_filename)
             {
                    rewrite  ^/(.*)([^/])$ http://$host/$1$2/  permanent;
             }
                            
              location ~ .*\.php?$
             {
                  include fcgi.conf;  &nb

sp;   
                  #fastcgi_pass  unix:/tmp/php-cgi.sock;
                   fastcgi_pass  127.0.0.1:9000;
                  fastcgi_index  index.php;
             }

             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  /usr/local/nginx/logs/access.log  access;
     }

     server
      {
             listen       80;
             server_name 192.168.203.129;
              index index.html index.htm index.php;
             root   /usr/local/niginx/html;

             if (-d $request_filename)
              {
                    rewrite ^/(.*)([^/])$ http://$host/$1$2/ permanent;
             }

              location ~ .*\.php?$
             {
                  include  fcgi.conf;
                  #fastcgi_pass  unix:/tmp/php-cgi.sock;
                   fastcgi_pass  127.0.0.1:9000;
                  fastcgi_index  index.php;
             }

             log_format  wwwlogs   ‘$remote_addr – $remote_user [$time_local] "$request" ‘
                                    ‘$status $body_bytes_sent "$http_referer" ‘
                                    ‘"$http_user_agent" $http_x_forwarded_for’;
              access_log  /usr/local/nigix/logs/wwwlogs.log  wwwlogs;
     }

     server
      {
             listen  80;
             server_name   status.tt.com;

             location / {
                   stub_status on;
                  access_log   off;
             }
      }
}

编辑配置文件fcgi.conf

vim /usr/local/webserver/nginx/conf/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

ulimit -SHn 51200
/usr/local/nginx/sbin/nginx

修改/usr/local/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:

/usr/local/nginx/sbin/nginx -t

如果屏幕显示以下两行信息,说明配置文件正确:
  the configuration file  /usr/local/nginx/conf/nginx.conf syntax is ok
  the configuration  file /usr/local/nginx/conf/nginx.conf was tested  successfully
查看Nginx主进程号

ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ‘ ‘ ‘{print $2}’

杀掉Nginx进程

kill -HUP Nginx进程ID

或者执行

kill -HUP `cat /usr/local/nginx/nginx.pid`

直接杀死进程即可重新载入配置文件而不用重启nginx进程
配置开机自动启动Nginx + PHP

vim /etc/rc.local

增加一下内容

ulimit -SHn  51200
/usr/local/php/sbin/php-fpm  start
/usr/local/nginx

/sbin/nginx

优化Linux内核参数

vim /etc/sysctl.conf

增加:

net.ipv4.tcp_fin_timeout =  30
net.ipv4.tcp_keepalive_time = 300
net.ipv4.tcp_syncookies =  1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle =  1
net.ipv4.ip_local_port_range = 5000    65000

使配置立即生效:

/sbin/sysctl -p
 

 

转自大熊博客:http://www.dancebear.cn/read.php/30.htm

© 版权声明

相关文章

暂无评论

  • dancebear
    dancebear 游客

    我的博客可不在那里,那里是小鱼的垃圾站。哈哈

    回复
  • dancebear
    dancebear 游客

    我的博客可不在那里,那里是小鱼的垃圾站。哈哈

    回复