nginx+php+mysql源码安装配置

nginx+php+mysql源码安装配置
系统要求:linux 2.6+内核,本文中的linux操作系统为centos 5.5  64bit)
下载程序源码包:
cd  /opt/software
wget http://blog.s135.com/soft/linux/nginx_php/nginx/nginx-0.7.51.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/php/php-5.2.8.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/phpfpm/php-5.2.8-fpm-0.5.10.diff.gz
wget http://blog.s135.com/soft/linux/nginx_php/mysql/mysql-5.1.33.zip
wget http://blog.s135.com/soft/linux/nginx_php/libiconv/libiconv-1.13.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/libmcrypt-2.5.8.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/mcrypt-2.6.8.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/memcache/memcache-2.2.5.tgz
wget http://blog.s135.com/soft/linux/nginx_php/mhash/mhash-0.9.9.9.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/pcre/pcre-7.8.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/eaccelerator/eaccelerator-0.9.5.3.tar.bz2
wget http://blog.s135.com/soft/linux/nginx_php/pdo/PDO_MYSQL-1.0.2.tgz
wget http://blog.s135.com/soft/linux/nginx_php/imagick/ImageMagick.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/imagick/imagick-2.2.2.tgz
安装步骤:
先安装系统所需的支持库:
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
一、编译安装数据库:(mysql 5.1. 33版本)
cd /opt/software/
tar zxvf mysql-5.1.33.tar.gz
cd mysql-5.1.33
./configure --prefix=/opt/app/mysql5 --localstatedir=/opt/app/mysql5/var --with-comment=Source --with-server-suffix=-enterprise-mysql --without-debug --with-charset=utf8 --with-extra-charsets=all --enable-thread-safe-client --with-pthread --with-plugins=all --without-ndb-debug
#make && make install
特别注意:在执行make编译的过程中,如果你看到屏幕上出现下面这一行(make[4]: Entering directory `/opt/software/mysql-5.1.59/mysql-test'),卡在那里,很久都过不去,千万别以为是编译错误了,或者服务器卡住了,只需要坚持,耐心的等待几分钟,就会编译成功!
/usr/sbin/groupadd mysql                                 #添加mysql组
/usr/sbin/useradd -g mysql mysql -s /bin/false    #创建用户mysql并加入到mysql组,不允许mysql用户直接登录系统
chown -R root /opt/app/mysql5/                        #允许root访问
chgrp -R mysql /opt/app/mysql5/                      #允许mysql组访问  
cd /opt/software/mysql-5.1.33/  
cp support-files/mysql.server /etc/rc.d/init.d/mysqld  #把Mysql加入系统启动
cp support-files/my-medium.cnf /etc/my.cnf #拷贝配置文件(注意:/etc目录下面默认有一个my.cnf,直接覆盖即可)
chmod 755 /etc/init.d/mysqld   #执行权限
vim /etc/profile   #把mysql服务加入系统环境变量。
 在最后添加下面这一行:
 export PATH=$PATH:/opt/app/mysql5/bin
source /etc/profile 在当前bash环境下读取 profile文件
mysql_install_db --user=mysql &  #生成mysql系统数据库
mysqld_safe --user=mysql &       #启动mysql
/etc/init.d/mysqld  start #启动
修改root密码:进去到mysql安装目录的bin目录下执行
cd /opt/app/mysql5/bin
mysqladmin --user=root password 123456
service mysqld restart  #重启
到此,mysql安装完成。
二、编译安装PHP 5.2.8版本  (FastCGI模式)
1. 编译安装PHP 5.2.8所需的支持库
cd /opt/software
tar zxvf libiconv-1.13.tar.gz
cd libiconv-1.13/
./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/bin/libmcrypt-config /usr/bin/libmcrypto-config
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
tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig
./configure
make && make install
cd ../
2.编译安装PHP (FastCGI模式)
cd /opt/software
tar zxvf php-5.2.8.tar.gz
gzip -cd php-5.2.8-fpm-0.5.10.diff.gz | patch -d php-5.2.8 -p1
cd php-5.2.8/
./configure --prefix=/opt/app/php5 --with-config-file-path=/opt/app/php5/etc --with-mysql=/opt/app/mysql5  --with-mysqli=/opt/app/mysql5/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-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
make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-dist /opt/app/php5/etc/php.ini
cd ../
/usr/sbin/groupadd www               创建www组
/usr/sbin/useradd -g www www    创建www用户,并归属到www组
3.编译安装PHP5扩展模块 (此步骤可以省略,以后可以根据程序员的需求再进行安装)
tar zxvf memcache-2.2.5.tgz
cd memcache-2.2.5/
/opt/app/php5/bin/phpize
./configure --with-php-config=/opt/app/php5/bin/php-config
make
make install
cd ../
tar jxvf eaccelerator-0.9.5.3.tar.bz2
cd eaccelerator-0.9.5.3/
/opt/app/php5/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/opt/app/php5/bin/php-config
make
make install
cd ../
tar zxvf PDO_MYSQL-1.0.2.tgz
cd PDO_MYSQL-1.0.2/
/opt/app/php5/bin/phpize
./configure --with-php-config=/opt/app/php5/bin/php-config --with-pdo-mysql=/opt/app/mysql5
make
make install
cd ../
tar zxvf ImageMagick.tar.gz
cd ImageMagick-6.5.1-2/
./configure
make
make install
cd ../
tar zxvf imagick-2.2.2.tgz
cd imagick-2.2.2/
/opt/app/php5/bin/phpize
./configure --with-php-config=/opt/app/php5/bin/php-config
make
make install
cd ../
4.修改php.ini文件 (如果没安装php5的扩展模块,以下三行步骤省略)
 1)手工修改:
      #vim /opt/app/php5/etc/php.ini
      查找:/extension_dir = "./"
      修改:extension_dir = "/opt/app/php5/lib/php5/extensions/no-debug-non-zts-20060613/"
 2)并在此行后增加以下几行,然后保存:
      extension = "memcache.so"
      extension = "pdo_mysql.so"
      extension = "imagick.so"
 3)再查找output_buffering = Off
      修改为output_buffering = On
5. 配置eAccelerator加速PHP (此步骤可以省略,可以根据程序员的要求做此步骤)
#mkdir �Cp /opt/app/eaccelerator_cache
#vim /opt/app/php5/etc/php.ini
按shift+g键跳到配置文件的最末尾,加上以下配置信息:
[eaccelerator]
zend_extension="/opt/app/php5/lib/php5/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="128"
eaccelerator.cache_dir="/opt/app/eaccelerator_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"
6. 创建php-fpm配置文件(php-fpm是为PHP打的一个FastCGI管理补丁,可以平滑变更php.ini配置而无需重启php-cgi):
 在/opt/app/php5/etc/目录中创建php-fpm.conf文件:
rm -f /opt/app/php5/etc/php-fpm.conf
vim /opt/app/php5/etc/php-fpm.conf
<?xml version="1.0" ?>
<configuration>
All relative paths in this config are relative to php's install prefix
 <section name="global_options">
 Pid file
   <value name="pid_file">/opt/app/php5/logs/php-fpm.pid</value>
   Error log file
   <value name="error_log">/opt/app/php5/logs/php-fpm.log</value>
   Log level
   <value name="log_level">notice</value>
   When this amount of php processes exited with SIGSEGV or SIGBUS ...
   <value name="emergency_restart_threshold">10</value>
   ... 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.
   <value name="emergency_restart_interval">1m</value>
   Time limit on waiting child's reaction on signals from master
   <value name="process_control_timeout">5s</value>
   Set to 'no' to debug fpm
   <value name="daemonize">yes</value>
 </section>
 <workers>
   <section name="pool">
     Name of pool. Used in logs and stats.
     <value name="name">default</value>
     Address to accept fastcgi requests on.
     Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket'
     <value name="listen_address">127.0.0.1:9000</value>
     <value name="listen_options">
       Set listen(2) backlog
       <value name="backlog">-1</value>
       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.
       <value name="owner"></value>
       <value name="group"></value>
       <value name="mode">0666</value>
     </value>
     Unix user of processes
       <value name="user">www</value>
     Unix group of processes
       <value name="group">www</value>
     Process manager settings
     <value name="pm">
       Sets style of controling worker process count.
       Valid values are 'static' and 'apache-like'
       <value name="style">static</value>
       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.
       <value name="max_children">128</value>
       Settings group for 'apache-like' pm style
       <value name="apache_like">
         Sets the number of server processes created on startup.
         Used only when 'apache-like' pm_style is selected
         <value name="StartServers">20</value>
         Sets the desired minimum number of idle server processes.
         Used only when 'apache-like' pm_style is selected
         <value name="MinSpareServers">5</value>
         Sets the desired maximum number of idle server processes.
         Used only when 'apache-like' pm_style is selected
         <value name="MaxSpareServers">35</value>
       </value>
     </value>
     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'
     <value name="request_terminate_timeout">0s</value>
     The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file
     '0s' means 'off'
     <value name="request_slowlog_timeout">0s</value>
     The log file for slow requests
     <value name="slowlog">logs/slow.log</value>
     Set open file desc rlimit
     <value name="rlimit_files">51200</value>
     Set max core size rlimit
     <value name="rlimit_core">0</value>
     Chroot to this directory at the start, absolute path
     <value name="chroot"></value>
     Chdir to this directory at the start, absolute path
     <value name="chdir"></value>
     Redirect workers' stdout and stderr into main error log.
     If not set, they will be redirected to /dev/null, according to FastCGI specs
     <value name="catch_workers_output">yes</value>
     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
     <value name="max_requests">500</value>
     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.
     <value name="allowed_clients">127.0.0.1</value>
     Pass environment variables like LD_LIBRARY_PATH
     All $VARIABLEs are taken from current environment
     <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>
9、启动php-cgi进程,监听127.0.0.1的9000端口,进程数为200(如果服务器内存小于3GB,可以只开启64个进程),用户为www:
ulimit -SHn 51200
/opt/app/php5/sbin/php-fpm start
注:/opt/app/php/sbin/php-fpm还有其他参数,包括:start|stop|quit|restart|reload|logrotate,修改php.ini后不重启php-cgi,重新加载配置文件使用reload
三、编译安装nginx 0.7.51
1、安装Nginx所需的pcre库:
tar zxvf pcre-7.8.tar.gz
cd pcre-7.8/
./configure
make && make install
cd ../
2、安装Nginx
tar zxvf nginx-0.7.51.tar.gz
cd nginx-0.7.51/
./configure --user=www --group=www --prefix=/opt/app/nginx  --with-http_stub_status_module --with-http_ssl_module
make && make install
cd ../
3、准备工作:
mkdir -p /opt/case     建一个web网站主页的目录
chown -R www:www /opt/case   给主页目录授权
usermod -G  www,wangm      将wangm加入到www用户中去
mkdir -p /opt/app/nginx/conf/vhosts   建一个nginx配置文件的目录
chmod 755 /opt/app/nginx/conf/vhosts       给vhosts分配755权限
4、创建Nginx配置文件
在/opt/app/nginx/conf/目录中创建nginx.conf文件:
rm -f /opt/app/nginx/conf/nginx.conf
vim /opt/app/nginx/conf/nginx.conf
输入以下内容:
user  www www;

master_process on;

worker_processes 32;

#worker_cpu_affinity 0001 0010 0100 1000;

pid /var/run/nginx.pid;

# [ debug | info | notice | warn | error | crit ]
error_log  /opt/app/nginx/logs/nginx.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 51200;
}

http
{
      include       mime.types;
      default_type  application/octet-stream;
      server_names_hash_bucket_size 128;
      client_header_buffer_size 32k;
      large_client_header_buffers 4 32k;


      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;  
      sendfile       on;
      tcp_nopush     on;
      tcp_nodelay    on;

      keepalive_timeout 120;


      fastcgi_connect_timeout 300;
      fastcgi_send_timeout 300;
      fastcgi_read_timeout 300;
      fastcgi_buffer_size 128k;
      fastcgi_buffers 2 256k;
      fastcgi_busy_buffers_size 256k;
      fastcgi_temp_file_write_size 256k;
      fastcgi_intercept_errors on;
      fastcgi_temp_path /dev/shm/fastcgi_temp;
      send_timeout 60;


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

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

      client_max_body_size       10m;
      client_body_buffer_size    256k;
      client_body_temp_path      /dev/shm/client_body_temp;
      #
      proxy_temp_path            /dev/shm/proxy_temp;



      # The following includes are specified for virtual hosts
      include          vhosts/*.conf;
}
5、编辑配置文件(以下是模板,请根据实际情况更改文件名和内容,可有多个.conf文件存在,并同时使用,注意,.conf文件的权限应该为644)
cd /opt/app/nginx/conf/vhosts/
vim test.conf
server

      {

              listen       80;

              server_name 117.79.91.211;

              index index.html index.php index.htm;

              root  /opt/case/;  

              error_page 404 /error.html;

           #    log_format  www  '$remote_addr - $remote_user [$time_local] $request '
               #         '"$status" $body_bytes_sent "$http_referer" '
                #        '"$http_user_agent" "$http_x_forwarded_for"';

             # access_log /opt/app/nginx/logs/access-dinglingling.log www;

              # support php
              location ~ .*\.php?$
              {
                      include enable_php5.conf;
              }

      }
注:root /opt/case/ 这个目录就更改了nginx默认的存放网页html目录。
6、在/opt/app /nginx/conf/目录中创建enable_php5.conf文件 ,调用php程序:
vim /opt/app/nginx/conf/enable_php5.conf
输入以下内容:
fastcgi_pass  127.0.0.1:9000;
fastcgi_index index.php;

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;
7、启动Nginx
#ulimit -SHn 51200
#/opt/app/nginx/sbin/nginx
8、添加index.php,因为我们已经把页面的内容指向了case目录,所以要在case目录中建立index.php

#vim /opt/case/index.php

<?
phpinfo();

?>
在IE中访问http://配置机器的IP/index.php
关闭Nginx
killall nginx
四、配置开机自动启动Nginx + PHP

#vim /etc/rc.local
在末尾增加以下内容:
#ulimit -SHn 51200
/opt/app/php5/sbin/php-fpm start
/opt/app/nginx/sbin/nginx


本文出自 “linux” 博客,谢绝转载!

你可能感兴趣的:(Nginx+PHP+MySQL)