linux+nginx+mysql+php高性能服务器搭建

1、安装基础包

1 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 libevent-devel perl-CPAN

2、下载源代码包

 1 wget http://blog.s135.com/soft/linux/nginx_php/nginx/nginx-0.8.34.tar.gz
 2 wget http://blog.s135.com/soft/linux/nginx_php/php/php-5.2.13.tar.gz
 3 wget http://php-fpm.org/downloads/0.6/php-fpm-0.6~5.2.patch
 4 wget http://blog.s135.com/soft/linux/nginx_php/mysql/mysql-5.5.2-m2.tar.gz
 5 wget http://blog.s135.com/soft/linux/nginx_php/libiconv/libiconv-1.13.tar.gz
 6 wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/libmcrypt-2.5.8.tar.gz
 7 wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/mcrypt-2.6.8.tar.gz
 8 wget http://blog.s135.com/soft/linux/nginx_php/memcache/memcache-2.2.5.tgz
 9 wget http://blog.s135.com/soft/linux/nginx_php/mhash/mhash-0.9.9.9.tar.gz
10 wget http://blog.s135.com/soft/linux/nginx_php/pcre/pcre-8.01.tar.gz
11 wget http://blog.s135.com/soft/linux/nginx_php/eaccelerator/eaccelerator-0.9.6.tar.bz2
12 wget http://blog.s135.com/soft/linux/nginx_php/pdo/PDO_MYSQL-1.0.2.tgz
13 wget http://blog.s135.com/soft/linux/nginx_php/imagick/ImageMagick.tar.gz
14 wget http://blog.s135.com/soft/linux/nginx_php/imagick/imagick-2.3.0.tgz
wget

二、安装PHP 5.2.13(FastCGI模式)

1.编译安装PHP 5.2.13所需的支持库:

 1 tar zxvf libiconv-1.13.tar.gz
 2 cd libiconv-1.13/
 3 ./configure --prefix=/usr/local
 4 make
 5 make install
 6 cd ../
 7 
 8 tar zxvf libmcrypt-2.5.8.tar.gz 
 9 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 /sbin/ldconfig
41 ./configure
42 make
43 make install
44 cd ../
编译安装PHP 5.2.13所需的支持库

2.编译安装MySQL 5.5.2-m2

 1 /usr/sbin/groupadd mysql
 2 /usr/sbin/useradd -g mysql mysql
 3 tar zxvf mysql-5.5.2-m2.tar.gz
 4 cd mysql-5.5.2-m2/
 5 ./configure --prefix=/usr/local/mysql --enable-assembler --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --with-extra-charsets=complex --enable-thread-safe-client --with-big-tables --with-readline --with-ssl --with-embedded-server --enable-local-infile --with-plugins=partition,innobase,myisammrg
 6 make && make install
 7 chown -R mysql:mysql /usr/local/mysql
 8 /usr/local/mysql/bin/mysql_install_db --user=mysql
 9 cp support-files/my-huge.cnf /etc/my.cnf
10 #vi /etc/my.cnf(优化mysql)
11 /usr/local/mysql/bin/mysqld_safe --user=mysql &
12 mysqladmin -uroot –p password 'password';
13 delete from user where password=’’;
14 cp /usr/local/mysql/share/mysql/mysql.server /etc/rc.d/init.d/mysqld
15 chkconfig --add mysqld
16 chkconfig --level 2345 mysqld on
编译安装MySQL 5.5.2-m2

3.编译安装PHP(FastCGI模式)

1 tar zxvf php-5.2.13.tar.gz
2 cd php-5.2.13/
3 patch -p1 <../php-fpm-0.6~5.2.patch
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  --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-fastcgi --with-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 --enable-ftp --with-libevent=shared
6 make ZEND_EXTRA_LIBS='-liconv'
7 make install
8 cp php.ini-dist /usr/local/php/etc/php.in
编译安装PHP(FastCGI模式)

