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 –p /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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章