centos 6.2 64位安装nginx php mysql

平台环境http://jiyunjie.blog.51cto.com/5348020/946860

centos 6.2 64位 nginx-1.2.1 php-5.3.14 mysql-5.5.25

阿里云2g内存实例,具体参数调整根据自己主机调整

里面也提到一部分修改建议

32位centos 512内存参考

http://jiyunjie.blog.51cto.com/5348020/907534

 

一、阿里云主机默认不挂载数据盘,先挂载

 

用  fdisk -l|grep Disk  查看你的系统有几块盘。

如:

Disk /dev/xvda: 160.0 GB, 160000000000 bytes

Disk /dev/xvdb: 160.0 GB, 160000000000 bytes

提示这个

disk /dev/xvdb doesn t contain a valid partition table

就是没有挂载,现在我们先挂载

1.   切换到root;

2.   运行命令 "fdisk /dev/xvdb";

3.   根据提示,依次输入 "n", "p", "1";

4.   然后 ,按两次回车;

5.   输入 "w", 当提示Syncing disks.时,表示已成功完成分区;

6.   输入 "mkfs.ext3 /dev/xvdb1" 格式化分区;这里要耐心等待

7.   输入 "mkdir /data0"建立挂载目录

8.   使用"mount /dev/xvdb1 /data0 "命令挂载分区后即可使用。

9.   vi /etc/fstab

按 i

最后一行写入

/dev/xvdb1    /data0    ext3         defaults     0 0

按esc 

输入:wq ,回车

操作完毕,完

 

附:也可以用这个

包括重装系统后,发现挂载的硬盘不见了

fdisk -l后,出现Disk /dev/sdc doesn't contain a valid partition table

如果都没数据的话,要先做文件系统

做文件系统

mke2fs -j /dev/sdb

然后挂载硬盘,即搞定

mkdir /data1

mount /dev/sdb /data1

但是这样会丢掉硬盘中所有数据

可以用下面命令查看磁盘容量及挂载点

df -lh 

 

二、安装依赖库和开发环境

 

阿里云主机64位默认不支持安装gcc的,32位可以省略此步骤

错误 提示:kernel-headers >= 2.2.1

解决办法是,打开 /etc/yum.conf 然后找到这行: 

exclude=kernel* 

修改为: 

exclude= 

就可以安装 gcc 了。

 

安装升级所需程序库

  1. sudo -s 
  2. LANG=C 
  3. 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 

 

64位编译mysql5.5.25a可能会出现各种各样的问题,下面的库也一起安装吧,有部分重复的自己去掉

  1. yum -y install bison gcc gcc-c++ autoconf automake zlib* libxml* ncurses-devel libtool-ltdl-devel* mysql-devel 

三、下载所需程序

  1. mkdir -p /data0/soft 
  2. cd /data0/soft 
  3. wget http://cn2.php.net/get/php-5.3.14.tar.gz/from/this/mirror 
  4. wget http://www.nginx.org/download/nginx-1.2.1.tar.gz 
  5. wget http://mysql.ntu.edu.tw/Downloads/MySQL-5.5/mysql-5.5.25a.tar.gz 
  6. wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz 
  7. wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/libmcrypt-2.5.8.tar.gz 
  8. wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/mcrypt-2.6.8.tar.gz 
  9. wget http://blog.s135.com/soft/linux/nginx_php/mhash/mhash-0.9.9.9.tar.gz 
  10. wget http://pecl.php.net/get/memcache-3.0.6.tgz 
  11. wget http://blog.s135.com/soft/linux/nginx_php/eaccelerator/eaccelerator-0.9.6.1.tar.bz2 
  12. wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz 
  13. wget http://blog.s135.com/soft/linux/nginx_php/imagick/ImageMagick.tar.gz 
  14. wget http://pecl.php.net/get/imagick-2.3.0.tgz 
  15. wget http://www.cmake.org/files/v2.8/cmake-2.8.8.tar.gz 
  16. wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.30.tar.gz 

