一步一步教你安装 Nginx+PHP+Mysql

 发现很多VPS用LNMP一键安装包都失败,有不能排错,唯有给出教程,一步一步教你安装,所谓授人于鱼不如授人于渔嘛,本文是在CentOS 5.4 操作系统下测试通过的,用的是DiaHosting上的32位系统,为什么要用32位系统?就是为了节省内存,VPS内存都不是很多的,纯32位系统比X86_X64兼用32位系统更省内存.

安装步骤:

1,升级系统和安装相关的程序库
01    yum -y install yum-fastestmirror
02    yum -y update
03    yum -y install patch make gcc gcc-c++ gcc-g77 flex bison
04    yum -y install libtool libtool-libs kernel-devel autoconf
05    yum -y install libjpeg libjpeg-devel libpng libpng-devel
06    yum -y install freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel
07    yum -y install glib2 glib2-devel bzip2 diff*
08    yum -y install bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs
09    yum -y install e2fsprogs-devel krb5 krb5-devel libidn libidn-devel
10    yum -y install openssl openssl-devel vim-minimal
11    yum -y install fonts-chinese scim-chewing scim-pinyin scim-tables-chinese


2,下载相关程序源码包
01    wget http://catlnmp.googlecode.com/files/libiconv-1.13.1.tar.gz
02    wget http://catlnmp.googlecode.com/files/libmcrypt-2.5.8.tar.gz
03    wget http://catlnmp.googlecode.com/files/mhash-0.9.9.9.tar.gz
04    wget http://catlnmp.googlecode.com/files/mcrypt-2.6.8.tar.gz
05    wget http://catlnmp.googlecode.com/files/mysql-5.1.44.tar.gz
06    wget http://php-fpm.org/downloads/php-5.2.13-fpm-0.5.13.diff.gz
07    wget http://www.sfr-fresh.com/unix/www/php-5.2.13.tar.gz
08    wget http://catlnmp.googlecode.com/files/memcache-2.2.5.tgz
09    wget http://catlnmp.googlecode.com/files/PDO_MYSQL-1.0.2.tgz
10    wget http://catlnmp.googlecode.com/files/eaccelerator-0.9.6.tar.bz2
11    wget http://catlnmp.googlecode.com/files/ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz
12    wget http://catlnmp.googlecode.com/files/ZendOptimizer-3.3.9-linux-glibc23-x86_64.tar.gz
13    wget http://catlnmp.googlecode.com/files/pcre-8.01.tar.gz
14    wget http://catlnmp.googlecode.com/files/nginx-0.7.65.tar.gz
15    wget http://catlnmp.googlecode.com/files/phpMyAdmin-3.2.4-all-languages.tar.gz
16    wget http://catlnmp.googlecode.com/files/index.php

3, 开始安装,先安装PHP需要的库程序
01    tar zxvf libiconv-1.13.1.tar.gz
02    cd libiconv-1.13.1/
03    ./configure --prefix=/usr/local
04    make
05    make install
06    cd ../
07     
08    tar zxvf libmcrypt-2.5.8.tar.gz
09    cd libmcrypt-2.5.8/
10    ./configure
11    make
12    make install
13    /sbin/ldconfig
14    cd libltdl/
15    ./configure --enable-ltdl-install
16    make
17    make install
18    cd ../../
19     
20    tar zxvf mhash-0.9.9.9.tar.gz
21    cd mhash-0.9.9.9/
22    ./configure
23    make
24    make install
25    cd ../
26     
27    ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
28    ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
29    ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
30    ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
31    ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
32    ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
33    ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
34    ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
35    ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
36    ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
37     
38    tar zxvf mcrypt-2.6.8.tar.gz
39    cd mcrypt-2.6.8/
40    ./configure
41    make
42    make install
43    cd ../

4, 安装mysql
1    tar -zxvf mysql-5.1.44.tar.gz
2    cd mysql-5.1.44
3    ./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
4    make && make install
5    cd ../