注意上述命令中--enable-fpm的作用是开启php的fastcgi功能即开启php-fpm功能,--with-mysql=/usr/local/mysql是启用php支持mysql的功能,/usr/local/mysql是mysql数据库的安装路径。

--enable-mbstring表示启用mbstring模块mbstring模块的主要作用在于检测和转换编码,提供对应的多字节操作的字符串函数。目前php内部的编码只支持ISO-8859-*、EUC-JP、UTF-8,其他的编码的语言是没办法在php程序上正确显示的,所以我们要启用mbstring模块。

同时我们也只是简单的开启和扩展php的一部分功能,其他需要的功能,请自行添加。

4.编译安装PHP5扩展模块

 1 tar zxvf memcache-2.2.5.tgz
 2 cd memcache-2.2.5/
 3 /usr/local/php/bin/phpize
 4 ./configure --with-php-config=/usr/local/php/bin/php-config
 5 make
 6 make install
 7 cd ../
 8 
 9 tar jxvf eaccelerator-0.9.6.tar.bz2
10 cd eaccelerator-0.9.6/
11 /usr/local/php/bin/phpize
12 ./configure       --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
13 make
14 make install
15 cd ../
16 
17 tar zxvf PDO_MYSQL-1.0.2.tgz
18 cd PDO_MYSQL-1.0.2/
19 /usr/local/php/bin/phpize
20 ./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql
21 make
22 make install
23 cd ../
24 
25 tar zxvf ImageMagick.tar.gz
26 cd ImageMagick-6.5.1-2/
27 ./configure
28 make
29 make install
30 cd ../
31 
32 tar zxvf imagick-2.3.0.tgz
33 cd imagick-2.3.0/
34 /usr/local/php/bin/phpize
35 ./configure --with-php-config=/usr/local/php/bin/php-config
36 make
37 make install
38 cd ../
编译安装PHP5扩展模块

5.修改php.ini文件

1 手工修改:查找/usr/local/php/etc/php.ini中的 extension_dir = "./"修改为extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
2 并在此行后增加以下几行,然后保存:
  • extension = "memcache.so"
  • extension = "pdo_mysql.so"
  • extension = "imagick.so"
  • 替换:
  • 1 sed -i "s/extension_dir = "./"/extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613"/g" /usr/local/php/etc/php.ini

 

  • 添加:
  • 1 sed -e "/extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613"/aextension = "memcache.so" extension = "pdo_mysql.so" extension = "imagick.so"" /usr/local/php/etc/php.ini
  • 再查找 output_buffering = Off
  • 修改为output_buffering = On
  •  1 sed -e 's/output_buffering = Off/output_buffering = on/g' /usr/local/php/etc/php.ini 

配置eAccelerator加速PHP:

  •  1 mkdir -p /usr/local/eaccelerator_cache
  •  2 vi /usr/local/php/etc/php.ini 

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

 1 [eaccelerator]
 2 zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
 3 eaccelerator.shm_size="64"
 4 eaccelerator.cache_dir="/usr/local/eaccelerator_cache"
 5 eaccelerator.enable="1"
 6 eaccelerator.optimizer="1"
 7 eaccelerator.check_mtime="1"
 8 eaccelerator.debug="0"
 9 eaccelerator.filter=""
10 eaccelerator.shm_max="0"
11 eaccelerator.shm_ttl="3600"
12 eaccelerator.shm_prune_period="3600"
13 eaccelerator.shm_only="0"
14 eaccelerator.compress="1"
15 eaccelerator.compress_level="9"

6.创建www用户和组,以及供www.carl.com虚拟主机使用的目录:

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

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

