搭建Linux + Nginx + PHP(fastcgi) + Mysql

1、编译php扩展模块

freetype- 2.1.10/
    ./configure  && make && make install

libevent-1.4.11-stable/
    ./configure && make && make install

libpng-1.2.37
    ./configure  --with-binconfigs=/usr/bin/pkg-config --with-pkgconfigdir=/usr && make && make install

libmcrypt-2.5.8 
    ./configure  --enable-static && make && make install
    ldconfig
    cd libltdl/  ####<----
这个注意必须编译,否这php编译报错
    ./configure --enable-ltdl-install
    ldconfig

mhash-0.9.9
    ./configure  && make && make install


jpeg-6b
    ./configure --enable-shared --enable-static
    mkdir -p /usr/local/man/man1/
    make && make install

libxml2-2.7.3  
    ./configure --with-history --enable-ipv6=no
    make && make install

shell> vim /etc/ld.so.conf
/usr/local/lib

shell> ldconfig


mcrypt-2.6.7
    ./configure && make && make install

libiconv-1.12./configure --prefix=/usr/local && make && make install
    ldconfig
gd-2.0.35
    ./configure --with-libiconv-prefix=/usr/local --with-png=/usr/local --with-freetype=/usr/local --with-fontconfig=/lib --with-jpeg=/usr/local
    make && make install
    ldconfig

确保以下几个包安装
   
shell>  yum install crul-devel ncurses-devel net-snmp-devel -y

shell> vim ~/.bash_profile
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/usr/local/mysql/libexec

# service mysql5 start


# tar xvf pcre-8.01.tar.gz  -C /usr/src
# cd /usr/src/pcre-8.01
# ./configure  && make && make install

安装MySQL-devel-community-5.1.39-0.rhel5.i386.rpm
不安装的话,编译php 会提示找不到mysql 文件头的 ,如果出现
Preparing...                ########################################### [100%]
        file /usr/bin/mysql_config from install of MySQL-devel-community-5.1.39-0.rhel5.i386 conflicts with file from package mysql-5.0.77-4.el5_4.1.i386
        file /usr/share/man/man1/mysql_config.1.gz from install of MySQL-devel-community-5.1.39-0.rhel5.i386 conflicts with file from package mysql-5.0.77-4.el5_4.1.i386
-------------------------------------------
说明跟5.077那个包冲突,要现删掉,或者安装加上 --froce参数,强制安装。
rpm -ivh MySQL-devel-community-5.1.39-0.rhel5.i386.rpm --force


开始编译php

# tar xvf php-5.2.13.tar.gz -C /usr/src
# gzip -cd php-5.2.13-fpm-0.5.13.diff.gz | patch -d /usr/src/php-5.2.13/ -p1
# cd /usr/src/php-5.2.13/

    ./configure  --sysconfdir=/usr/local/php  --with-mcrypt --with-mhash --with-mysql --with-mysqli --with-mysql-sock=/usr/local/mysql/sock/mysql.sock  --enable-sockets --enable-zip --enable-mbstring --with-openssl --with-config-file-path=/usr/local/lib/ --with-gd --enable-gd-native-ttf --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-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-pcntl --with-ldap --with-ldap-sasl --with-xmlrpc --enable-soap --enable-fpm --enable-force-cgi-redirect --enable-fastcgi

   
    make ZEND_EXTRA_LIBS='-liconv'
    make install


继续php第三方模块

加速模块
tar xvf eaccelerator-0.9.6.tar.bz2 -C /usr/src
cd /usr/src/eaccelerator-0.9.6/
/usr/local/bin/phpize
./configure  --with-php-config=/usr/local/bin/php-config --enable-eaccelerator=shared
make && make install

安装完毕,注意一下输出
Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-non-zts-20060613/

缓存模块
tar xvf memcache-2.2.5.tgz  -C /usr/src
cd /usr/src/memcache-2.2.5/
/usr/local/bin/phpize
./configure --with-php-config=/usr/local/bin/php-config
make && make install

Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-non-zts-20060613/


tar xvf ImageMagick.tar.gz  -C /usr/src
cd /usr/src/ImageMagick-6.5.1-2/
./configure && make && make install

tar xvf imagick-2.3.0.tgz  -C /usr/src
cd /usr/src/imagick-2.3.0/
/usr/local/bin/phpize
./configure --with-php-config=/usr/local/bin/php-config
make && make install

Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-non-zts-20060613/



tar xvf PDO_MYSQL-1.0.2.tgz -C /usr/src
cd /usr/src/PDO_MYSQL-1.0.2/
/usr/local/bin/phpize
./configure --with-php-config=/usr/local/bin/php-config --with-pdo-mysql=/usr/local/mysql
make && make install

Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-non-zts-20060613/

# vim /etc/ld.so.conf
/usr/local/lib/php/extensions/no-debug-non-zts-20060613/
# ldconfig


配置php的配置文件
# cp /usr/src/php-5.2.13/php.ini-dist /usr/local/lib/php.ini
# vim /usr/local/lib/php.ini
extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20060613/"
extension = "imagick.so"
extension = "memcache.so"
extension = "pdo_mysql.so"

output_buffering = On
max_execution_time = 30 <---
根据应用程序实际调整
memory_limit = 128M  <---
根据应用程序实际调整