四、安装PHP 5.3.14

  1. tar zxvf libiconv-1.14.tar.gz 
  2. cd libiconv-1.14/ 
  3. ./configure --prefix=/usr/local 
  4. make 
  5. make install 
  6. cd ../ 

 

  1. tar zxvf libmcrypt-2.5.8.tar.gz  
  2. cd libmcrypt-2.5.8/ 
  3. ./configure 
  4. make 
  5. make install 
  6. /sbin/ldconfig 
  7. cd libltdl/ 
  8. ./configure --enable-ltdl-install 
  9. make 
  10. make install 
  11. cd ../../ 
  1. tar zxvf mhash-0.9.9.9.tar.gz 
  2. cd mhash-0.9.9.9/ 
  3. ./configure 
  4. make 
  5. make install 
  6. cd ../ 

 

  1. ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la 
  2. ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so 
  3. ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4 
  4. ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8 
  5. ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a 
  6. ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la 
  7. ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so 
  8. ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2 
  9. ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1 
  10. ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config 

 

  1. tar zxvf mcrypt-2.6.8.tar.gz 
  2. cd mcrypt-2.6.8/ 
  3. /sbin/ldconfig 
  4. ./configure 
  5. make 
  6. make install 
  7. cd ../ 

 

五、编译安装MySQL 5.5.25a

  1. tar zxvf cmake-2.8.8.tar.gz 
  2. cd cmake-2.8.8 
  3. ./configure 
  4. gmake  
  5. gmake install  
  6. cd ../ 
64位系统安装cmake后执行cmake命令,如不能找到命令,是环境变量的问题,可到cmake目录再执行cmake命令
或添加环境变量,如能直接执行,此步可省略,设置环境变量# export PATH=/usr/local/bin:$PATH

  1. /usr/sbin/groupadd mysql 
  2. /usr/sbin/useradd -g mysql mysql 
  3. tar zxvf mysql-5.5.25a.tar.gz 
  4. cd mysql-5.5.25a 
  1. cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/webserver/mysql \
  2. -DEXTRA_CHARSETS=all \
  3. -DWITH_READLINE=1 \
  4. -DWITH_SSL=system \
  5. -DWITH_EMBEDDED_SERVER=1 \
  6. -DENABLED_LOCAL_INFILE=1 \
  7. -DWITH_INNOBASE_STORAGE_ENGINE=1 \
  8. -DWITHOUT_PARTITION_STORAGE_ENGINE=1
  9. make 
  10. make intall 

 

  1. chmod +w /usr/local/webserver/mysql 
  2. chown -R mysql:mysql /usr/local/webserver/mysql 
  3. cd ../ 

 

附:以下为附加步骤,如果你想在这台服务器上运行MySQL数据库,则执行以下几步。如果你只是希望让PHP支持MySQL扩展库,能够连接其他服务器上的MySQL数据库,那么,以下两步无需执行。
①、创建MySQL数据库存放目录

  1. mkdir -p /data0/mysql/3306/data/ 
  2. mkdir -p /data0/mysql/3306/binlog/ 
  3. mkdir -p /data0/mysql/3306/relaylog/ 
  4. chown -R mysql:mysql /data0/mysql/ 

 #设置mysql我这里分两种方案,可以任选其一。

方案一:

#在support-files目录中有五个配置信息文件:这里我们选择内存1-2G的

#my-small.cnf (内存<=64M)

#my-medium.cnf (内存 128M)

#my-large.cnf (内存 512M)

#my-huge.cnf (内存 1G-2G)

#my-innodb-heavy-4G.cnf (内存 4GB)

  1. cd /usr/local/webserver/mysql 
  2. cp ./support-files/my-huge.cnf /etc/my.cnf  
  3. vi /etc/my.cnf 

编辑配置文件,在 

#在 [mysqld] 段增加
 
  1. datadir = /data0/mysql/3306/data #添加MySQL数据库路径 
  2. wait-timeout = 30 
  3. max_connections = 512 
  4. default-storage-engine = MyISAM 
  5. max_allowed_packet = 16M

方案二:

关于my.cnf配置文件,我这里贴一个我配置的,仅作参考,可以忽略。如选择此方案,在/etc/my.cnf 里面输入下面的内容 

[client]

default-character-set = utf8

