linux nginx+apache+php+mysql 安装配置

时间:2022-11-08 20:28:29
CentOS-5.6-i386操作系统
nginx-1.1.6.tar.gz
httpd-2.2.21.tar.gz
mysql-5.1.44.tar.gz
php-5.3.8.tar.gz
1. lsb_release -a 查看系统环境

LSB Version: :core-4.0-ia32:core-4.0-noarch:graphics-4.0-ia32:graphics-4.0-noarch:printing-4.0-ia32:printing-4.0-noarch
Distributor ID: CentOS
Description: CentOS release 5.6 (Final)
Release: 5.6
Codename: Final

2. 查找是否已经安装过环境

[root@localhost www]# rpm -qa|grep httpd
[root@localhost www]# rpm -qa|grep mysqld
[root@localhost www]# rpm -qa|grep php
[root@localhost www]# rpm -qa|grep apache

如果找到 删除  (--nodeps 是强制删除的意思)
[root@localhost ~]# rpm -qa|grep httpd
httpd-2.2.3-31.el5.centos
[root@localhost ~]# rpm -e --nodeps httpd	

3. 解压缩

[root@localhost www]# tar -zxf nginx-1.1.6.tar.gz 
[root@localhost www]# tar -zxf httpd-2.2.21.tar.gz 
[root@localhost www]# tar -zxf mysql-5.1.44.tar.gz 
[root@localhost www]# tar -zxf php-5.3.8.tar.gz 


4. 安装PHP所需的软件包

  所有的软件包可以到http://code.google.com/p/myphp-appstore/downloads/list 下载

  4.1  安装libiconv(iconv函数需要用到)

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

 4.2  安装jpegsrc(magejpeg需要用到) 

# tar zxvf jpegsrc.v6b.tar.gz 
# cd jpeg-6b/ 
# ./configure –-enable-static –-enable-shared 
# make 
# make install 
# make install-lib 
# cd ../

 4.3 安装libpng包(支持PNG)

# cd /root/Software/ 
# tar -zvxf libpng-1.5.4.tar.gz
# cd libpng-1.5.4
# ./configure --prefix=/usr/local/libpng
# make
# make install

4.4 安装freetype

# cd /root/Software/ 
# tar -zvxf freetype-2.4.6.tar.gz 
# cd freetype-2.4.6 
# mkdir -p /usr/local/freetype 
# ./configure --prefix=/usr/local/freetype 
# make
# make install

4.5  安装gettext(gd所需)

# tar zxvf gettext-0.17.tar.gz
# cd gettext-0.17
# ./configure
# make
# make install

 4.6 安装gd

# tar zxf gd-2.0.35.tar.gz 
# cd gd
# cd  2.0.35/ 
# ./configure --enable-m4_pattern_allow
# make 
# make install 
cd ../

 4.7  安装libssh(可能ssh连接用到)

# tar zxf libssh2-0.12.tar.gz 
# cd libssh2-0.12/ 
# ./configure 
# make 
# make install 

 4.8  安装libmcrypt(加密算法扩展库 例如 DES 3DES)

# 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 ../../

 4.9 安装mhash(哈希函数库 例如 MD5 SHA CRC 等) 

# tar zxvf mhash-0.9.9.9.tar.gz
# cd mhash-0.9.9.9/
# ./configure
# make
# make install
# cd ../

 4.10 建立软链接

# 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

 4.11 安装 mcrypt(加密算法扩展库) 

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

5 安装MySQL

# cd mysql-5.1.44
# ./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
# make && make install

  创建MySQL数据库,用默认的配置my.cnf 

# groupadd mysql
# useradd -g mysql mysql
# cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf
# /usr/local/mysql/bin/mysql_install_db --user=mysql
# chown -R mysql /usr/local/mysql/var
# chgrp -R mysql /usr/local/mysql/  

  添加MySQL启动服务,并且设置root密码

# cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysql
# chmod 755 /etc/init.d/mysql
# chkconfig --level 345 mysql on
# echo "/usr/local/mysql/lib/mysql" >> /etc/ld.so.conf
# echo "/usr/local/lib" >>/etc/ld.so.conf
# ldconfig
# ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
# ln -s /usr/local/mysql/include/mysql /usr/include/mysql
# service mysql start
# /usr/local/mysql/bin/mysqladmin -u root password root    //root改为你需要的密码
# service mysql restart

6  安装Apache 

# cd httpd-2.2.21
# ./configure --prefix=/usr/local/apache2 --enable-modules=so --enable-rewrite
# make && make install
# /usr/local/apache2/bin/apachectl  start    //启动apahce
错误:httpd: Could not reliably determine the server's fully qualified domain name,  using 127.0.0.1 for ServerName
解决办法:cd /usr/local/apache/conf,  用记事本打开httpd.conf,  将里面的#ServerName localhost:80注释去掉即可。
用浏览器查看 http://127.0.0.1http://localhost, 得到it works,说明apache已经配置成功了.

