您的位置:首页 > 运维架构 > Nginx

ubuntu编译安装php5 mysql nginx

2014-03-20 17:36 369 查看
一、首先下载软件源码包

wget http://sysoev.ru/nginx/nginx-0.8.53.tar.gz
wget http:/.s135.com/soft/linux/nginx_php/php/php-5.2.10.tar.gz

wget http:/.s135.com/soft/linux/nginx_php/phpfpm/php-5.2.10-fpm-0.5.11.diff.gz

wget http:/.s135.com/soft/linux/nginx_php/mysql/mysql-5.1.38.tar.gz

wget http:/.s135.com/soft/linux/nginx_php/libiconv/libiconv-1.13.tar.gz

wget http:/.s135.com/soft/linux/nginx_php/mcrypt/libmcrypt-2.5.8.tar.gz

wget http:/.s135.com/soft/linux/nginx_php/mcrypt/mcrypt-2.6.8.tar.gz

wget http:/.s135.com/soft/linux/nginx_php/memcache/memcache-2.2.5.tgz

wget http:/.s135.com/soft/linux/nginx_php/mhash/mhash-0.9.9.9.tar.gz

wget http:/.s135.com/soft/linux/nginx_php/pcre/pcre-7.9.tar.gz

wget http:/.s135.com/soft/linux/nginx_php/eaccelerator/eaccelerator-0.9.5.3.tar.bz2

wget http:/.s135.com/soft/linux/nginx_php/pdo/PDO_MYSQL-1.0.2.tgz

wget http:/.s135.com/soft/linux/nginx_php/imagick/ImageMagick.tar.gz

wget http:/.s135.com/soft/linux/nginx_php/imagick/imagick-2.2.2.tgz

二、安装PHP 5.2.10(FastCGI模式)

1、首先安装PHP 5.2.10所需的支持库

tar zxvf libiconv-1.13.tar.gz

cd libiconv-1.13/

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

cd ..

#在make中途出错的话, apt-get install g++ ,然后重新再做一边

ln -s /usr/local/lib/libmcrypt.la  /usr/lib/libmcrypt.la

ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so

ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4

ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8

ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a

ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la

ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so

ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2

ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1

tar zxvf mcrypt-2.6.8.tar.gz

cd mcrypt-2.6.8/

/sbin/ldconfig

./configure

make

make install

cd ..

2、编译安装MySQL 5.1.38

添加mysql用户组和用户

/usr/sbin/groupadd mysql

/usr/sbin/useradd -g mysql mysql

tar zxvf mysql-5.1.38.tar.gz

cd mysql-5.1.38/

./configure –prefix=/usr/local/webserver/mysql/ –enable-assembler –with-extra-charsets=complex –enable-thread-safe-client –with-big-tables –with-readline –with-ssl –with-embedded-server –enable-local-infile –with-plugins=innobase

make && make install

chmod +w /usr/local/webserver/mysql

chown -R mysql:mysql /usr/local/webserver/mysql

cd ..

#如果编译过程中出现如下错误

#checking for tgetent in -ltermcap… no

#checking for termcap functions library… configure: error: No curses/termcap library found

#说明 curses/termcap 库没有安装去下载一个ncurses-5.6.tar.gz,

#wget http://ftp.gnu.org/pub/gnu/ncurses/ncurses-5.6.tar.gz
#tar zxvf ncurses-5.6.tar.gz

#cd ncurses-5.6

# ./configure –prefix=/usr –with-shared –without-debug

# make

# make install clean

#然后再重新编译Mysql进行安装。

#如果又遇到如下错误:

make[2]: *** [do_abi_check] 错误 1

make[2]: Leaving directory `/tmp/mysql-5.5.3-m3′

make[1]: *** [abi_check] 错误 2

make[1]: Leaving directory `/tmp/mysql-5.5.3-m3′

make: *** [all-recursive] 错误 1