port    = 3306

socket  = /tmp/mysql.sock

[mysqld]

character-set-server = utf8

replicate-ignore-db = mysql

replicate-ignore-db = test

replicate-ignore-db = information_schema

user    = mysql

port    = 3306

socket  = /tmp/mysql.sock

basedir = /usr/local/webserver/mysql

datadir = /data0/mysql/3306/data

log-error = /data0/mysql/3306/mysql_error.log

pid-file = /data0/mysql/3306/mysql.pid

#默认10240

open_files_limit    = 9600

#默认600

back_log = 300

#默认5000

max_connections = 2000

#默认6000

max_connect_errors = 3000

table_cache = 614

table_open_cache = 614

external-locking = FALSE

max_allowed_packet = 32M

sort_buffer_size = 1M

join_buffer_size = 1M

#默认300

thread_cache_size = 150

#双核的话设4,4核设8,此处禁止了

#thread_concurrency = 8

#默认512

query_cache_size = 256M

query_cache_limit = 2M

query_cache_min_res_unit = 2k

default-storage-engine = MyISAM

thread_stack = 192K

transaction_isolation = READ-COMMITTED

#默认246

tmp_table_size = 128M

max_heap_table_size = 246M

long_query_time = 3

log-slave-updates

log-bin = /data0/mysql/3306/binlog/binlog

binlog_cache_size = 4M

binlog_format = MIXED

max_binlog_cache_size = 8M

max_binlog_size = 1G

relay-log-index = /data0/mysql/3306/relaylog/relaylog

relay-log-info-file = /data0/mysql/3306/relaylog/relaylog

relay-log = /data0/mysql/3306/relaylog/relaylog

expire_logs_days = 30

#默认256

key_buffer_size = 64M

read_buffer_size = 1M

read_rnd_buffer_size = 16M

#默认64

bulk_insert_buffer_size = 32M

#默认128

myisam_sort_buffer_size = 64M

myisam_max_sort_file_size = 10G

myisam_repair_threads = 1

myisam_recover

 

interactive_timeout = 120

wait_timeout = 120

 

skip-name-resolve

#master-connect-retry = 10

slave-skip-errors = 1032,1062,126,1114,1146,1048,1396

 

#master-host     =   192.168.1.2

#master-user     =   username

#master-password =   password

#master-port     =  3306

 

server-id = 1

 

innodb_additional_mem_pool_size = 16M

innodb_buffer_pool_size = 512M

innodb_data_file_path = ibdata1:256M:autoextend

innodb_file_io_threads = 4

innodb_thread_concurrency = 8

innodb_flush_log_at_trx_commit = 2

innodb_log_buffer_size = 16M

innodb_log_file_size = 128M

innodb_log_files_in_group = 3

innodb_max_dirty_pages_pct = 90

innodb_lock_wait_timeout = 120

innodb_file_per_table = 0

#log-slow-queries = /data0/mysql/3306/slow.log

#long_query_time = 10

[mysqldump]

quick

max_allowed_packet = 32M

[myisamchk]

#默认256

key_buffer_size = 128M

#默认256

sort_buffer_size = 128M

read_buffer = 2M

write_buffer = 2M

[mysqlhotcopy]

interactive-timeout

 

以上配置文件仅作1.5G内存主机参考

#生成授权表

  1. ./scripts/mysql_install_db --basedir=/usr/local/webserver/mysql --datadir=/data0/mysql/3306/data --user=mysql #生成mysql系统数据库 

#更改密码

  1. /usr/local/webserver/mysql/bin/mysqladmin -u root password '12345678'  

#开启mysql

  1. /usr/local/webserver/mysql/bin/mysqld_safe & 

#通过命令行登录管理MySQL服务器(提示输入密码时直接回车,上处修改密码了的话这里要输入的):

  1. /usr/local/webserver/mysql/bin/mysql -u root -p -S /tmp/mysql.sock 
  2. show databases; 
  3. exit; 

#设置开机启动

  1. vi /etc/rc.d/rc.local 

#加入

  1. /usr/local/webserver/mysql/bin/mysqld_safe & 

 

可以暂时停止mysql