1 在/usr/local/php/etc/目录中创建php-fpm.conf文件:
2 rm -f /usr/local/php/etc/php-fpm.conf
3 vi /usr/local/php/etc/php-fpm.conf
  1. 输入以下内容(如果您安装 Nginx + PHP 用于程序调试,请将以下的<value name="display_errors">0</value>改为<value name="display_errors">1</value>,以便显示PHP错误信息,否则,Nginx 会报状态为500的空白错误页):
  1 <?xml version="1.0" ?>
  2 <configuration>
  3 
  4   All relative paths in this config are relative to php's install prefix
  5 
  6   <section name="global_options">
  7 
  8     Pid file
  9     <value name="pid_file">/usr/local/php/logs/php-fpm.pid</value>
 10 
 11     Error log file
 12     <value name="error_log">/usr/local/php/logs/php-fpm.log</value>
 13 
 14     Log level
 15     <value name="log_level">notice</value>
 16 
 17     When this amount of php processes exited with SIGSEGV or SIGBUS ...
 18     <value name="emergency_restart_threshold">10</value>
 19 
 20     ... in a less than this interval of time, a graceful restart will be initiated.
 21     Useful to work around accidental curruptions in accelerator's shared memory.
 22     <value name="emergency_restart_interval">1m</value>
 23 
 24     Time limit on waiting child's reaction on signals from master
 25     <value name="process_control_timeout">5s</value>
 26 
 27     Set to 'no' to debug fpm
 28     <value name="daemonize">yes</value>
 29 
 30   </section>
 31 
 32   <workers>
 33 
 34     <section name="pool">
 35 
 36       Name of pool. Used in logs and stats.
 37       <value name="name">default</value>
 38 
 39       Address to accept fastcgi requests on.
 40       Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket'
 41       <value name="listen_address">127.0.0.1:9000</value>
 42 
 43       <value name="listen_options">
 44 
 45         Set listen(2) backlog
 46         <value name="backlog">-1</value>
 47 
 48         Set permissions for unix socket, if one used.
 49         In Linux read/write permissions must be set in order to allow connections from web server.
 50         Many BSD-derrived systems allow connections regardless of permissions.
 51         <value name="owner"></value>
 52         <value name="group"></value>
 53         <value name="mode">0666</value>
 54       </value>
 55 
 56       Additional php.ini defines, specific to this pool of workers.
 57       <value name="php_defines">
 58         <value name="sendmail_path">/usr/sbin/sendmail -t -i</value>
 59         <value name="display_errors">0</value>
 60       </value>
 61 
 62       Unix user of processes
 63       <value name="user">www</value>
 64 
 65       Unix group of processes
 66       <value name="group">www</value>
 67 
 68       Process manager settings
 69       <value name="pm">
 70 
 71         Sets style of controling worker process count.
 72         Valid values are 'static' and 'apache-like'
 73         <value name="style">static</value>
 74 
 75         Sets the limit on the number of simultaneous requests that will be served.
 76         Equivalent to Apache MaxClients directive.
 77         Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi
 78         Used with any pm_style.
 79         <value name="max_children">128</value>
 80 
 81         Settings group for 'apache-like' pm style
 82         <value name="apache_like">
 83 
 84           Sets the number of server processes created on startup.
 85           Used only when 'apache-like' pm_style is selected
 86           <value name="StartServers">20</value>
 87 
 88           Sets the desired minimum number of idle server processes.
 89           Used only when 'apache-like' pm_style is selected
 90           <value name="MinSpareServers">5</value>
 91 
 92           Sets the desired maximum number of idle server processes.
 93           Used only when 'apache-like' pm_style is selected
 94           <value name="MaxSpareServers">35</value>
 95 
 96         </value>
 97 
 98       </value>
 99 