7 安装PHP

# ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs --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=/usr/local/freetype --with-jpeg-dir --with-png-dir=/usr/local/libpng --with-zlib --with-libxml-dir=/usr --enable-xml --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curlwrappers --enable-mbregex --enable-soap --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-ftp 
# make ZEND_EXTRA_LIBS='-liconv' (必须要加,要不然PHP编译出错)
# make install
# cp php.ini-development /usr/local/php/etc/php.ini (好象PHP5.3开始就没有php.ini-dist文件了,可以是php.ini-development或php.ini-production)
# cd ../

8 安装PHP扩展模块
   8.1 memcache 客户端

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

   8.2 PDO_MySQL

# 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 ../

  8.3 安装eaccelerator (PHP加速器)

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

9 修改PHP.INI 文件

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

10 配置eAccelerator加速

PHP创建缓存目录
mkdir -p /usr/local/eaccelerator_cache
在php.ini文件最后面添加下面几行内容就可以了
[eaccelerator]
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/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"

11 重新配置apache2让他支持php

在LoadModule php5_module modules/libphp5.so
添加AddType application/x-httpd-php  .php
OK,基本的安装已经完成.
重新起动APACHE:
# /usr/local/apache2/bin/apachectl  graceful   ---记得每次修改php.ini配置或httpd.conf 配置都要重启Apache

12  Apache配置httpd-vhosts虚拟主机

   12.1 修改httpd.conf 文件

    (a)  添加如下两行(假设当前是基于同一IP的两个端口)

       Listen 8080
       Listen 8081

     (b) 注释以下内容

      #ServerAdmin you@example.com

      #DocumentRoot "/usr/local/apache2/htdocs"

     #<Directory "/usr/local/apache2/htdocs">

     (c) 修改Directory模块内容(相当是全局的,以后httpd-vhosts.conf也会用到这个设置)

      <Directory />
        Options FollowSymLinks
         AllowOverride None
        Order deny,allow
          Deny from all    (将 Deny 改成 Allow)
       </Directory>

     (c)  找到如下模块             

        # Virtual hosts
       #Include conf/extra/httpd-vhosts.conf
     去掉前面的#,这样就开启了httpd-vhosts虚拟主机文件。

   12.2  修改httpd-vhosts文件

NameVirtualHost *:8080
NameVirtualHost *:8081
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:8080>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "/home/www/htdocs/8080"
    ServerName  test.example.com
    ServerAlias www.dummy-host.example.com
    ErrorLog "/home/www/logs/8080-error.log"
    CustomLog "/home/www/logs/8080-access.log" common
</VirtualHost>
<VirtualHost *:8081>
    ServerAdmin webmaster@dummy-host2.example.com
    DocumentRoot "/home/www/htdocs/8081"
    ServerName  test2.example.com
    ErrorLog "/home/www/logs/8081-error.log"
    CustomLog "/home/www/logs/8081-access.log" common
</VirtualHost>

13  查看配置是否正确并重启apache

 /usr/local/apache2/bin/apachectl -S    --检查配置文件的语法

# /usr/local/apache2/bin/apachectl graceful

或者

# /usr/local/apache2/bin/apachectl -k restart

前一种方法不会中断当前的客户端连接,后一种方法则会中断当前客户端的连接
如果配置没有什么问题,到此就可以http://127.0.0.1:8080http://127.0.0.1:80801 访问虚拟机的web页面了。

接下来要做的事情就是通过nginx 反向代理php请求

14  安装nginx所需软件包

tar zxvf pcre-8.01.tar.gz
cd pcre-8.01/
./configure
make && make install
cd ../

15 安装nginx

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

16 nginx做负载均衡(实现动静分离),其详细配置如下:

user  www www;
worker_processes  8;

error_log  logs/error.log;
pid        logs/nginx.pid;

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 64k;
  large_client_header_buffers 4 128k;
  client_max_body_size 8m;

  tcp_nopush     on;
  keepalive_timeout 60;
  tcp_nodelay on;

  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;

  upstream  backphp_service
  {
      server 192.168.52.246:8080 weight=1 max_fails=2 fail_timeout=180s;
      server 192.168.52.246:8081 weight=1 max_fails=2 fail_timeout=180s;
  }

 server
    {
        listen       80;
        server_name  test.nginx.sdo.com;
        root  /home/www/static;

        location ~ .+\.php($|/)
        {
           proxy_set_header   Host             $host;
           proxy_set_header   X-Real-IP        $remote_addr;
           proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
           proxy_pass        http://backphp_service;
        }

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

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

    }

}


17 启动nginx 进程

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

18  配置开机自启动
echo "ulimit -SHn 65535" >>/etc/rc.local
echo "/usr/local/apache2/bin/apachectl  -k start" >>/etc/rc.local
echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local
 

到这里工作已经全部完成!