#在网上搜了一下,说是Mysql的一个Bug,解决方法是打开Makefile将do_abi_check:后面的语句删除,注意do_abi_check:需要留下来,然后再 make && make install

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

1)、创建MySQL数据库存放目录

mkdir -p /var/webserver/mysql/3306/data

chown -R mysql:mysql /var/webserver/mysql/3306

2)、以mysql用户帐号的身份建立数据表:

/usr/local/webserver/mysql/bin/mysql_install_db –basedir=/usr/local/webserver/mysql –datadir=/var/webserver/mysql/3306/data –user=mysql

3)、创建my.cnf配置文件:

vim /var/webserver/mysql/3306/my.cnf

输入以下内容:

[client]

default-character-set = utf8

port    = 3306

socket  = /tmp/mysql.sock

[mysql]

prompt=”(\u:blog.s135.com:)[\d]> ”

no-auto-rehash

[mysqld]

#default-character-set = utf8

user    = mysql

port    = 3306

socket  = /tmp/mysql.sock

basedir = /usr/local/webserver/mysql

datadir = /var/webserver/mysql/3306/data

open_files_limit    = 10240

back_log = 600

max_connections = 3000

max_connect_errors = 6000

table_cache = 614

external-locking = FALSE

max_allowed_packet = 32M

sort_buffer_size = 2M

join_buffer_size = 2M

thread_cache_size = 300

thread_concurrency = 8

query_cache_size = 32M

query_cache_limit = 2M

query_cache_min_res_unit = 2k

default-storage-engine = MyISAM

default_table_type = MyISAM

thread_stack = 192K

transaction_isolation = READ-COMMITTED

tmp_table_size = 246M

max_heap_table_size = 246M

long_query_time = 1

log_long_format

log-bin = /var/webserver/mysql/3306/binlog

binlog_cache_size = 4M

binlog_format = MIXED

max_binlog_cache_size = 8M

max_binlog_size = 512M

expire_logs_days = 7

key_buffer_size = 256M

read_buffer_size = 1M

read_rnd_buffer_size = 16M

bulk_insert_buffer_size = 64M

myisam_sort_buffer_size = 128M

myisam_max_sort_file_size = 10G

myisam_max_extra_sort_file_size = 10G

myisam_repair_threads = 1

myisam_recover

skip-name-resolve

#master-connect-retry = 10

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

server-id = 1

innodb_additional_mem_pool_size = 16M

innodb_buffer_pool_size = 2048M  #如果启动不了Mysql把它改为1024或者增大swap分区的大小

innodb_data_file_path = ibdata1:1024M: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

[mysqldump]

quick

max_allowed_packet = 32M

4)、创建管理MySQL数据库的shell脚本:

vim /etc/init.d/mysql

输入以下内容:

#!/bin/sh

mysql_port=3306

mysql_username=”root”

mysql_password=”root”

function_start_mysql()

{

printf “Starting MySQL…\n”

/bin/sh /usr/local/webserver/mysql/bin/mysqld_safe –defaults-file=/var/webserver/mysql/${mysql_port}/my.cnf 2>&1 > /dev/null &

}

function_stop_mysql()

{

printf “Stoping MySQL…\n”

/usr/local/webserver/mysql/bin/mysqladmin -u ${mysql_username} -p${mysql_password} -S /tmp/mysql.sock shutdown

}

function_restart_mysql()

{

printf “Restarting MySQL…\n”

function_stop_mysql

sleep 5

function_start_mysql

}

function_kill_mysql()

{

kill -9 $(ps -ef | grep ‘bin/mysqld_safe’ | grep ${mysql_port} | awk ‘{printf $2}’)

kill -9 $(ps -ef | grep ‘libexec/mysqld’ | grep ${mysql_port} | awk ‘{printf $2}’)

}

if [ "$1" = "start" ]; then

function_start_mysql

elif [ "$1" = "stop" ]; then

function_stop_mysql