100       The timeout (in seconds) for serving a single request after which the worker process will be terminated
101       Should be used when 'max_execution_time' ini option does not stop script execution for some reason
102       '0s' means 'off'
103       <value name="request_terminate_timeout">0s</value>
104 
105       The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file
106       '0s' means 'off'
107       <value name="request_slowlog_timeout">0s</value>
108 
109       The log file for slow requests
110       <value name="slowlog">logs/slow.log</value>
111 
112       Set open file desc rlimit
113       <value name="rlimit_files">65535</value>
114 
115       Set max core size rlimit
116       <value name="rlimit_core">0</value>
117 
118       Chroot to this directory at the start, absolute path
119       <value name="chroot"></value>
120 
121       Chdir to this directory at the start, absolute path
122       <value name="chdir"></value>
123 
124       Redirect workers' stdout and stderr into main error log.
125       If not set, they will be redirected to /dev/null, according to FastCGI specs
126       <value name="catch_workers_output">yes</value>
127 
128       How much requests each process should execute before respawn.
129       Useful to work around memory leaks in 3rd party libraries.
130       For endless request processing please specify 0
131       Equivalent to PHP_FCGI_MAX_REQUESTS
132       <value name="max_requests">1024</value>
133 
134       Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect.
135       Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi (5.2.2+)
136       Makes sense only with AF_INET listening socket.
137       <value name="allowed_clients">127.0.0.1</value>
138 
139       Pass environment variables like LD_LIBRARY_PATH
140       All $VARIABLEs are taken from current environment
141       <value name="environment">
142         <value name="HOSTNAME">$HOSTNAME</value>
143         <value name="PATH">/usr/local/bin:/usr/bin:/bin</value>
144         <value name="TMP">/tmp</value>
145         <value name="TMPDIR">/tmp</value>
146         <value name="TEMP">/tmp</value>
147         <value name="OSTYPE">$OSTYPE</value>
148         <value name="MACHTYPE">$MACHTYPE</value>
149         <value name="MALLOC_CHECK_">2</value>
150       </value>
151 
152     </section>
153 
154   </workers>
155 
156 </configuration>
php-fpm.conf

8、启动php-cgi进程,监听127.0.0.1的9000端口,进程数为128(如果服务器内存小于3GB,可以只开启64个进程),用户为www:

1 ulimit -SHn 65535
2 /usr/local/php/sbin/php-fpm start

三、安装Nginx 0.8.34

1、  安装Nginx所需的pcre库:

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

2、  安装Nginx

1 tar zxvf nginx-0.8.34.tar.gz
2 cd nginx-0.8.34/
3 ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
4 make && make install

3、  创建Nginx配置文件

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

输入以下内容:

 1 user  www www;
 2 
 3 worker_processes 8;
 4 
 5 error_log  /usr/local/nginx/logs/error.log  crit;
 6 
 7 pid        /usr/local/nginx/logs/nginx.pid;
 8 
 9 #Specifies the value for maximum file descriptors that can be opened by this process. 
10 worker_rlimit_nofile 65535;
11 
12 events 
13 {
14   use epoll;
15   worker_connections 65535;
16 }
17 
18 http 
19 {
20   include       mime.types;
21   default_type  application/octet-stream;
22 
23   #charset  gb2312;
24       
25   server_names_hash_bucket_size 128;
26   client_header_buffer_size 32k;
27   large_client_header_buffers 4 32k;
28   client_max_body_size 8m;
29       
30   sendfile on;
31   tcp_nopush     on;
32 
33   keepalive_timeout 60;
34 
35   tcp_nodelay on;
36 
37   fastcgi_connect_timeout 300;
38   fastcgi_send_timeout 300;
39   fastcgi_read_timeout 300;
40   fastcgi_buffer_size 64k;
41   fastcgi_buffers 4 64k;
42   fastcgi_busy_buffers_size 128k;
43   fastcgi_temp_file_write_size 128k;
44 
45   gzip on;
46   gzip_min_length  1k;
47   gzip_buffers     4 16k;
48   gzip_http_version 1.0;
49   gzip_comp_level 2;
50   gzip_types       text/plain application/x-javascript text/css application/xml;
51   gzip_vary on;
52 
53   #limit_zone  crawler  $binary_remote_addr  10m;
54 
55   server
56   {
57     listen       80;
58     server_name  www.carl.com;
59     index index.html index.htm index.php;
60     root  /data0/htdocs/blog;
61 
62     #limit_conn   crawler  20;    
63                              
64     location ~ .*.(php|php5)?$
65     {      
66       #fastcgi_pass  unix:/tmp/php-cgi.sock;
67       fastcgi_pass  127.0.0.1:9000;
68       fastcgi_index index.php;
69       include fcgi.conf;
70     }
71     
72     location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
73     {
74       expires      30d;
75     }
76 
77     location ~ .*.(js|css)?$
78     {
79       expires      1h;
80     }    
81 
82     log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
83               '$status $body_bytes_sent "$http_referer" '
84               '"$http_user_agent" $http_x_forwarded_for';
85     access_log  /usr/local/nginx/logs/carl_access.log  access;
86       }
87 
88  
89   server
90   {
91     listen  80;
92     server_name  status.blog.s135.com;
93 
94     location / {
95     stub_status on;
96     access_log   off;
97     }
98   }
99 }
nginx.conf

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

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

 1 fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
 2 fastcgi_param  SERVER_SOFTWARE    nginx;
 3 
 4 fastcgi_param  QUERY_STRING       $query_string;
 5 fastcgi_param  REQUEST_METHOD     $request_method;
 6 fastcgi_param  CONTENT_TYPE       $content_type;
 7 fastcgi_param  CONTENT_LENGTH     $content_length;
 8 
 9 fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
