参考资料:
http://blog.s135.com/nginx_php_v6/
http://blog.s135.com/post/375/
背景介绍:
以前,因为AWS EC2的价格比较昂贵而租用了其它国外小厂商的VPS,在使用了3年多之后,发现AWS EC2的价格居然比现在正在使用的VPS要便宜很多。
全球线路与速度最理想的日本节点的t1.micro型号,选择3年长期合约的价格在2500元人民币左右,一年下来也不到900元,所以就打算将Blog迁移到AWS EC2上。
之前在部署VPS环境的时候,基本上是完整的参考了张宴的两篇文档,包括操作系统的选择也是相同的。
但这一次我想采用目前较新的CentOS 6.4 minimal x86_64。
同时选择版本最新的Nginx,PHP以及MySQL,引入EPEL仓库来简化安装与部署过程中对额外的依赖软件的安装,优化一些原来的工作方式,比如创建服务管理脚本来启动/停止Nginx与MySQL,使用logrotate来切割日志等;
经过了一番折腾,在填了不少的坑之后,终于成功的完成了新环境的部署,感觉非常不错。
下面,就是此次的整个安装配置过程:
1. 安装常用工具
yum install -y vim wget unzip screen tree mlocate
2. 安装编译工具
yum install -y gcc gcc-c++ autoconf patch cmake automake
3. 安装EPEL仓库
yum install -y http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
4. 安装所有必需的软件包
yum install -y libjpeg-turbo libjpeg-turbo-devel libpng libpng-devel freetype freetype-devel
yum install -y libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel gmp gmp-devel
yum install -y bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel unixODBC unixODBC-devel
yum install -y krb5-libs krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel
yum install -y libgcrypt libgcrypt-devel libtool
yum install -y libmcrypt libmcrypt-devel mhash mhash-devel mcrypt pcre pcre-devel
yum install -y ImageMagick ImageMagick-devel
5. 下载所有必需的软件源码包
mkdir /root/packages
cd /root/packages
wget http://nginx.org/download/nginx-1.4.7.tar.gz
wget http://museum.php.net/php5/php-5.2.17.tar.gz
wget http://php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz
wget http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-5.5.36.tar.gz
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz
wget http://pecl.php.net/get/imagick-3.2.0RC1.tgz
wget http://pkgs.fedoraproject.org/repo/pkgs/php-eaccelerator/eaccelerator-0.9.6.1.tar.bz2/32ccd838e06ef5613c2610c1c65ed228/eaccelerator-0.9.6.1.tar.bz2
6. 编译安装iconv
cd /root/packages
tar xzvf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure
make
make install
7. 编译安装并配置MySQL
7.1 编译安装MySQL
cd /root/packages
tar xzvf mysql-5.5.36.tar.gz
cd mysql-5.5.36
cmake -DCMAKE_INSTALL_PREFIX=/webserver/mysql
-DSYSCONFDIR=/webserver/mysql/etc
-DMYSQL_DATADIR=/webserver/mysql/data
-DMYSQL_TCP_PORT=3306
-DMYSQL_UNIX_ADDR=/werbserver/mysql/run/mysqld.sock
-DMYSQL_USER=mysql
-DEXTRA_CHARSETS=all
-DWITH_READLINE=1
-DWITH_SSL=system
-DWITH_EMBEDDED_SERVER=1
-DENABLED_LOCAL_INFILE=1
-DWITH_INNOBASE_STORAGE_ENGINE=1
make
make install
7.2 创建mysql用户
useradd mysql -d /webserver/mysql/data
7.3 创建MySQL所需目录
mkdir -p /webserver/mysql/{etc,data,tmp,run,binlogs,log}
chown mysql:mysql -R /webserver/mysql/{etc,data,tmp,run,binlogs,log}
7.4 创建my.cnf配置文件
vim /webserver/mysql/etc/my.cnf
03 |
datadir = /webserver/mysql/data |
|
|
04 |
tmpdir = /webserver/mysql/tmp |
|
|
05 |
socket = /webserver/mysql/run/mysqld.sock |
|
|
07 |
pid-``file = /webserver/mysql/run/mysqld.pid |
|
|
10 |
default-storage-engine = INNODB |
|
|
11 |
innodb_file_per_table = 1 |
|
|
12 |
log-bin = /webserver/mysql/binlogs/bin-log-mysqld |
|
|
13 |
log-bin-index = /webserver/mysql/binlogs/bin-log-mysqld.index |
|
|
14 |
innodb_data_home_dir = /webserver/mysql/data |
|
|
15 |
innodb_data_file_path = ibdata1:10M:autoextend |
|
|
16 |
innodb_log_group_home_dir = /webserver/mysql/data |
|
|
18 |
# increase the max connections |
|
|
21 |
# the expiration time for logs, including binlogs |
|
|
24 |
# set the character as utf8 |
|
|
25 |
character-``set``-server = utf8 |
|
|
26 |
collation-server = utf8_unicode_ci |
|
|
33 |
log-error = /webserver/mysql/log/mysqld.log |
|
|
34 |
pid-``file = /webserver/mysql/run/mysqld.pid |
|
|
35 |
open``-files-limit = 8192 |
|
|
42 |
socket = /webserver/mysql/run/mysqld.sock |
|
|
43 |
default-character-``set = utf8 |
|
|
chown mysql:mysql /webserver/mysql/etc/my.cnf
7.5 初始化MySQL系统数据库
chmod +x scripts/mysql_install_db
scripts/mysql_install_db –user=mysql –basedir=/webserver/mysql –datadir=/webserver/mysql/data/ –defaults-file=/webserver/mysql/etc/my.cnf
7.6 创建MySQL服务管理脚本
mkdir /webserver/init.d/
cp support-files/mysql.server /webserver/init.d/mysqld
chmod +x /webserver/init.d/mysqld
7.7 启动MySQL服务
/webserver/init.d/mysqld start
7.8 登陆MySQL命令行终端
/webserver/mysql/bin/mysql -uroot -p
8. 编译安装并配置PHP
8.1 编译安装PHP
cd /root/packages
tar xzf php-5.2.17.tar.gz
gzip -cd php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.2.17 -p1
注:创建软链接以便PHP在编译时能顺利找到所需的库文件
ln -s /webserver/mysql/lib /webserver/mysql/lib64
ln -s /usr/local/lib/libiconv.so /usr/local/lib64/libiconv.so
ln -s /usr/lib64/libmhash.so.2 /usr/lib64/libmhash.so
cd php-5.2.17
./configure –prefix=/webserver/php
–with-config-file-path=/webserver/php/etc
–with-mysql=/webserver/mysql –with-mysqli=/webserver/mysql/bin/mysql_config
–with-libdir=lib64
–with-iconv-dir=/usr/local
–with-freetype-dir –with-jpeg-dir –with-png-dir
–with-zlib –with-libxml-dir –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
make ZEND_EXTRA_LIBS=’-liconv’
注:创建软链接以便PHP在安装时能顺利找到所需的库文件
ln -s /webserver/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18
ln -s /usr/local/lib/libiconv.so.2 /usr/lib64/libiconv.so.2
make install
cp php.ini-dist /webserver/php/etc/php.ini
8.2 编译安装eaccelerator
cd /root/packages
tar xjf eaccelerator-0.9.6.1.tar.bz2
cd eaccelerator-0.9.6.1/
/webserver/php/bin/phpize
./configure –enable-eaccelerator=shared –with-php-config=/webserver/php/bin/php-config
make
make install
cd ../
8.3 编译安装PDO_MYSQL
tar zxvf PDO_MYSQL-1.0.2.tgz
cd PDO_MYSQL-1.0.2/
/webserver/php/bin/phpize
./configure –with-php-config=/webserver/php/bin/php-config –with-pdo-mysql=/webserver/mysql
make
make install
cd ../
8.4 编译安装imagick
tar xzf imagick-3.2.0RC1.tgz
cd imagick-3.2.0RC1
/webserver/php/bin/phpize
./configure –with-php-config=/webserver/php/bin/php-config
make
make install
8.5 修改php.ini配置文件
vim /webserver/php/etc/php.ini
01 |
; Directory``in which the loadable extensions (modules) reside. |
|
|
02 |
;extension_dir =``"./" |
|
|
03 |
extension_dir =``"/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/" |
|
|
04 |
extension =``"imagick.so" |
|
|
05 |
extension =``"pdo_mysql.so" |
|
|
07 |
;output_buffering = Off |
|
|
8.6 创建缓存目录
mkdir /webserver/eaccelerator_cache
8.7 在php.ini中添加eaccelerator支持
vim /webserver/php/etc/php.ini
02 |
zend_extension=``"/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so" |
|
|
03 |
eaccelerator.shm_size=``"1" |
|
|
04 |
eaccelerator.cache_dir=``"/webserver/eaccelerator_cache" |
|
|
05 |
eaccelerator.``enable``=``"1" |
|
|
06 |
eaccelerator.optimizer=``"1" |
|
|
07 |
eaccelerator.check_mtime=``"1" |
|
|
08 |
eaccelerator.debug=``"0" |
|
|
09 |
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" |
|
|
16 |
eaccelerator.keys =``"disk_only" |
|
|
17 |
eaccelerator.sessions =``"disk_only" |
|
|
18 |
eaccelerator.content =``"disk_only" |
|
|
8.8 创建www用户
useradd -r www
8.9 创建所需目录
mkdir -p /webserver/blog/{rainbow,eric,keshui,logs}
chown www:www /webserver/blog/{rainbow,eric,keshui,logs}
chmod +w /webserver/blog/{rainbow,eric,keshui,logs}
mkdir /webserver/php/run
8.10 创建php-fpm.con配置文件
rm -f /webserver/php/etc/php-fpm.conf
vim /webserver/php/etc/php-fpm.conf
001 |
<?``xml version``=``"1.0" ?> |
|
|
004 |
All relative paths in this config are relative to php's install prefix |
|
|
006 |
<``section name``=``"global_options"``> |
|
|
009 |
<``value name``=``"pid_file"``>/webserver/php/logs/php-fpm.pid</``value``> |
|
|
012 |
<``value name``=``"error_log"``>/webserver/php/logs/php-fpm.log</``value``> |
|
|
015 |
<``value name``=``"log_level"``>notice</``value``> |
|
|
017 |
When this amount of php processes exited with SIGSEGV or SIGBUS ... |
|
|
018 |
<``value name``=``"emergency_restart_threshold"``>10</``value``> |
|
|
020 |
... in a less than this interval of time, a graceful restart will be initiated. |
|
|
021 |
Useful to work around accidental curruptions in accelerator's shared memory. |
|
|
022 |
<``value name``=``"emergency_restart_interval"``>1m</``value``> |
|
|
024 |
Time limit on waiting child's reaction on signals from master |
|
|
025 |
<``value name``=``"process_control_timeout"``>5s</``value``> |
|
|
027 |
Set to 'no' to debug fpm |
|
|
028 |
<``value name``=``"daemonize"``>yes</``value``> |
|
|
034 |
<``section name``=``"pool"``> |
|
|
036 |
Name of pool. Used in logs and stats. |
|
|
037 |
<``value name``=``"name"``>default</``value``> |
|
|
039 |
Address to accept fastcgi requests on. |
|
|
040 |
Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket' |
|
|
041 |
<``value name``=``"listen_address"``>/webserver/php/run/php-cgi.sock</``value``> |
|
|
043 |
<``value name``=``"listen_options"``> |
|
|
045 |
Set listen(2) backlog |
|
|
046 |
<``value name``=``"backlog"``>-1</``value``> |
|
|
048 |
Set permissions for unix socket, if one used. |
|
|
049 |
In Linux read/write permissions must be set in order to allow connections from web server. |
|
|
050 |
Many BSD-derrived systems allow connections regardless of permissions. |
|
|
051 |
<``value name``=``"owner"``></``value``> |
|
|
052 |
<``value name``=``"group"``></``value``> |
|
|
053 |
<``value name``=``"mode"``>0666</``value``> |
|
|
056 |
Additional php.ini defines, specific to this pool of workers. |
|
|
057 |
<``value name``=``"php_defines"``> |
|
|
058 |
<``value name``=``"sendmail_path"``>/usr/sbin/sendmail -t -i</``value``> |
|
|
059 |
<``value name``=``"display_errors"``>0</``value``> |
|
|
062 |
Unix user of processes |
|
|
063 |
<``value name``=``"user"``>www</``value``> |
|
|
065 |
Unix group of processes |
|
|
066 |
<``value name``=``"group"``>www</``value``> |
|
|
068 |
Process manager settings |
|
|
069 |
<``value name``=``"pm"``> |
|
|
071 |
Sets style of controling worker process count. |
|
|
072 |
Valid values are 'static' and 'apache-like' |
|
|
073 |
<``value name``=``"style"``>static</``value``> |
|
|
075 |
Sets the limit on the number of simultaneous requests that will be served. |
|
|
076 |
Equivalent to Apache MaxClients directive. |
|
|
077 |
Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi |
|
|
078 |
Used with any pm_style. |
|
|
079 |
<``value name``=``"max_children"``>5</``value``> |
|
|
081 |
Settings group for 'apache-like' pm style |
|
|
082 |
<``value name``=``"apache_like"``> |
|
|
084 |
Sets the number of server processes created on startup. |
|
|
085 |
Used only when 'apache-like' pm_style is selected |
|
|
086 |
<``value name``=``"StartServers"``>20</``value``> |
|
|
088 |
Sets the desired minimum number of idle server processes. |
|
|
089 |
Used only when 'apache-like' pm_style is selected |
|
|
090 |
<``value name``=``"MinSpareServers"``>5</``value``> |
|
|
092 |
Sets the desired maximum number of idle server processes. |
|
|
093 |
Used only when 'apache-like' pm_style is selected |
|
|
094 |
<``value name``=``"MaxSpareServers"``>35</``value``> |
|
|
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 |
|
|
103 |
<``value name``=``"request_terminate_timeout"``>0s</``value``> |
|
|
105 |
The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file |
|
|
107 |
<``value name``=``"request_slowlog_timeout"``>0s</``value``> |
|
|
109 |
The log file for slow requests |
|
|
110 |
<``value name``=``"slowlog"``>logs/slow.log</``value``> |
|
|
112 |
Set open file desc rlimit |
|
|
113 |
<``value name``=``"rlimit_files"``>65535</``value``> |
|
|
115 |
Set max core size rlimit |
|
|
116 |
<``value name``=``"rlimit_core"``>0</``value``> |
|
|
118 |
Chroot to this directory at the start, absolute path |
|
|
119 |
<``value name``=``"chroot"``></``value``> |
|
|
121 |
Chdir to this directory at the start, absolute path |
|
|
122 |
<``value name``=``"chdir"``></``value``> |
|
|
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``> |
|
|
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``> |
|
|
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``> |
|
|
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``> |
|
|
8.11 创建php-fpm服务管理脚本
ln -s /webserver/php/sbin/php-fpm /webserver/init.d/php-fpm
8.12 启动php-fpm服务
/webserver/init.d/php-fpm start
9. 编译安装并配置Nginx
9.1 编译安装Nginx
cd /root/packages
tar xzf nginx-1.4.7.tar.gz
cd nginx-1.4.7/
./configure –user=www –group=www
–prefix=/usr/local/webserver/nginx
–with-http_stub_status_module –with-http_ssl_module
make
make install
9.2 创建Nginx所需目录
mkdir /webserver/nginx/conf/conf.d
mkdir /webserver/nginx/run
9.3 创建fastcgi_params配置文件
vim fastcgi_params
01 |
fastcgi_param QUERY_STRING $query_string; |
|
|
02 |
fastcgi_param REQUEST_METHOD $request_method; |
|
|
03 |
fastcgi_param CONTENT_TYPE $content_type; |
|
|
04 |
fastcgi_param CONTENT_LENGTH $content_length; |
|
|
06 |
fastcgi_param SCRIPT_NAME $fastcgi_script_name; |
|
|
07 |
fastcgi_param REQUEST_URI $request_uri; |
|
|
08 |
fastcgi_param DOCUMENT_URI $document_uri; |
|
|
09 |
fastcgi_param DOCUMENT_ROOT $document_root; |
|
|
10 |
fastcgi_param SERVER_PROTOCOL $server_protocol; |
|
|
12 |
fastcgi_param GATEWAY_INTERFACE CGI/1.1; |
|
|
13 |
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; |
|
|
15 |
fastcgi_param REMOTE_ADDR $remote_addr; |
|
|
16 |
fastcgi_param REMOTE_PORT $remote_port; |
|
|
17 |
fastcgi_param SERVER_ADDR $server_addr; |
|
|
18 |
fastcgi_param SERVER_PORT $server_port; |
|
|
19 |
fastcgi_param SERVER_NAME $server_name; |
|
|
21 |
# PHP only, required if PHP was built with --enable-force-cgi-redirect |
|
|
22 |
fastcgi_param REDIRECT_STATUS 200; |
|
|
9.4 创建Nginx.conf主配置文件
vim /webserver/nginx/conf/nginx.conf
04 |
error_log /webserver/nginx/logs/error.log; |
|
|
05 |
pid /webserver/nginx/run/nginx.pid; |
|
|
07 |
worker_rlimit_nofile 32768; |
|
|
11 |
worker_connections 32768; |
|
|
16 |
include /webserver/nginx/conf/mime.types; |
|
|
17 |
default_type application/octet-stream; |
|
|
19 |
log_format main``'$remote_addr - $remote_user [$time_local] "$request" ' |
|
|
20 |
'$status $body_bytes_sent "$http_referer" ' |
|
|
21 |
'"$http_user_agent" "$http_x_forwarded_for"'``; |
|
|
23 |
access_log /webserver/nginx/logs/access.log main; |
|
|
25 |
server_names_hash_bucket_size 128; |
|
|
26 |
client_header_buffer_size 32k; |
|
|
27 |
large_client_header_buffers 4 32k; |
|
|
36 |
fastcgi_connect_timeout 300; |
|
|
37 |
fastcgi_send_timeout 300; |
|
|
38 |
fastcgi_read_timeout 300; |
|
|
39 |
fastcgi_buffer_size 64k; |
|
|
40 |
fastcgi_buffers 4 64k; |
|
|
41 |
fastcgi_busy_buffers_size 128k; |
|
|
42 |
fastcgi_temp_file_write_size 128k; |
|
|
47 |
gzip_http_version 1.0; |
|
|
49 |
gzip_types text/plain application/x-javascript text/css application/xml; |
|
|
52 |
include /webserver/nginx/conf/conf.d/*.conf; |
|
|
9.5 创建附属配置文件
9.5.1 创建status.conf配置文件管理status页面
vim /webserver/nginx/conf/conf.d/status.conf
4 |
server_name status.nginxs.com status.heylinux.com; |
|
|
9.5.2 创建rainbow.conf配置文件管理heylinux.com
vim /webserver/nginx/conf/conf.d/rainbow.conf
04 |
server_name heylinux.com *.heylinux.com; |
|
|
05 |
index index.html index.htm index.php; |
|
|
06 |
root /webserver/blog/rainbow; |
|
|
09 |
if (-f $request_filename/index.html){ |
|
|
10 |
rewrite (.*) $1/index.html``break``; |
|
|
12 |
if (-f $request_filename/index.php){ |
|
|
13 |
rewrite (.*) $1/index.php; |
|
|
15 |
if (!-f $request_filename){ |
|
|
16 |
rewrite (.*) /index.php; |
|
|
20 |
location ~ .*.(php\|php5)?$ |
|
|
22 |
fastcgi_pass unix:/webserver/php/run/php-cgi.sock; |
|
|
23 |
fastcgi_index index.php; |
|
|
24 |
fastcgi_param SCRIPT_FILENAME /webserver/blog/rainbow$fastcgi_script_name; |
|
|
25 |
include /webserver/nginx/conf/fastcgi_params; |
|
|
28 |
location ~ .*.(gif\|jpg\|jpeg\|png\|bmp\|swf)$ |
|
|
33 |
location ~ .*.(js\|css)?$ |
|
|
38 |
log_format rainbow_access``'$remote_addr - $remote_user [$time_local] "$request" ' |
|
|
39 |
'$status $body_bytes_sent "$http_referer" ' |
|
|
40 |
'"$http_user_agent" $http_x_forwarded_for'``; |
|
|
41 |
access_log /webserver/blog/logs/rainbow_access.log rainbow_access; |
|
|
9.5.3 创建eric.conf配置文件管理nginxs.com
vim /webserver/nginx/conf/conf.d/eric.conf
04 |
server_name nginxs.com *.nginxs.com; |
|
|
05 |
index index.html index.htm index.php; |
|
|
06 |
root /webserver/blog/eric; |
|
|
09 |
if (-f $request_filename/index.html){ |
|
|
10 |
rewrite (.*) $1/index.html``break``; |
|
|
12 |
if (-f $request_filename/index.php){ |
|
|
13 |
rewrite (.*) $1/index.php; |
|
|
15 |
if (!-f $request_filename){ |
|
|
16 |
rewrite (.*) /index.php; |
|
|
20 |
location ~ .*.(php\|php5)?$ |
|
|
22 |
fastcgi_pass unix:/webserver/php/run/php-cgi.sock; |
|
|
23 |
fastcgi_index index.php; |
|
|
24 |
fastcgi_param SCRIPT_FILENAME /webserver/blog/eric$fastcgi_script_name; |
|
|
25 |
include /webserver/nginx/conf/fastcgi_params; |
|
|
28 |
location ~ .*.(gif\|jpg\|jpeg\|png\|bmp\|swf)$ |
|
|
33 |
location ~ .*.(js\|css)?$ |
|
|
38 |
log_format eric_access``'$remote_addr - $remote_user [$time_local] "$request" ' |
|
|
39 |
'$status $body_bytes_sent "$http_referer" ' |
|
|
40 |
'"$http_user_agent" $http_x_forwarded_for'``; |
|
|
41 |
access_log /webserver/blog/logs/eric_access.log eric_access; |
|
|
9.6 创建Nginx服务管理脚本
touch /webserver/init.d/nginx
chmod +x /webserver/init.d/nginx
vim /webserver/init.d/nginx
003 |
# nginx - this script starts and stops the nginx daemon |
|
|
006 |
# description: Nginx is an HTTP(S) server, HTTP(S) reverse |
|
|
007 |
# proxy and IMAP/POP3 proxy server |
|
|
009 |
# config: /etc/nginx/nginx.conf |
|
|
010 |
# config: /etc/sysconfig/nginx |
|
|
011 |
# pidfile: /var/run/nginx.pid |
|
|
013 |
# Source function library. |
|
|
014 |
. /etc/rc.d/init.d/functions |
|
|
016 |
# Source networking configuration. |
|
|
017 |
. /etc/sysconfig/network |
|
|
019 |
# Check that networking is up. |
|
|
020 |
[``"$NETWORKING" =``"no" ] &&``exit 0 |
|
|
022 |
nginx=``"/webserver/nginx/sbin/nginx" |
|
|
023 |
prog=$(``basename $nginx) |
|
|
025 |
NGINX_CONF_FILE=``"/webserver/nginx/conf/nginx.conf" |
|
|
027 |
lockfile=/var/lock/subsys/nginx |
|
|
030 |
# make required directories |
|
|
031 |
user=`$nginx -V 2>&1 \|```grep` `"configure arguments:"` `\|sed‘s/[^]–user=([^ ])./1/g’` “-“` |
|
|
032 |
if [ -z```"grep $user /etc/passwd"``];`then |
|
|
033 |
useradd -M -s /bin/nologin $user |
|
|
035 |
`options=$nginx -V 2>&1 |``grep `’configure arguments:’“““ |
|
|
036 |
for opt``in $options;``do |
|
|
037 |
if [ ````echo` `$opt \|grep‘.*-temp-path’` ];then` |
|
|
038 |
value=````echo` `$opt \|cut-d"="`-f 2“` |
|
|
039 |
if [ ! -d``"$value" ];``then |
|
|
040 |
# echo "creating" $value |
|
|
041 |
mkdir -p $value &&``chown -R $user $value |
|
|
048 |
[ -x $nginx ] \|\|``exit 5 |
|
|
049 |
[ -f $NGINX_CONF_FILE ] \|\|``exit 6 |
|
|
051 |
echo -n $``"Starting $prog: " |
|
|
052 |
daemon $nginx -c $NGINX_CONF_FILE |
|
|
055 |
[ $retval -``eq 0 ] &&``touch $lockfile |
|
|
060 |
echo -n $``"Stopping $prog: " |
|
|
064 |
[ $retval -``eq 0 ] &&``rm -f $lockfile |
|
|
069 |
configtest \|\|``return $? |
|
|
076 |
configtest \|\|``return $? |
|
|
077 |
echo -n $``"Reloading $prog: " |
|
|
088 |
$nginx -t -c $NGINX_CONF_FILE |
|
|
096 |
rh_status >/dev/null 2>&1 |
|
|
101 |
rh_status_q &&``exit 0 |
|
|
105 |
rh_status_q \|\|``exit 0 |
|
|
112 |
rh_status_q \|\|``exit 7 |
|
|
121 |
condrestart\|try-restart) |
|
|
122 |
rh_status_q \|\|``exit 0 |
|
|
125 |
echo $``"Usage: $0 {start\|stop\|status\|restart\|condrestart\|try-restart\|reload\|force-reload\|configtest}" |
|
|
9.7 启动Nginx服务
/webserver/init.d/nginx start
10. 优化系统内核参数
vim /etc/sysctl.conf
02 |
net.ipv4.tcp_max_syn_backlog = 65536 |
|
|
03 |
net.core.netdev_max_backlog = 32768 |
|
|
04 |
net.core.somaxconn = 32768 |
|
|
05 |
net.ipv4.tcp_timestamps = 0 |
|
|
06 |
net.ipv4.tcp_synack_retries = 2 |
|
|
07 |
net.ipv4.tcp_syn_retries = 2 |
|
|
08 |
net.ipv4.tcp_tw_recycle = 1 |
|
|
09 |
net.ipv4.tcp_tw_reuse = 1 |
|
|
10 |
net.ipv4.ip_local_port_range = 1024 65535 |
|
|
sysctl -p
vim /etc/security/limits.conf
11. 创建交换分区
dd if=/dev/zero of=/opt/swapfile bs=1M count=256
mkswap /opt/wapfile
swapon /opt/swapfile
vi /etc/fstab
1 |
/opt/swapfile swap swap defaults 0 0 |
|
|
12. 配置logrotate切割日志
yum install -y logrotate
vim /etc/logrotate.d/blog
01 |
/webserver/blog/logs/rainbow_access.log |
|
|
02 |
/webserver/blog/logs/eric_access.log |
|
|
11 |
if [ -f /webserver/nginx/run/nginx.pid ];``then |
|
|
12 |
/bin/``kill -HUP `/bin/```cat`/webserver/nginx/run/nginx.pid“` |
|
|
13. 结束
相关文章
转载请注明:爱开源 » Nginx+PHP(FastCGI)+MySQL在小内存VPS(t1.micro)上的安装配置与优化
转载请注明:爱开源 » Nginx+PHP(FastCGI)+MySQL在小内存VPS(t1.micro)上的安装配置与优化