elif [ "$1" = "restart" ]; then

function_restart_mysql

elif [ "$1" = "kill" ]; then

function_kill_mysql

else

printf “Usage: /var/webserver/mysql/${mysql_port}/mysql {start|stop|restart|kill}\n”

fi

5)、添加执行权限

sudo chmod +x /etc/init.d/mysql

6)、添加mysql服务(ubuntu),并启动服务添加自定义shell命令

#添加mysql服务

sudo update-rc.d mysql defaults 100

#启动mysql服务

service mysql start

#添加自写义shell命令,方便进入mysql

sudo vim ~/.bashrc

#在最后一行加入以下内容

mysql () {

/usr/local/webserver/mysql/bin/mysqlr

}

#让其立即生效

source ~/.bashrc

7)、登陆mysql以上添加好之后,直接输入mysql就可以登陆进去了

8)、输入以下SQL语句,创建一个具有root权限的用户

GRANT ALL PRIVILEGES ON *.* TO ‘root’@'localhost’ IDENTIFIED BY ”;

GRANT ALL PRIVILEGES ON *.* TO ‘root’@’127.0.0.1′ IDENTIFIED BY ”;

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

tar zxvf php-5.2.10.tar.gz

gzip -cd php-5.2.10-fpm-0.5.11.diff.gz | patch -d php-5.2.10 -p1

cd php-5.2.10/

./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 –without-pear

#如果出现下面的错误提示

#checking for xml2-config… no

#checking for xml-config… no

#configure: error: XML configuration could not be found

#安装  apt-get install libxml2-dev

#如果出现下面的错误提示

#checking for pkg-config… /usr/bin/pkg-config

#configure: error: Cannot find OpenSSL’s <evp.h>

#安装   apt-get install libssl-dev”

#如果出现下面的错误提示

#checking for cURL in default path… not found

#configure: error: Please reinstall the libcurl distribution –  easy.h should be in <curl-dir>/include/curl/

#安装:  apt-get install libcurl4-gnutls-dev

#如果出现:

#configure: error: libjpeg.(a|so) not found.

#安装:  apt-get install libjpeg-dev

#如果出现:

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

#安装:  apt-get install libpng12-dev

#如果出现:configure: error: freetype.h not found.

#安装:  apt-get install libfreetype6-dev

#如果出现:configure: error: sasl.h not found!

#安装:  apt-get install libsasl2-dev

如果出现:

+——————————————————————–+

|                       *** ATTENTION ***                            |

|                                                                    |

| Something is likely to be messed up here, because the configure    |

| script was not able to detect a simple feature on your platform.   |

| This is often caused by incorrect configuration parameters. Please |

| see the file debug.log for error messages.                         |

|                                                                    |

| If you are unable to fix this, send the file debug.log to the      |

| php-install@lists.php.net mailing list and include appropiate      |

| information about your setup.                                      |

+——————————————————————–+

| License:                                                           |

| This software is subject to the PHP License, available in this     |

| distribution in the file LICENSE.  By continuing this installation |

| process, you are bound by the terms of this license agreement.     |

| If you do not agree with the terms of this license, you must abort |

| the installation process at this point.                            |

+——————————————————————–+

Thank you for using PHP.

#到php安装程序目录(编译PHP的目录)去查看debug.log最后几行如果有以下警告

A   conftest.c:3: warning: incompatible implicit declaration of built-in function ‘exit’

B   ./conftest: error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or directory

#增加LD_LIBRARY_PATH=/usr/local/mysql/lib/mysql到环境变量,方法为编辑/etc/profile文 件,添 加:export LD_LIBRARY_PATH=/usr/local/webserver/mysql/lib/mysql

#如果不想重启使用 . /etc/profile 就可以了,注意.和/etc中间有空格,然后再重新编译

make ZEND_EXTRA_LIBS=’-liconv’

make install