# shift + g
来到配置文件的底部添加:
[eaccelerator]
zend_extension="/usr/local/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so" 
《--根据编译获得路径修改
eaccelerator.shm_size="64"
eaccelerator.cache_dir="/wwwroot/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"



# mkdir -p /wwwroot/eaccelerator_cache
# chmod 777  /wwwroot/eaccelerator_cache


=========================================

安装nginx

# tar xvf nginx-0.8.46.tar.gz  -C /usr/src 
# ./configure --user=daemon --group=daemon --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
 

 

 

参数 --with-http_stub_status_module 是为了启用 nginxNginxStatus 功能,用来监控 Nginx 的当前状态。

注意以下输出:
  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"


配置php-fpm配置文件
# vim /usr/local/etc/php-fpm.conf
.....
                <!--    <value name="listen_address">127.0.0.1:9000</value> -->
                        <value name="listen_address">/var/run/fastcgi/fastcgi.socket</value>

.....

 <value name="sendmail_path">/usr/sbin/sendmail -t -i</value>           
                                <value name="display_errors">0</value>
....

                        Unix user of processes
                        <value name="user">daemon</value>                              

                        Unix group of processes
                        <value name="group">daemon</value>

<value name="max_children">64</value>
<value name="rlimit_files">65535</value>
<value name="rlimit_core">65535</value>
<value name="max_requests">2048</value>


# mkdir -p /var/run/fastcgi/
# chown daemon:daemon !$
# chmod 777 !$



修改nginx配置文件
# vim /usr/local/nginx/conf/nginx.conf

user  daemon;
worker_processes  4;

error_log  logs/error.log  info;

pid        logs/nginx.pid;

worker_rlimit_nofile 65535;

events {
    use epoll;
    worker_connections  65535;
}


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;
    client_max_body_size  8m;

    sendfile        on;
    tcp_nopush     on;

    keepalive_timeout  65;

    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;

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

    server {
        listen       10.1.1.21:80;
        server_name  10.1.1.21;
        root /wwwroot;
        index index.htm index.html index.php;
    error_page  404   =200     /404.html;  ####
        location ~ .*\.php$ {                         <----
这个加上就可以支持PHP文件,可以放到server外。
            fastcgi_pass   unix:/var/run/fastcgi/fastcgi.socket;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;  ###
            include        fastcgi_params;
        }
#注意,如果php-fpm配置文件配置是端口9000,上面需改成:fastcgi_pass 127.0.0.1:9000; 
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
                expires 7d;
        }
        location ~ .*\.(js|css)$
        {
                expires 1d;
        }


    }

}


启动php fastcgi进程

# /usr/local/sbin/php-fpm start
Starting php_fpm  done

启动nginx
/usr/local/nginx/sbin/nginx

修改了配置文件
kill -HUP  #nginx-pid              
《----重新加载。

停止nginx
/usr/local/nginx/sbin/nginx -s stop

重载配置文件之前先测试一下正确性
/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf


基于IP的虚拟主机

绑定多个IP

server
{
        listen       10.1.1.21:80;
        server_name  10.1.1.21;
    root /wwwroot/;

}

server
{
        listen       10.1.1.22:80;
        server_name  10.1.1.22;
    root /wwwroot/bbs;
}

基于端口的虚拟主机

server
{
        listen       10.1.1.21:80;
        server_name  10.1.1.21;
    root /wwwroot/;

}

server
{
        listen       10.1.1.21:8080;  # http://10.1.1.21:8080/
        server_name  10.1.1.21;
    root /wwwroot/bbs;
}

基于域名的虚拟主机

server
{
        listen       80;
        server_name  www.dogdogcom.com;
    root /wwwroot/;

}

server
{
        listen       80; 
        server_name  bbs.dogdogcom.com;
    root /wwwroot/bbs;
}


访问控制
server
{
        listen       80; 
        server_name  bbs.dogdogcom.com;
    root /wwwroot/bbs;
        location / {
                deny 10.1.1.18;
                allow all;
        }
}


要求:
    1
、如果想同时对多个网站生效,访问控制应该怎么写?
       
放到全局位置
http {
    deny 10.1.1.21;
    server { }
    server { }
}

    2
、如果只想对网站的某个目录进行访问控制,怎么写?
http {
    server {
        location /uc {
                deny 10.1.1.20;
        }
    }
}

访问日志记录,日志轮换
server {
        log_format  main  '$remote_addr - $remote_user [$time_local]  '
: '"$request" $status $bytes_sent '
: '"$http_referer" "$http_user_agent" "$gzip_ratio"';

        access_log  logs/www.dogdogcom.com.access.log  main;
}


# vim /etc/logrotate.conf
/usr/local/nginx/logs/www.dogdogcom.com.access.log
{
    daily
    size 1M
    create 0644 root root
    rotate 3
    sharedscripts
    postrotate
        /bin/kill -HUP `cat /usr/local/nginx/logs/nginx.pid 2> /dev/null` 2> /dev/null || true
    endscript
}

访问验证的实现

        location = / {
                auth_basic "Nginx_Panel";
                auth_basic_user_file "www.password";
                deny 10.1.1.18;
                allow all;
        }


# htpasswd -c ./www.password tom

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

你可能感兴趣的:(nginx,linux,mysql,数据库,休闲)