/usr/local/webserver/mysql/bin/mysqladmin -u root -p shutdown

//mysql安装到此截止了,php请看下一章

附:常用mysql命令:

(1)查看用户信息

select user,host,password from mysql.user;

(2)输入以下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';

(3)创建数据库

/usr/local/webserver/mysql/bin/mysql -u admin -p -S /tmp/mysql.sock

(注意密码是 12345678)

MySQL>create database blog;

MySQL>flush privileges;

查询用户下的数据库

SHOW DATABASES; 

查询数据库编码:SHOW VARIABLES LIKE 'character%';

3、编译安装PHP(FastCGI模式)如果是64位操作系统,需要先进行下步工作

 如果是64位操作系统,需要先进行下步工作

 

解决没有安装 patch 工具问题,

解决imagemagick问题     

解决 configure: error: libjpeg.(a|so) not found  错误

解决configure: error: libpng.(a|so) not found

解决  configure: error: Cannot find ldap libraries in /usr/lib  错误,上面的错误也可用这种方法解决

  1. yum install patch 
  2. yum -y install perl-CPAN 
  3. ln -s /usr/lib64/libjpeg.so /usr/lib/libjpeg.so 
  4. ln -s /usr/lib64/libpng.so /usr/lib/libpng.so 
  5. ln -s /usr/lib64/libldap.so /usr/lib/libldap.so 
  1. tar zxvf php-5.3.14.tar.gz 
  2. cd php-5.3.14/ 
  3. ln -s /usr/local/webserver/mysql/lib/libmysqlclient.so /usr/lib64/ 
  4. ln -s /usr/local/webserver/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18 
32位系统应该是下面的,为免出错,可以全做
  1. ln -s /usr/local/webserver/mysql/lib/libmysqlclient.so /usr/lib/ 
  2. ln -s /usr/local/webserver/mysql/lib/libmysqlclient.so.18 /usr/lib/libmysqlclient.so.18 
  1. ./configure --prefix=/usr/local/webserver/php --with-config-file-path=/usr/local/webserver/php/etc --with-mysql=/usr/local/webserver/mysql --with-mysqli=/usr/local/webserver/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-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap 
  1. make ZEND_EXTRA_LIBS='-liconv' 
  2. make install 
  1. cp php.ini-production /usr/local/webserver/php/etc/php.ini 
  2. cd ../ 

4、编译安装PHP5扩展模块

  1. tar zxvf memcache-3.0.6.tgz 
  2. cd memcache-3.0.6/ 
  3. /usr/local/webserver/php/bin/phpize 
  4. ./configure --with-php-config=/usr/local/webserver/php/bin/php-config 
  5. make 
  6. make install 
  7. cd ../ 
  1. tar jxvf eaccelerator-0.9.6.1.tar.bz2 
  2. cd eaccelerator-0.9.6.1/ 
  3. /usr/local/webserver/php/bin/phpize 
  4. ./configure --enable-eaccelerator=shared --with-php-config=/usr/local/webserver/php/bin/php-config 
  5. make 
  6. make install 
  7. cd ../ 
  1. tar zxvf PDO_MYSQL-1.0.2.tgz 
  2. cd PDO_MYSQL-1.0.2/ 
  3. /usr/local/webserver/php/bin/phpize 
  4. ./configure --with-php-config=/usr/local/webserver/php/bin/php-config --with-pdo-mysql=/usr/local/webserver/mysql 
  5. make 
  6. make install 
  7. cd ../ 
  1. tar zxvf ImageMagick.tar.gz 
  2. cd ImageMagick-6.5.1-2/ 
  3. ./configure 
  4. make 
  5. make install 
  6. cd ../ 
  1. tar zxvf imagick-2.3.0.tgz 
  2. cd imagick-2.3.0/ 
  3. /usr/local/webserver/php/bin/phpize 
  4. ./configure --with-php-config=/usr/local/webserver/php/bin/php-config 
  5. make 
  6. make install 
  7. cd ../ 

 5、修改php.ini

手工修改:查找/usr/local/webserver/php/etc/php.ini中的找到

找到:;date.timezone =