#复制配置好的php.ini到/usr/local/webserver/php/etc/目录下

cp php.ini-dist /usr/local/webserver/php/etc/php.ini

cd ..

#curl http://pear.php.net/go-pear | /usr/local/webserver/php/bin/php

4、编译安装PHP5扩展模块

tar zxvf memcache-2.2.5.tgz

cd memcache-2.2.5/

/usr/local/webserver/php/bin/phpize

./configure –with-php-config=/usr/local/webserver/php/bin/php-config

make

make install

cd ..

#如果出现:

#Cannot find autoconf. Please check your autoconf installation and the

#$PHP_AUTOCONF environment variable. Then, rerun this script.

#安装:

#sudo apt-get install m4

#sudo apt-get install autoconf

#或者直接:

#sudo apt-get install autoconf

#因为autoconf 依赖于m4,所以会自动下载解决这个依赖关系.

tar jxvf eaccelerator-0.9.5.3.tar.bz2

cd eaccelerator-0.9.5.3/

/usr/local/webserver/php/bin/phpize

./configure –enable-eaccelerator=shared –with-php-config=/usr/local/webserver/php/bin/php-config

make

make install

cd ..

tar zxvf PDO_MYSQL-1.0.2.tgz

cd PDO_MYSQL-1.0.2/

/usr/local/webserver/php/bin/phpize

./configure –with-php-config=/usr/local/webserver/php/bin/php-config –with-pdo-mysql=/usr/local/webserver/mysql

make

make install

cd ..

tar zxvf ImageMagick.tar.gz

cd ImageMagick-6.5.1-2/

./configure

make

make install

cd ..

#如果执行make时出错,信息提示如下:

#/usr/bin/ld: cannot find -lperl

#collect2: ld returned 1 exit status

#make[2]: *** [blib/arch/auto/Image/Magick/Magick.so] 错误 1

#make[2]:正在离开目录 `/home/ouyangjunqiu/下载/ImageMagick-6.5.1-2/PerlMagick’

#make[1]: *** [all-perl] 错误 2

#make[1]:正在离开目录 `/home/ouyangjunqiu/下载/ImageMagick-6.5.1-2′

#make: *** [all] 错误 2

#有两种解决方法

#1.在配置的时候加个。。

#./configure –without-perl

#2.安装库文件

#apt-get install libperl-dev

#安装完成后验证一下是否能正常工作输入convert -version 如果出现如下错误:

convert: error while loading shared libraries: libMagickCore.so.2: cannot open shared object file: No such file or directory

#我们需要把安装目录下缺失的文件,用ln链接到/usr/lib目录下

#ln -s /usr/local/lib/libMagickCore.so.2 /usr/lib

#然后可以使用convert  xx.jpg  xx.gif 把jpg的图片转换为gif看看能不能正常使用

tar zxvf imagick-2.2.2.tgz

cd imagick-2.2.2/

/usr/local/webserver/php/bin/phpize

./configure –with-php-config=/usr/local/webserver/php/bin/php-config

make

make install

cd ..

5、修改php.ini文件

手工修改:查找/usr/local/webserver/php/etc/php.ini中的extension_dir = “./”

修改为extension_dir = “/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/”

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

extension = “memcache.so”

extension = “pdo_mysql.so”

extension = “imagick.so”

再查找output_buffering = Off

修改为output_buffering = On

6、配置eAccelerator加速PHP:

mkdir -p /usr/local/webserver/eaccelerator_cache

vi /usr/local/webserver/php/etc/php.ini

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

[eaccelerator]

zend_extension=”/usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/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用户和组:

/usr/sbin/groupadd www

/usr/sbin/useradd -g www www

mkdir -p /var/webserver/www

chmod +w /var/webserver/www

chown -R www:www /var/webserver/www

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

在/usr/local/webserver/php/etc/目录中创建php-fpm.conf文件:

rm -rf /usr/local/webserver/php/etc/php-fpm.conf