10 fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
11 fastcgi_param  REQUEST_URI        $request_uri;
12 fastcgi_param  DOCUMENT_URI       $document_uri;
13 fastcgi_param  DOCUMENT_ROOT      $document_root;
14 fastcgi_param  SERVER_PROTOCOL    $server_protocol;
15 
16 fastcgi_param  REMOTE_ADDR        $remote_addr;
17 fastcgi_param  REMOTE_PORT        $remote_port;
18 fastcgi_param  SERVER_ADDR        $server_addr;
19 fastcgi_param  SERVER_PORT        $server_port;
20 fastcgi_param  SERVER_NAME        $server_name;
21 
22 # PHP only, required if PHP was built with --enable-force-cgi-redirect
23 fastcgi_param  REDIRECT_STATUS    200;
fcgi.conf

4、  启动Nginx

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

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

              vi /etc/rc.local

1 ulimit -SHn 65535
2 /usr/local/php/sbin/php-fpm start
3 /usr/local/nginx/sbin/nginx

五、优化Linux内核参数

vi /etc/sysctl.conf

 1 net.ipv4.tcp_max_syn_backlog = 65536
 2 net.core.netdev_max_backlog =  32768
 3 net.core.somaxconn = 32768
 4 
 5 net.core.wmem_default = 8388608
 6 net.core.rmem_default = 8388608
 7 net.core.rmem_max = 16777216
 8 net.core.wmem_max = 16777216
 9 
10 net.ipv4.tcp_timestamps = 0
11 net.ipv4.tcp_synack_retries = 2
12 net.ipv4.tcp_syn_retries = 2
13 
14 net.ipv4.tcp_tw_recycle = 1
15 #net.ipv4.tcp_tw_len = 1
16 net.ipv4.tcp_tw_reuse = 1
17 
18 net.ipv4.tcp_mem = 94500000 915000000 927000000
19 net.ipv4.tcp_max_orphans = 3276800
20 
21 #net.ipv4.tcp_fin_timeout = 30
22 #net.ipv4.tcp_keepalive_time = 120
23 net.ipv4.ip_local_port_range = 1024  65535

 /sbin/sysctl –p 使新添加的内核参数生效!

六、在不停止Nginx服务的情况下平滑变更Nginx配置
1、修改/usr/local/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:

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


                如果屏幕显示以下两行信息,说明配置文件正确:
  the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully

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

/usr/local/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/nginx/nginx.pid`

七、编写每天定时切割Nginx日志的脚本
  1、创建脚本/usr/local/nginx/sbin /cut_nginx_log.sh

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


                输入 以下内容:

1 #!/bin/bash
2 # This script run at 00:00
3 
4 # The Nginx logs path
5 logs_path="/usr/local/nginx/logs/"
6 
7 mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
8 mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
9 kill -USR1 `cat /usr/local/nginx/nginx.pid`

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

crontab -e

 输入以下内容:

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

原文地址:https://www.cnblogs.com/nb-blog/p/5281733.html