修改为:date.timezone = PRC #设置时区

找到:expose_php = On

修改为:expose_php = OFF #禁止显示php版本的信息

找到:display_errors = On

修改为:display_errors = OFF #关闭错误提示

 

查找extension_dir = "./"

修改为extension_dir = "/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626/"

  并在此行后增加以下几行,然后保存:

  extension = "memcache.so"

  extension = "pdo_mysql.so"

  extension = "imagick.so"

查找

short_open_tag = Off

修改

short_open_tag = On

 

6、配置eAccelerator加速PHP:

  1. mkdir -p /usr/local/webserver/eaccelerator_cache 
  2. chmod 777  /usr/local/webserver/eaccelerator_cache 
  3. vi /usr/local/webserver/php/etc/php.ini 

 按shift+g键跳到配置文件的最末尾,加上以下配置信息:

[eaccelerator]
zend_extension="/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20090626/eaccelerator.so"
eaccelerator.shm_size="64"
eaccelerator.cache_dir="/usr/local/webserver/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"

 7、创建www用户和组,以及供两个虚拟主机使用的目录:

  1. /usr/sbin/groupadd www 
  2. /usr/sbin/useradd -g www www 
  3. mkdir -p /data0/htdocs/blog 
  4. chmod +w /data0/htdocs/blog 
  5. chown -R www:www /data0/htdocs/blog 
  6. mkdir -p /data0/htdocs/www 
  7. chmod +w /data0/htdocs/www 
  8. chown -R www:www /data0/htdocs/www 

 创建php-fpm配置文件(php-fpm是为PHP打的一个FastCGI管理补丁,可以平滑变更php.ini配置而无需重启php-cgi)

  1. cp /usr/local/webserver/php/etc/php-fpm.conf.default /usr/local/webserver/php/etc/php-fpm.conf 

 修改

  1. vi /usr/local/webserver/php/etc/php-fpm.conf 
php_flag[display_errors] = off;不显示错误信息
pm.max_children = 36 ;静态方式下开启的php-fpm进程数量,内存小于3g不要超过64
pm.start_servers = 5;动态方式下的起始php-fpm进程数量。
pm.min_spare_servers = 5;动态方式下的最小php-fpm进程数量
pm.max_spare_servers = 36;动态方式下的最大php-fpm进程数量
pm.max_requests = 512
user = www
group = www 
pm = dynamic;进程数动态模式不清楚的不要修改
;pm = static;进程数静态模式不清楚的不要修改
pid = run/php-fpm.pid ;取消前面的分号

 #检查语法是否正确

  1. /usr/local/webserver/php/sbin/php-fpm -t 

 #出现NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful 测试成功

启动fpm

  1. /usr/local/webserver/php/sbin/php-fpm & 

 相关命令

INT, TERM 立刻终止

QUIT 平滑终止

USR1 重新打开日志文件

USR2 平滑重载所有worker进程并重新载入配置和二进制模块

示例:

php-fpm 关闭:

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

php-fpm 重启:

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

查看php-fpm进程数:

ps aux | grep -c php-fpm

三、安装Nginx 1.2.1

1、安装Nginx所需的pcre库:

  1. tar zxvf pcre-8.30.tar.gz 
  2. cd pcre-8.30/ 
  3. ./configure 
  4. make && make install 
  5. cd ../ 

2、安装Nginx

  1. tar zxvf nginx-1.2.1.tar.gz 
  2. cd nginx-1.2.1/ 
  3. ./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/data0/soft/pcre-8.30 
  4. make && make install 
  5. cd ../ 

3、创建Nginx日志目录

  1. mkdir -p /data0/logs 
  2. chmod +w /data0/logs 
  3. chown -R www:www /data0/logs 

 

 

4、创建Nginx配置文件

  ①、在/usr/local/webserver/nginx/conf/目录中创建nginx.conf文件:

  1. rm -f /usr/local/webserver/nginx/conf/nginx.conf 
  2. vi /usr/local/webserver/nginx/conf/nginx.conf 

 

输入以下内容:

user  www www;

worker_processes 8;

error_log  /data0/logs/nginx_error.log  crit;