vi /usr/local/webserver/php/etc/php-fpm.conf

输入以下内容(如果您安装 Nginx + PHP 用于程序调试,请将以下的<value name=”display_errors”>0</value>改为<value name=”display_errors”>1</value>,以便显示PHP错误信息,否则,Nginx 会报状态为500的空白错误页):

<?xml version=”1.0″ ?>

<configuration>

All relative paths in this config are relative to php’s install prefix

<section name=”global_options”>

Pid file

<value name=”pid_file”>/usr/local/webserver/php/logs/php-fpm.pid</value>

Error log file

<value name=”error_log”>/usr/local/webserver/php/logs/php-fpm.log</value>

Log level

<value name=”log_level”>notice</value>

When this amount of php processes exited with SIGSEGV or SIGBUS …

<value name=”emergency_restart_threshold”>10</value>

… in a less than this interval of time, a graceful restart will be initiated.

Useful to work around accidental curruptions in accelerator’s shared memory.

<value name=”emergency_restart_interval”>1m</value>

Time limit on waiting child’s reaction on signals from master

<value name=”process_control_timeout”>5s</value>

Set to ‘no’ to debug fpm

<value name=”daemonize”>yes</value>

</section>

<workers>

<section name=”pool”>

Name of pool. Used in logs and stats.

<value name=”name”>default</value>

Address to accept fastcgi requests on.

Valid syntax is ‘ip.ad.re.ss:port’ or just ‘port’ or ‘/path/to/unix/socket’

<value name=”listen_address”>127.0.0.1:9000</value>

<value name=”listen_options”>

Set listen(2) backlog

<value name=”backlog”>-1</value>

Set permissions for unix socket, if one used.

In Linux read/write permissions must be set in order to allow connections from web server.

Many BSD-derrived systems allow connections regardless of permissions.

<value name=”owner”></value>

<value name=”group”></value>

<value name=”mode”>0666</value>

</value>

Additional php.ini defines, specific to this pool of workers.

<value name=”php_defines”>

<value name=”sendmail_path”>/usr/sbin/sendmail -t -i</value>

<value name=”display_errors”>1</value>

</value>

Unix user of processes

<value name=”user”>www</value>

Unix group of processes

<value name=”group”>www</value>

Process manager settings

<value name=”pm”>

Sets style of controling worker process count.

Valid values are ‘static’ and ‘apache-like’

<value name=”style”>static</value>

Sets the limit on the number of simultaneous requests that will be served.

Equivalent to Apache MaxClients directive.

Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi

Used with any pm_style.

<value name=”max_children”>128</value>

Settings group for ‘apache-like’ pm style

<value name=”apache_like”>

Sets the number of server processes created on startup.

Used only when ‘apache-like’ pm_style is selected

<value name=”StartServers”>20</value>

Sets the desired minimum number of idle server processes.

Used only when ‘apache-like’ pm_style is selected

<value name=”MinSpareServers”>5</value>

Sets the desired maximum number of idle server processes.

Used only when ‘apache-like’ pm_style is selected

<value name=”MaxSpareServers”>35</value>

</value>

</value>

The timeout (in seconds) for serving a single request after which the worker process will be terminated

Should be used when ‘max_execution_time’ ini option does not stop script execution for some reason

’0s’ means ‘off’

<value name=”request_terminate_timeout”>0s</value>

The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file

’0s’ means ‘off’

<value name=”request_slowlog_timeout”>0s</value>

The log file for slow requests

<value name=”slowlog”>logs/slow.log</value>

Set open file desc rlimit

<value name=”rlimit_files”>65535</value>

Set max core size rlimit

<value name=”rlimit_core”>0</value>

Chroot to this directory at the start, absolute path

<value name=”chroot”></value>

Chdir to this directory at the start, absolute path

<value name=”chdir”></value>

Redirect workers’ stdout and stderr into main error log.

