一、安装 nginx
1. # yum install gcc openssl-devel pcre-devel zlib-devel
2. # yum groupinstall 'Development Tools' 'Development Libraries' -y
3.# groupadd nginx
# useradd -g nginx -s /bin/false -M nginx
4. # tar xvf nginx-0.8.20.tar.gz
# cd nginx-0.8.20
./configure \
--prefix=/usr \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/tmp/nginx/client/ \
--http-proxy-temp-path=/var/tmp/nginx/proxy/ \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/
make && make install
#########################################################################################
关于配置选项的简单说明:
--prefix=<path> - The path relative to which all other Nginx paths will resolve. If not
specified, defaults to /usr/local/nginx.
--sbin-path=<path> - The path to the nginx executable. Only used for installation. If not
specified defaults to <prefix>/sbin/nginx.
--conf-path=<path> - The default location of nginx.conf if no -c parameter is provided. If
not provided, defaults to <prefix>/conf/nginx.conf.
--pid-path=<path> - The path to nginx.pid, if not set via the "pid" directive in nginx.conf.
If not provided, defaults to <prefix>/logs/nginx.pid.
--error-log-path=<path> - The location of the error log if not set via the "error_log" in
nginx.conf. If not set, defaults to <prefix>/logs/error.log.
--http-log-path=<path> - The location of the access log if not set via the "access_log"
directive in nginx.conf. If not set, defaults to <prefix>/logs/access.log.
--user=<user> - The default user that nginx will run as if not set in nginx.conf via the
"user" directive. If not set, defaults to "nobody".
--group=<group> - The default group that nginx will run under if not set via the "user"
directive in nginx.conf. If not set defaults to "nobody".
--with-http_ssl_module - Enable ngx_http_ssl_module. Enables SSL support and the ability to
handle HTTPS requests. Requires OpenSSL. On Debian, this is libssl-dev.
--with-http_flv_module - Enable ngx_http_flv_module
--http-client-body-temp-path=PATH - Set path to the http client request body temporary files.
If not set, defaults to <prefix>/client_body_temp
--http-proxy-temp-path=PATH - Set path to the http proxy temporary files. If not set,
defaults to <prefix>/proxy_temp
--http-fastcgi-temp-path=PATH - Set path to the http fastcgi temporary files. If not set,
defaults to <prefix>/fastcgi_temp
--lock-path=<path> - The path to the nginx.lock file. If not provided, defaults to
<prefix>/logs/nginx.lock.
############################################################################################
Red Hat Nginx Init Script Should work on RHEL, Fedora, CentOS. Tested on CentOS 5.
5.
Save this file as /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g'
-`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-
reload|configtest}"
exit 2
esac
6.
# chmod +x /etc/init.d/nginxd
# chkconfig --add nginxd
# chkconfig nginxd on
# service nginxd start
7. 测试 http://192.168.0.37/
可以看到 Welcome to nginx!
修改路径
8. # mkdir -pv /www/htdocs
9. # vim /etc/nginx/nginx.conf
修改
user nginx
location / {
root /www/htdocs;
index index.html index.htm;
}
10. echo '<h1>My nginx</h1>' > /www/htdocs/index.html
11. service nginxd restart
12. 测试 http://192.168.0.37/
二、 安装mysql
1. tar xvf mysql-5.1.45-linux-i686-glibc23.tar.gz -C /usr/local/
2. # cd /usr/local/
# ln -sv mysql-5.1.45-linux-i686-glibc23/ mysql
3. 添加用户和组
# groupadd mysql
# useradd -g mysql -s /sbin/nologin -M mysql
4.
# cd mysql
# chown mysql:mysql . -R
# scripts/mysql_install_db --user=mysql (初始化)
# chown -R root .
# chown -R mysql data/
5.
# bin/mysqld_safe --user=mysql
# netstat -tnlp (查看3306端口是否打开)
6. 加载mysql的库文件
# vim /etc/ld.so.conf.d/mysql.conf
添加库文件路径 “/usr/local/mysql/lib”
# ldconfig -v (加载 )
7.
加载开发文件
# ln -sv /usr/local/mysql/include/ /usr/include/mysql
8.
启动文件
# cp support-files/mysql.server /etc/init.d/mysqld
# chkconfig --add mysqld (加到自动启动队列)
# chkconfig mysqld on (开机自动启动)
9. 调整配置文件
# cp support-files/my-large.cnf /etc/my.cnf
10. 验证 重启服务
service mysqld restart
11.
# vim /etc/profile
找到 ‘export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC’
在前一行添加
PATH=$PATH:/usr/local/mysql/bin
# . /etc/profile
# mysql (即可进入mysql的命令行)
三、 安装php
1. 安装libevent (调度文件时依赖于这个库文件)
# tar xvf libevent-1.4.14b-stable.tar.gz
# cd libevent-1.4.14b-stable
# ./configure
# make && make install
2 安装 libiconv(建立会话是用来实现网络解析方式的一种组件,有利于加速网络访问)
# tar xvf libiconv-1.13.1.tar.gz
# cd libiconv-1.13.1
# ./configure
# make && make install
3. 安装php
# tar xvf php-5.3.3.tar.bz2
# cd php-5.3.3
# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --
enable-fpm --with-libevent-dir=/usr/local --with-mysqli=/usr/local/mysql/bin/mysql_config --
enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml
-dir=/usr --enable-xml --with-iconv-dir=/usr/local
# make ZEND_EXTRA_LIBS='-liconv'
# make install
# cp php.ini-production /usr/local/php/etc/php.ini
# cd /usr/local/php/etc/
# cp php-fpm.conf.default php-fpm.conf
# vim php-fpm.conf
启用
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
启动服务:
# /usr/local/php/sbin/php-fpm
查看9000端口是否打开
设置开机启动
# vim /etc/rc.d/rc.local
加入 /usr/local/php/sbin/php-fpm
四、配置
1、 编辑主配置文件
# vim /etc/nginx/nginx.conf
修改一下两段内容为:
location / {
root /www/htdocs;
index index.php index.html index.htm;
}
location ~ \.php$ {
root /www/htdocs;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
# vim /etc/nginx/fastcgi_params
修改为:
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;
2. # service nginxd restart
# vim /www/htdocs/index.php
<?php
phpinfo()
?>
测试: http://192.168.0.37/