pid        /usr/local/webserver/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;

  server_tokens off;

  #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  42.121.83.101;

    index index.html index.htm index.php;

    root  /data0/htdocs/blog;

    #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;

    }    

      }

    log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '

              '$status $body_bytes_sent "$http_referer" '

              '"$http_user_agent" $http_x_forwarded_for';

    access_log  /data0/logs/access.log  access;

  server

  {

    listen       80;

    server_name  www.s135.com;

    index index.html index.htm index.php;

    root  /data0/htdocs/www;

 

    location ~ .*\.(php|php5)?$

    {      

      #fastcgi_pass  unix:/tmp/php-cgi.sock;

      fastcgi_pass  127.0.0.1:9000;

      fastcgi_index index.php;

      include fcgi.conf;

    }

  }

    log_format  wwwlogs  '$remote_addr - $remote_user [$time_local] "$request" '

               '$status $body_bytes_sent "$http_referer" '

               '"$http_user_agent" $http_x_forwarded_for';

    access_log  /data0/logs/wwwlogs.log  wwwlogs;

  server

  {

    listen  80;

    server_name  status.blog.s135.com;

 

    location / {

    stub_status on;

    access_log   off;

    }

  }

}

 

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

  1. vi /usr/local/webserver/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;
 

 5、启动Nginx

  1. /usr/local/webserver/nginx/sbin/nginx & 

 #检测配置文件

  1. /usr/local/webserver/nginx/sbin/nginx -t 

 

#提示表示成功

#nginx: the configuration file /usr/local/webserver/nginx/conf/nginx.conf syntax is ok

#nginx: configuration file /usr/local/webserver/nginx/conf/nginx.conf test is successful

 

相关命令

#开启Nginx

/usr/local/webserver/nginx/sbin/nginx &

#平滑重启Nginx

/usr/local/webserver/nginx/sbin/nginx -s reload

 

四、配置开机自动启动Nginx + PHP

  1. vi /etc/rc.d/rc.local  

在末尾增加以下内容:

  1. /usr/local/webserver/php/sbin/php-fpm & 
  2. /usr/local/webserver/nginx/sbin/nginx &

五、优化Linux内核参数

  1. 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

 使配置立即生效:

  1. /sbin/sysctl -p 

 六、在不停止Nginx服务的情况下平滑变更Nginx配置

  1、修改/usr/local/webserver/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:

/usr/local/webserver/nginx/sbin/nginx -t

  如果屏幕显示以下两行信息,说明配置文件正确:

  the configuration file /usr/local/webserver/nginx/conf/nginx.conf syntax is ok

  the configuration file /usr/local/webserver/nginx/conf/nginx.conf was tested successfully

 

  2、平滑重启:

  ①、对于Nginx 0.8.x版本,现在平滑重启Nginx配置非常简单,执行以下命令即可:

/usr/local/webserver/nginx/sbin/nginx -s reload

 

  ②、对于Nginx 0.8.x之前的版本,平滑重启稍微麻烦一些,按照以下步骤进行即可。输入以下命令

 

查看Nginx主进程号:

ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'

  屏幕显示的即为Nginx主进程号,例如:

  6302

  这时,执行以下命令即可使修改过的Nginx配置文件生效:

kill -HUP 6302

  或者无需这么麻烦,找到Nginx的Pid文件:

kill -HUP `cat /usr/local/webserver/nginx/nginx.pid`

七、编写每天定时切割Nginx日志的脚本

 1、创建脚本

  1. vi /usr/local/webserver/nginx/sbin/cut_nginx_log.sh 

 输入以下内容:

#!/bin/bash

# This script run at 00:00

# The Nginx logs path

logs_path="/usr/local/webserver/nginx/logs/"mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log

kill -USR1 `cat /usr/local/webserver/nginx/nginx.pid`

 2、设置crontab,每天凌晨00:00切割nginx访问日志

  1. crontab -e 

 

输入以下内容:

00 00 * * * /bin/bash  /usr/local/webserver/nginx/sbin/cut_nginx_log.sh

查看你的定时任务是否加进去了。

  1. crontab -l 

你可能感兴趣的:(centos)