If not set, they will be redirected to /dev/null, according to FastCGI specs

<value name=”catch_workers_output”>yes</value>

How much requests each process should execute before respawn.

Useful to work around memory leaks in 3rd party libraries.

For endless request processing please specify 0

Equivalent to PHP_FCGI_MAX_REQUESTS

<value name=”max_requests”>102400</value>

Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect.

Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi (5.2.2+)

Makes sense only with AF_INET listening socket.

<value name=”allowed_clients”>127.0.0.1</value>

Pass environment variables like LD_LIBRARY_PATH

All $VARIABLEs are taken from current environment

<value name=”environment”>

<value name=”HOSTNAME”>$HOSTNAME</value>

<value name=”PATH”>/usr/local/bin:/usr/bin:/bin</value>

<value name=”TMP”>/tmp</value>

<value name=”TMPDIR”>/tmp</value>

<value name=”TEMP”>/tmp</value>

<value name=”OSTYPE”>$OSTYPE</value>

<value name=”MACHTYPE”>$MACHTYPE</value>

<value name=”MALLOC_CHECK_”>2</value>

</value>

</section>

</workers>

</configuration>

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

ulimit -SHn 65535

/usr/local/webserver/php/sbin/php-fpm start

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

三、安装Nginx 0.8.53

1、安装Nginx所需的pcre库:

tar zxvf pcre-7.9.tar.gz

cd pcre-7.9/

./configure

make && make install

cd ..

2、安装Nginx

tar zxvf nginx-0.8.53.tar.gz

cd nginx-0.8.53/

./configure –user=www –group=www –prefix=/usr/local/webserver/nginx –with-http_stub_status_module –with-http_ssl_module

make && make install

cd ..

3、创建Nginx日志目录

mkdir -p /var/log/nginx

chmod +w /var/log/nginx

chown -R www:www /var/log/nginx

4、创建Nginx配置文件

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

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

vi /usr/local/webserver/nginx/conf/nginx.conf

输入以下内容:

user  www www;

worker_processes 2;

error_log  /var/log/nginx/nginx_error.log;

pid        /var/run/nginx.pid;

events {

worker_connections  1024;

}