创建MySQL数据库,用默认的配置my.cnf
1    groupadd mysql
2    useradd -g mysql mysql
3    cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf
4    /usr/local/mysql/bin/mysql_install_db --user=mysql
5    chown -R mysql /usr/local/mysql/var
6    chgrp -R mysql /usr/local/mysql/.

添加Mysql启动服务,并且设置root密码
01    cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysql
02    chmod 755 /etc/init.d/mysql
03    chkconfig --level 345 mysql on
04    echo "/usr/local/mysql/lib/mysql" >> /etc/ld.so.conf
05    echo "/usr/local/lib" >>/etc/ld.so.conf
06    ldconfig
07    ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
08    ln -s /usr/local/mysql/include/mysql /usr/include/mysql
09    service mysql start
10    /usr/local/mysql/bin/mysqladmin -u root password root    //root改为你需要的密码
11    service mysql restart

5, 安装PHP(FastCGI模式)
1    tar zxvf php-5.2.13.tar.gz
2    gzip -cd php-5.2.13-fpm-0.5.13.diff.gz | patch -d php-5.2.13 -p1
3    cd php-5.2.13/
4    ./buildconf --force
5    ./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-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-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-ftp
6    make ZEND_EXTRA_LIBS='-liconv'
7    make install
8    cp php.ini-dist /usr/local/php/etc/php.ini
9    cd ../

6, 安装PHP扩展模块
01    tar zxvf memcache-2.2.5.tgz
02    cd memcache-2.2.5/
03    /usr/local/php/bin/phpize
04    ./configure --with-php-config=/usr/local/php/bin/php-config
05    make
06    make install
07    cd ../
08     
09    tar zxvf PDO_MYSQL-1.0.2.tgz
10    cd PDO_MYSQL-1.0.2/
11    /usr/local/php/bin/phpize
12    ./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql
13    make
14    make install
15    cd ../
16     
17    tar jxvf eaccelerator-0.9.6.tar.bz2
18    cd eaccelerator-0.9.6/
19    /usr/local/php/bin/phpize
20    ./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
21    make
22    make install
23    cd ../

安装Zend Optimizer,32位系统版本
1    tar zxvf ZendOptimizer-3.3.9-linux-glibc23-i386.tar.gz
2    mkdir -p /usr/local/zend/
3    cp ZendOptimizer-3.3.9-linux-glibc23-i386/data/5_2_x_comp/ZendOptimizer.so /usr/local/zend/

如果是64 位系统,则
1    tar zxvf ZendOptimizer-3.3.9-linux-glibc23-x86_64.tar.gz
2    mkdir -p /usr/local/zend/
3    cp ZendOptimizer-3.3.9-linux-glibc23-x86_64/data/5_2_x_comp/ZendOptimizer.so /usr/local/zend/

配置 php.ini
1    cat >>/usr/local/php/etc/php.ini< <EOF
2    [Zend Optimizer]
3    zend_optimizer.optimization_level=1
4    zend_extension="/usr/local/zend/ZendOptimizer.so"
5    EOF

7, 修改php.ini文件
手工修改:查找/usr/local/php/etc/php.ini中的 extension_dir = "./"
修改为extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
并在此行后增加以下几行,然后保存:
extension = "memcache.so"
extension = "pdo_mysql.so"

再查找 output_buffering = Off
修改为output_buffering = On

自动修改:可执行以下shell命令,自动完成对php.ini文件的修改:
[/code]
1    sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"/nextension = "memcache.so"/nextension = "pdo_mysql.so"/n#' /usr/local/php/etc/php.ini
2    sed -i 's#output_buffering = Off#output_buffering = On#' /usr/local/php/etc/php.ini

8, 配置eAccelerator加速PHP:
创建缓存目录
1    mkdir -p /usr/local/eaccelerator_cache

配置php.ini
01    cat >>/usr/local/php/etc/php.ini< <EOF
02    [eaccelerator]
03    zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
04    eaccelerator.shm_size="1"
05    eaccelerator.cache_dir="/usr/local/eaccelerator_cache"
06    eaccelerator.enable="1"
07    eaccelerator.optimizer="1"
08    eaccelerator.check_mtime="1"
09    eaccelerator.debug="0"
10    eaccelerator.filter=""
11    eaccelerator.shm_max="0"
12    eaccelerator.shm_ttl="3600"
13    eaccelerator.shm_prune_period="3600"
14    eaccelerator.shm_only="0"
15    eaccelerator.compress="1"
16    eaccelerator.compress_level="9"
17    eaccelerator.keys = "disk_only"
18    eaccelerator.sessions = "disk_only"
19    eaccelerator.content = "disk_only"
20    EOF

9, 创建www用户和组,以及主机需要的目录,日志目录
[/code]
1    groupadd www
2    useradd -g www www
3    mkdir -p /home/www
4    chmod +w /home/www
5    mkdir -p /home/www/logs
6    chmod 777 /home/www/logs
7    chown -R www:www /home/www

10, 创建php-fpm配置文件
1    rm -f /usr/local/php/etc/php-fpm.conf
2    vi /usr/local/php/etc/php-fpm.conf

输入以下内容,我设置开的进程是5个.需要更改进程数,可以修改5
001    <? xml version="1.0" ?>
002    <configuration>
003        All relative paths in this config are relative to php's install prefix
004        <section name="global_options">
005            Pid file
006            <value name="pid_file">/usr/local/php/logs/php-fpm.pid</value>
007            Error log file
008            <value name="error_log">/home/www/logs/php-fpm.log</value>
009            Log level
010            <value name="log_level">notice</value>
011            When this amount of php processes exited with SIGSEGV or SIGBUS ...
012            <value name="emergency_restart_threshold">10</value>
013            ... in a less than this interval of time, a graceful restart will be initiated.
014            Useful to work around accidental curruptions in accelerator's shared memory.
015            <value name="emergency_restart_interval">1m</value>
016            Time limit on waiting child's reaction on signals from master
017            <value name="process_control_timeout">5s</value>
018            Set to 'no' to debug fpm
019            <value name="daemonize">yes</value>
020        </section>
021        <workers>
022            <section name="pool">
023                Name of pool. Used in logs and stats.
024                <value name="name">default</value>
025                Address to accept fastcgi requests on.
026                Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket'
027                <value name="listen_address">127.0.0.1:9000</value>
028                <value name="listen_options">
029                    Set listen(2) backlog
030                    </value><value name="backlog">-1</value>
031                    Set permissions for unix socket, if one used.
032                    In Linux read/write permissions must be set in order to allow connections from web server.
033                    Many BSD-derrived systems allow connections regardless of permissions.
034                    <value name="owner">www</value>
035                    <value name="group">www</value>
036                    <value name="mode">0666</value>
037     
038                Additional php.ini defines, specific to this pool of workers.
039                <value name="php_defines">
040            <!--     <value name="sendmail_path">/usr/sbin/sendmail -t -i</value>        -->
041            <!--     <value name="display_errors">0                                -->
042     
043                Unix user of processes
044            <value name="user">www</value>
045                Unix group of processes
046            <value name="group">www</value>
047                Process manager settings
048                <value name="pm">
049                    Sets style of controling worker process count.
050                    Valid values are 'static' and 'apache-like'
051                    </value><value name="style">static</value>
052                    Sets the limit on the number of simultaneous requests that will be served.
053                    Equivalent to Apache MaxClients directive.
054                    Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi
055                    Used with any pm_style.
056                    <value name="max_children">5</value>
057                    Settings group for 'apache-like' pm style
058                    <value name="apache_like">
059                        Sets the number of server processes created on startup.
060                        Used only when 'apache-like' pm_style is selected
061                        </value><value name="StartServers">20</value>
062                        Sets the desired minimum number of idle server processes.
063                        Used only when 'apache-like' pm_style is selected
064                        <value name="MinSpareServers">5</value>
065                        Sets the desired maximum number of idle server processes.
066                        Used only when 'apache-like' pm_style is selected
067                        <value name="MaxSpareServers">35</value>
068     
069                The timeout (in seconds) for serving a single request after which the worker process will be terminated
070                Should be used when 'max_execution_time' ini option does not stop script execution for some reason
071                '0s' means 'off'
072                <value name="request_terminate_timeout">0s</value>
073                The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file
074                '0s' means 'off'
075                <value name="request_slowlog_timeout">0s</value>
076                The log file for slow requests
077                <value name="slowlog">logs/slow.log</value>
078                Set open file desc rlimit
079                <value name="rlimit_files">65535</value>
080                Set max core size rlimit
081                <value name="rlimit_core">0</value>
082                Chroot to this directory at the start, absolute path
083                <value name="chroot"></value>
084                Chdir to this directory at the start, absolute path
085                <value name="chdir"></value>
086                Redirect workers' stdout and stderr into main error log.
087                If not set, they will be redirected to /dev/null, according to FastCGI specs
088                <value name="catch_workers_output">yes</value>
089                How much requests each process should execute before respawn.
090                Useful to work around memory leaks in 3rd party libraries.
091                For endless request processing please specify 0
092                Equivalent to PHP_FCGI_MAX_REQUESTS
093                <value name="max_requests">102400</value>
094                Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect.
095                Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi (5.2.2+)
096                Makes sense only with AF_INET listening socket.
097                <value name="allowed_clients">127.0.0.1</value>
098                Pass environment variables like LD_LIBRARY_PATH
099                All $VARIABLEs are taken from current environment
100                <value name="environment">
101                    </value><value name="HOSTNAME">$HOSTNAME</value>
102                    <value name="PATH">/usr/local/bin:/usr/bin:/bin</value>
103                    <value name="TMP">/tmp</value>
104                    <value name="TMPDIR">/tmp</value>
105                    <value name="TEMP">/tmp</value>
106                    <value name="OSTYPE">$OSTYPE</value>
107                    <value name="MACHTYPE">$MACHTYPE</value>
108                    <value name="MALLOC_CHECK_">2</value>
109     
110            </section>
111        </workers>
112    </configuration>

11, 启动php-cgi进程,监听127.0.0.1的9000端口,进程数为5,用户为www
1    ulimit -SHn 65535
2    /usr/local/php/sbin/php-fpm start

注:/usr /local/php/sbin/php-fpm还有其他参数,包 括:start|stop|quit|restart|reload|logrotate,修改php.ini后不重启php-cgi,重新加载配置文件 使用reload

12,安装Nginx
01    tar zxvf pcre-8.01.tar.gz
02    cd pcre-8.01/
03    ./configure
04    make && make install
05    cd ../
06     
07    tar zxvf nginx-0.7.65.tar.gz
08    cd nginx-0.7.65/
09    ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module
10    make && make install
11    cd ../

13, 创建Nginx配置文件
1    mkdir -p /usr/local/nginx/conf/servers
2    rm -f /usr/local/nginx/conf/nginx.conf
3    vi /usr/local/nginx/conf/nginx.conf

输入以下内容:
01    user  www www;
02    worker_processes 1;
03    error_log  /home/www/logs/nginx_error.log  crit;
04    pid        /usr/local/nginx/nginx.pid;
05    #Specifies the value for maximum file descriptors that can be opened by this process.
06    worker_rlimit_nofile 65535;
07    events
08        {
09            use epoll;
10            worker_connections 65535;
11        }
12    http
13        {
14            include       mime.types;
15            default_type  application/octet-stream;
16            #charse  gb2312;
17            server_names_hash_bucket_size 128;
18            client_header_buffer_size 128k;
19            large_client_header_buffers 4 256k;
20            client_max_body_size 8m;
21            sendfile on;
22            tcp_nopush     on;
23            keepalive_timeout 60;
24            tcp_nodelay on;
25     
26            fastcgi_connect_timeout 300;
27            fastcgi_send_timeout 300;
28            fastcgi_read_timeout 300;
29            fastcgi_buffer_size 64k;
30            fastcgi_buffers 4 64k;
31            fastcgi_busy_buffers_size 128k;
32            fastcgi_temp_file_write_size 128k;
33     
34            gzip on;
35            gzip_min_length  1k;
36            gzip_buffers     4 16k;
37            gzip_http_version 1.1;
38            gzip_comp_level 9;
39            gzip_types       text/plain application/x-javascript text/css application/xml;
40            gzip_vary on;
41            output_buffers   4 32k;
42            postpone_output  1460;
43     
44            #limit_zone  crawler  $binary_remote_addr  10m;
45     
46    server
47        {
48            listen       80;
49            server_name vps.imcat.in;
50            index index.html index.htm index.php;
51            include location.conf;
52            root  /home/www;
53        }
54            include servers/*;
55    }

14, 在/usr/local/nginx/conf/目录中创建location.conf文件:
1    vi /usr/local/nginx/conf/location.conf

输入内容:
01    location ~ .*/.(php|php5)?$
02        {
03        #fastcgi_pass  unix:/tmp/php-cgi.sock;
04        fastcgi_pass  127.0.0.1:9000;
05        fastcgi_index index.php;
06        include fastcgi.conf;
07        }
08     
09    location ~ .*/.(gif|jpg|jpeg|png|bmp|swf)$
10        {
11            expires      30d;
12        }
13     
14    location ~ .*/.(js|css)?$
15        {
16            expires      12h;
17        }

多站点管理,可以在 /usr/local/nginx/conf/servers目录添加配置文件,格式为:
1    vi /usr/local/nginx/conf/servers/imcat.conf

内容:
1    server
2     
3        {
4            listen       80;
5            server_name imcat.in;
6            index index.html index.htm index.php;
7            root  /home/www/imcat;
8        }

请 注意,我是没有开启 Nginx日志记录功能的.
启动Nginx:
1    ulimit -SHn 65535
2    /usr/local/nginx/sbin/nginx

放个探针看看
1    mv index.php /home/www/

访问你的IP看看吧!

15,安装phpMyAdmin,管理Mysql数据库
1    tar zxvf phpMyAdmin-3.2.4-all-languages.tar.gz
2    mv phpMyAdmin-3.2.4-all-languages /home/www/phpmyadmin

16, 配置开机自动启动Nginx + PHP
1    echo "ulimit -SHn 65535" >>/etc/rc.local
2    echo "/usr/local/php/sbin/php-fpm start" >>/etc/rc.local
3    echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local

17, 优化Linux内核参数(我只在Xen VPS用过,Openvz VPS失败,慎用)
1    vi /etc/sysctl.conf

在最后加入
01    # Add
02    net.ipv4.tcp_max_syn_backlog = 65536
03    net.core.netdev_max_backlog =  32768
04    net.core.somaxconn = 32768
05     
06    net.core.wmem_default = 8388608
07    net.core.rmem_default = 8388608
08    net.core.rmem_max = 16777216
09    net.core.wmem_max = 16777216
10     
11    net.ipv4.tcp_timestamps = 0
12    net.ipv4.tcp_synack_retries = 2
13    net.ipv4.tcp_syn_retries = 2
14     
15    net.ipv4.tcp_tw_recycle = 1
16    #net.ipv4.tcp_tw_len = 1
17    net.ipv4.tcp_tw_reuse = 1
18     
19    net.ipv4.tcp_mem = 94500000 915000000 927000000
20    net.ipv4.tcp_max_orphans = 3276800
21     
22    #net.ipv4.tcp_fin_timeout = 30
23    #net.ipv4.tcp_keepalive_time = 120
24    net.ipv4.ip_local_port_range = 1024  65535

使配置立即生效:
1    /sbin/sysctl -p

18, 需要安装ftp的,可以简单安装vsftpd应用:
1    yum -y install vsftpd
2    /etc/init.d/vsftpd start
3    chkconfig --level 345 vsftpd on

19, 请务必更改www用户密码:
1    passwd www

全文完!欢迎抓错误!

你可能感兴趣的:(nginx,PHP,mysql,Zend,extension,permissions)