centos6.4(64位)安装nginx1.7.1+php-5.5.13+mysql-5.5.25

本文所用的所有安装包下载:http://pan.baidu.com/s/1zWTDc

第一步:安装所需依赖包

yum -y install gcc gcc-c++ autoconf cmake 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 gd gd2 gd-devel gd2-devel perl-CPAN

第二步:安装mysql-5.5.25

    创建mysql用户和组

groupadd mysql
useradd -g mysql -s /usr/sbin/nologin mysql
mkdir -p /data/mysql/data
mkdir /data/logs
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /data/mysql

    编译安装mysql

tar zxvf mysql-5.5.25.tar.gz
cd mysql-5.5.25
cmake  -DCMAKE_INSTALL_PREFIX=/usr/local/mysql  
-DMYSQL_DATADIR=/data/mysql/data  -DDEFAULT_CHARSET=utf8  
-DDEFAULT_COLLATION=utf8_unicode_ci  -DWITH_READLINE=1  
-DWITH_SSL=system  -DWITH_EMBEDDED_SERVER=1  -DENABLED_LOCAL_INFILE=1  
-DDEFAULT_COLLATION=utf8_general_ci  -DWITH_MYISAM_STORAGE_ENGINE=1  
-DWITH_INNOBASE_STORAGE_ENGINE=1  -DWITH_DEBUG=0
make && make install

    配置开机自启动

cp ./support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on

    添加MySQL的软链接以适应init脚本

ln -sv /usr/local/mysql/bin/mysql  /usr/sbin/mysql
ln -sv /usr/local/mysql/bin/mysqladmin  /usr/sbin/mysqladmin
ln -sv /usr/local/mysql/bin/mysqldump  /usr/sbin/mysqldump

    修改配置文件

cp ./support-files/my-medium.cnf /etc/my.cnf
vi /etc/my.cnf
# 输入以下内容(可以先清空默认内容):
[mysqld]
datadir=/data/mysql/data
socket=/tmp/mysql.sock
user=mysql
symbolic-links=0
[mysqld_safe]
log-error=/data/mysql/logs/mysqld.log
pid-file=/data/mysql/mysqld.pid
user=mysql
tmpdir=/tmp

    初始化数据库

/usr/local/mysql/scripts/mysql_install_db  --user=mysql  --basedir=/usr/local/mysql  --datadir=/data/mysql/data

    启动mysql

service mysqld start  # 或 /etc/init.d/mysqld start

    进入mysql(直接回车即可进入mysql)

/usr/local/mysql/bin/mysql

    输入以下SQL语句,创建一个具有root权限的用户(admin)和密码(12345678)

GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost' IDENTIFIED BY '12345678';
GRANT ALL PRIVILEGES ON *.* TO 'admin'@'127.0.0.1' IDENTIFIED BY '12345678';
DELETE FROM user WHERE User!=‘admin’; #删除原先默认的用户,仅保留新建的admin用户

第三步:安装nginx1.7.1

    添加www用户和组、创建网站虚拟目录

groupadd www
useradd -g www -s /usr/sbin/nologin www
mkdir -p /data/htdocs/www
chmod +w /data/htdocs/www
chown -R www:www /data/htdocs/www

    安装Nginx所需的pcre库

tar zxvf pcre-8.33.tar.gz
cd pcre-8.33
./configure
make && make install
ln -s /usr/local/lib/libpcre.so.1 /usr/lib64/libpcre.so.1
cd ../

    安装nginx1.7.1

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

    创建nginx日志目录

mkdir -p /data/logs
chmod +w /data/logs
chown -R www:www /data/logs

    创建Nginx配置文件,在/usr/local/nginx/conf/目录中创建nginx.conf文件:

rm -f /usr/local/nginx/conf/nginx.conf
vi /usr/local/nginx/conf/nginx.conf

    输入以下内容:

user  www www;
worker_processes 8;
error_log  /data/logs/nginx_error.log  crit;
pid        /usr/local/nginx/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
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 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 8m;
      
    sendfile on;
    tcp_nopush     on;

    keepalive_timeout 60;

    tcp_nodelay on;

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

    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;

    #limit_zone  crawler  $binary_remote_addr  10m;

    server
    {
        listen       80;
        server_name  localhost;
        index index.html index.htm index.php;
        root  /data/htdocs/www;

        #limit_conn   crawler  20;    
                                
        location ~ .*\.(php|php5)?$
        {      
            #fastcgi_pass  unix:/tmp/php-cgi.sock;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
            include fcgi.conf;
        }

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

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

    在/usr/local/nginx/conf/目录中创建fcgi.conf文件:

vi /usr/local/nginx/conf/fcgi.conf

    输入一下内容:

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;

    启动nginx:

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

第三步:安装php5.5.13

    安装PHP所需依赖包:

tar zxvf libiconv-1.14.tar.gz
cd libiconv-1.14
./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/lib64/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib64/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib64/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib64/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib64/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib64/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib64/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib64/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib64/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18

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

    安装php:

tar zxvf php-5.5.13.tar.gz
cd php-5.5.13
./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 --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr \
--enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization \
--with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf \
--with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap \
--enable-opcache=no --without-pear --disable-fileinfo
#注:如果内存较大 可以去掉--disable-fileinfo
make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-development /usr/local/php/etc/php.ini
cd ../
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
vi /usr/local/php/etc/php-fpm.conf

    修改

user = nobody
group = nobody

    为

user = www
group = www

# 将;pid = run/php-fpm.pid前的;去掉并修改为
pid = /usr/local/php/var/run/php-fpm.pid

     启动php-fpm

/usr/local/php/sbin/php-fpm

     将Nginx与fpm加入自启动

vi /etc/rc.local
# 输入
ulimit -SHn 65535
/usr/local/php/sbin/php-fpm
/usr/local/nginx/sbin/nginx

    编译PHP扩展模块memcache、pdo_mysql、imagick

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

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
ln -s /usr/local/mysql/include/* /usr/local/include/
make
make install
cd ../

tar zxvf ImageMagick.tar.gz
cd ImageMagick-6.5.1-2/
./configure
make
make install
cd ../

tar zxvf imagick-3.2.0RC1.tgz
cd imagick-3.2.0RC1
/usr/local/php/bin/phpize
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
cd ../

    修改php.ini配置文件

vi /usr/local/php/etc/php.ini
#查找; extension_dir = "/" 将前面的;去掉并修改为
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/"
#并加入
extension=memcache.so
extension=pdo_mysql.so
extension=imagick.so

    执行下面的命令使配置文件立即生效:

kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`

    其他(可选):

    优化linux内核参数

vi /etc/sysctl.conf

    在末尾增加以下内容:

# Add
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog =  32768
net.core.somaxconn = 32768

net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216

net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2

net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800

#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 1024  65535

    使配置立即生效:

/sbin/sysctl -p

    安装opcache(因为PHP 5.5已经集成Zend Opcache,可以替代eaccelerator)

tar zxvf zendopcache-7.0.3.tgz
cd zendopcache-7.0.3
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
cd ../

    在php.ini中加入下面配置:

[opcache]
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/opcache.so"
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1

# 使php.ini配置文件立即生效
kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`

    常用命令:

#修改完php.ini后执行:
kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`
#修改完nginx.conf后执行
/usr/local/nginx/sbin/nginx -s reload
#重启mysql服务执行:
service mysqld (start|stop|restart)


你可能感兴趣的:(PHP,linux,mysql,nginx,fastcgi)