http {

include       /usr/local/webserver/nginx/conf/mime.types;

default_type  application/octet-stream;

log_format  main  ’$remote_addr – $remote_user [$time_local] “$request “‘

‘$status $body_bytes_sent “$http_referer” ‘

‘”$http_user_agent” “$http_x_forwarded_for”‘;

access_log  /var/log/nginx/access.log  main;

sendfile        on;

autoindex off;

map $scheme $fastcgi_https { ## Detect when HTTPS is used

default off;

https on;

}

keepalive_timeout  10;

fastcgi_connect_timeout 300;

fastcgi_send_timeout 300;

fastcgi_read_timeout 300;

fastcgi_buffers 8 128k;

gzip  on;

gzip_comp_level 2;

gzip_proxied any;

#gzip_types   text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;

# Load config files from the /usr/local/webserver/nginx/conf/conf.d directory

include  /usr/local/webserver/nginx/conf/conf.d/*.conf;

}

添加虚似主机(注:因本人要使用Magento所以下面有的东西是给Magento写的,这只是个例子)

vim /usr/local/webserver/nginx/conf/conf.d/zozhang.test.conf

写下配置文件

server {

listen 80;

server_name zozhang.test;

#下面这包主要是给没有加www的添加www,由于是本地测试,就不启用了。

#rewrite / $scheme://www.$host$request_uri permanent; ## Forcibly prepend a www

}

server {

listen 80 default; #default 所的配置文件中,只能定义一个;

server_name zozhang.test; ## Domain is here twice so server_name_in_redirect will favour the www

root /var/webserver/www/test;

location / {

index index.html index.php; ## Allow a static html file to be shown first

try_files $uri $uri/ @handler; ## If missing pass the URI to Magento’s front handler

expires 30d; ## Assume all files are cachable

}

location /minify/ { ## Needed for Fooman Speedster

rewrite ^/minify/([0-9]+)(/.*\.(js|css))$ /lib/minify/m.php?f=$2&d=$1 last;

}

## These locations would be hidden by .htaccess normally

location /app/                { deny all; }

location /includes/           { deny all; }

location /lib/                { deny all; }

location /lib/minify/         { allow all; }  ## Deny is applied after rewrites so must specifically allow minify

location /media/downloadable/ { deny all; }

location /pkginfo/            { deny all; }

location /report/config.xml   { deny all; }

location /var/                { deny all; }

location /var/export/ { ## Allow admins only to view export folder

auth_basic           “Restricted”; ## Message shown in login window

auth_basic_user_file htpasswd; ## See /usr/local/webserver/nginx/confhtpassword

autoindex            on;

}

location  /. { ## Disable .htaccess and other hidden files

return 404;

}

location @handler { ## Magento uses a common front handler

rewrite / /index.php;

}

location ~ \.php/ { ## Forward paths like /js/index.php/x.js to relevant handler

rewrite ^(.*\.php)/ $1 last;

}

location ~ \.php$ { ## Execute PHP scripts

expires        off; ## Do not cache dynamic content

fastcgi_pass   127.0.0.1:9000;

fastcgi_param  HTTPS $fastcgi_https;

fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

include        fastcgi_params; ## See /usr/local/webserver/nginx/conf/fastcgi_params

}

}

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

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

ulimit -SHn 65535

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

#启动过程中如果提示:/usr/local/webserver/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.0: cannot open shared object file: No such file or directory

#解决方法,在/lib中创建一个symbol link到/usr/local/lib/libpcre.so.0

# ln -s  /usr/local/lib/libpcre.so.0  /lib

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

vi /etc/rc.local

在末尾增加以下内容:

ulimit -SHn 65535

/usr/local/webserver/php/sbin/php-fpm start

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

创建Nginx管理脚本(启动、停止、配置、重启)

################################################

#!/bin/bash

# v.0.0.3

# create by jackbillow at 2007.10.15

# redevelop by XGuru at 2010.6.28

# On Ubuntu 10.04

# nginx – This shell script takes care of starting and stopping nginx.

#

# description: nginx [engine x] is light http web/proxy server

# that answers incoming ftp service requests.

###############################################

nginx_path=”/usr/local/webserver/nginx/”  #配置nginx安装路径

nginx_pid=”/usr/local/webserver/nginx/logs/nginx.pid”

prog=”nginx”

RETVAL=0

start() {

# Start daemons.

if [ -e $nginx_path/conf/nginx.conf ];then

echo -n $”Starting $prog: ”

$nginx_path/sbin/nginx -c $nginx_path/conf/nginx.conf &

RETVAL=$?

[ $RETVAL -eq 0 ] && {

echo Start “$prog” successfully!

}

else

RETVAL=1

fi

return $RETVAL

}

# Stop daemons.

stop() {

echo -n $”Stopping $prog\n”

sudo killall -9 nginx

RETVAL=$?

}

# See how we were called.

conf(){

vim “$nginx_path/conf/nginx.conf”   #这里请设置要使用什么方式打开配置文件,我使用的是vim

}

case “$1″ in

start)

start

;;

stop)

stop

;;

conf)

conf

;;

restart)

stop

start

;;

*)

echo $”Usage: $0 {start|stop|conf|restart}”

echo $”Your may need root privilege to execute this script!”

exit 1

esac

exit $RETVAL

六、在不停止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主进程号:

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、创建脚本/usr/local/webserver/nginx/sbin/cut_nginx_log.sh

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访问日志

crontab -e

输入以下内容:

00 00 * * * /bin/bash  /usr/local/webserver/nginx/sbin/cut_nginx_log.sh
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: