1, 编译及安装Nginx
1) 安装支持软件
[root@localhost ~]# yum -y install pcre-devel zlib-devel
注意:安装pcre是为了使nginx支持HTTP rewrite模块。
2)创建程序用户和程序组
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
3)编译安装Nginx
[root@localhost ~]# cd /aaa/
[root@localhost aaa]# tar zxf nginx-1.0.8.tar.gz
[root@localhost aaa]# cd nginx-1.0.8
[root@localhost nginx-1.0.8]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module ; make ; make install
[root@localhost nginx-1.0.8]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.0.8]# ls -l /usr/local/sbin/nginx
lrwxrwxrwx 1 root root 27 05-31 14:00 /usr/local/sbin/nginx -> /usr/local/nginx/sbin/nginx
2,Nginx的运行控制
1)检查配置文件
[root@localhost ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
2)启动、停止Nginx
[root@localhost ~]# nginx //启动nginx
[root@localhost ~]# netstat -anpt |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6504/nginx: master
[root@localhost ~]# killall -s HUP nginx //平滑重启nginx (reload) 等同于下个命令
[root@localhost ~]# killall -1 nginx
[root@localhost ~]# killall -s QUIT nginx //正常停止nginx (stop) 等同于下个命令
[root@localhost ~]# killall -3 nginx
[root@localhost ~]# killall -s USR1 nginx //用于nginx的日志切换,也就是重新打开一个日志文件,例如每天要生成一个日志文件时,可以使用这个信号来控制。
[root@localhost ~]# killall -s USR2 nginx //用于平滑升级可执行程序。
3)编写nginx服务器脚本
[root@localhost ~]# vi /etc/init.d/nginx
#!/bin/bash
# chkconfig: 2345 99 20
# description: Nginx Server Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 (start|stop|restart|reload)"
exit 1
esac
exit 0
[root@localhost ~]# chmod +x /etc/init.d/nginx
[root@localhost ~]# chkconfig --add nginx
[root@localhost ~]# chkconfig nginx on
[root@localhost ~]# chkconfig nginx --list
nginx 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
3,配置文件nginx.conf
[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# vi nginx.conf
#全局配置部分:main
2 user nginx nginx; //nginx的程序账户及程序组
3 worker_processes 1; //指定nginx要开启的进程数量,每个进程要耗费10M-12M内存之间,如果你的CPU是多核的,建议指定和CPU一样多的数量。
4
5 #error_log logs/error.log;
6 #error_log logs/error.log notice;//输出级别有:debug\info\notice\warn\error\crit
7 error_log logs/error.log info;
8
9 pid logs/nginx.pid; //指定进程id的存储位置。
10
#I/0事件配置 ,使用events {…..}界定标记
12 events { // 用来设定nginx的工作模式及连接数上限。
13 use epoll; // 用来设定nginx的工作模式。nginx的工作模式有select/poll/kqueue/epoll/rtsig和/dev/poll 其中select/poll是标准工作模式,kqueue/epoll是高效工作模式。不同是:epoll用于linux内核,kqueue用于BSD系统。对于epoll是linux的首选参数。
14 worker_connections 10240; //用于定义nginx每个进程的最大连接数,默认是1024,最大的客户端连接数:worker_processes × worker_connections = max_clients.进程的最大连接数受进程的最大打开文件数量限制。ulimit -n 65536
15 }
#http服务器配置部分 ,使用http{…..}界定标记
http {
include 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 logs/access.log main;//客户端访问日志位置。
sendfile on;//支持文件发送下载。
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;//连接保持超时时间。
#gzip on;
server { //定义虚拟主机开始
listen 80;//定义虚拟主机的服务器端口
server_name www.benet.com;//指定虚拟主机绑定的域名
charset utf-8; //指定默认字符集
#access_log logs/host.access.log main;
location / {
root html;//指定网站根目录的位置
index index.html index.htm;//指定默认首页地址
}
location ~ /status { //访问位置为/status
stub_status on; //打开状态统计功能
access_log off; //关闭此位置的日志记录
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@localhost conf]# ulimit -n 65535
[root@localhost conf]# service nginx restart
1)nginx的访问统计状态测试
2)基于域名的虚拟web主机
[root@localhost ~]# mkdir -p /var/www/benet
[root@localhost ~]# echo "
" > /var/www/benet/index.html
[root@localhost ~]# mkdir -p /var/www/accp
[root@localhost ~]# echo "
" > /var/www/accp/index.html
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
……. //省略全局配置
http {
………//省略部分信息
18 server {
19 listen 80;
20 server_name www.benet.com;
21 charset utf-8;
22 location / {
23 root /var/www/benet;
24 index index.html index.htm;
25 }
26 error_page 500 502 503 504 /50x.html;
27 location = /50x.html {
28 root html;
29 }
30 }
31 server {
32 listen 80;
33 server_name www.accp.com;
34 charset utf-8;
35 location / {
36 root /var/www/accp;
37 index index.html index.htm;
38 }
39 error_page 500 502 503 504 /50x.html;
40 location = /50x.html {
41 root html;
42 }
43 }
}
[root@localhost conf]# service nginx restart
测试:
4,LEMP架构及应用
1) 构建LEMP网站平台―MySql数据库
A,安装MySQL数据库
[root@localhost aaa]# cd /aaa
[root@localhost aaa]# tar zxf mysql-5.1.55.tar.gz
[root@localhost aaa]# cd mysql-5.1.55
[root@localhost mysql-5.1.55]# ./configure --prefix=/usr/local/mysql --with-charset=utf8 --with-collation=utf8_general_ci --with-extra-charsets=gbk,gb2312 ; make ; make install
B,安装后的调整和优化
[root@localhost mysql-5.1.55]# cp support-files/my-medium.cnf /etc/my.cnf
[root@localhost mysql-5.1.55]# cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql-5.1.55]# chmod +x /etc/init.d/mysqld
[root@localhost mysql-5.1.55]# chkconfig --add mysqld
[root@localhost mysql-5.1.55]# ln -s /usr/local/mysql/bin/* /usr/local/bin/
[root@localhost mysql-5.1.55]# ln -s /usr/local/mysql/lib/mysql/* /usr/lib
[root@localhost mysql-5.1.55]# ln -s /usr/local/mysql/include/mysql/* /usr/include/
C、初始化数据库
[root@localhost mysql-5.1.55]# useradd -M -u 27 -s /sbin/nologin mysql
[root@localhost mysql-5.1.55]# cd /usr/local/mysql/bin/
[root@localhost bin]# ./mysql_install_db --user=mysql
[root@localhost bin]# chown -R root:mysql /usr/local/mysql/
[root@localhost bin]# chown -R mysql /usr/local/mysql/var/
D、启动Mysql服务
[root@localhost bin]# service mysqld start
Starting MySQL. [确定]
[root@localhost bin]# mysqladmin -uroot password "123456"
2) 构建LEMP网站平台―安装PHP解析环境
A、编译安装PHP
[root@localhost php-5.3.6]# cd /aaa
[root@localhost php-5.3.6]# tar xf php-5.3.6.tar.gz
[root@localhost php-5.3.6]# cd php-5.3.6
[root@localhost php-5.3.6]# ./configure --prefix=/usr/local/php5 --with-gd --with-zlib --with-mysql=/usr/local/mysql --with-config-file-path=/usr/local/php5 --enable-mbstring --enable-fpm ; make ; make install
B、PHP安装后的调整
[root@localhost php-5.3.6]# cp php.ini-development /usr/local/php5/php.ini
[root@localhost php-5.3.6]# ln -s /usr/local/php5/bin/* /usr/local/bin/
[root@localhost php-5.3.6]# ln -s /usr/local/php5/sbin/* /usr/local/sbin/
C、安装ZendGuardLoader(它是PHP的一个优化模块,用于优化PHP程序的)
[root@localhost php-5.3.6]# cd /aaa/
[root@localhost aaa]# tar zxf ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz
[root@localhost aaa]# cd ZendGuardLoader-php-5.3-linux-glibc23-i386/php-5.3.x/
[root@localhost php-5.3.x]# cp ZendGuardLoader.so /usr/local/php5/lib/php/
[root@localhost php-5.3.x]# vi /usr/local/php5/php.ini
zend_extension=/usr/local/php5/lib/php/ZendGuardLoader.so
zend_loader.enable=1
下面配置Nginx支持PHP
D、启用PHP-fpm进程
[root@localhost php-5.3.x]# cd /usr/local/php5/etc/
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# vi php-fpm.conf
25 pid = run/php-fpm.pid //确认pid文件位置
122 user = nginx //程序用户
123 group = nginx //程序组
157 pm.start_servers = 20 //启动时开启的进程数
162 pm.min_spare_servers = 5 //最少空闲进程数
167 pm.max_spare_servers = 35 //最多空闲进程数
[root@localhost etc]# /usr/local/sbin/php-fpm
[root@localhost etc]# netstat -anpt |grep php-fpm
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 23289/php-fpm.conf)
[root@localhost run]# vi /etc/init.d/nginx
#!/bin/bash
# chkconfig: 2345 99 20
# description: Nginx Server Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
PROG_FPM="/usr/local/sbin/php-fpm"
PIDF_FPM="/usr/local/php5/var/run/php-fpm.pid"
case "$1" in
start)
$PROG
$PROG_FPM
;;
stop)
kill -s QUIT $(cat $PIDF)
kill -s QUIT $(cat $PIDF_FPM)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 (start|stop|restart|reload)"
exit 1
esac
exit 0
E、配置Nginx支持PHP解析
第一种:nginx依靠apache服务器进行php解析,将客户请求的php解析任务转交给apahce
server {
…. //省略部分
location ~ \.php$ { //访问.php页面的配置段
proxy_pass http://192.168.6.123:80; //apache服务器的监听地址
}
}
第二种:(调用本机的php-fpm进程)
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.benet.com;
charset utf-8;
location / {
root /var/www/benet;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root /var/www/benet;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
[root@localhost ~]# service nginx restart
F、PHP页面访问测试
[root@localhost ~]# vi /var/www/benet/test.php
$link=mysql_connect('localhost','root','123456');
if($link) echo "
";
mysql_close();
?>
测试结果
3)在LEMP平台中部署web应用
A、下载并部署程序代码
[root@localhost aaa]# tar jxf SKYUC_3.2.2_Free_For_PHP5.3.tar.bz2
[root@localhost aaa]# mv SKYUC_3.2.2_Free_For_PHP5.3/wwwroot /var/www/benet/skyuc
[root@localhost aaa]# cd /var/www/benet/skyuc/
[root@localhost skyuc]# chown -R nginx:nginx admincp/ data/ templates/ upload/
B、创建数据库
[root@localhost ~]# mysql -uroot -p123456
mysql> create database skyucdb;
Query OK, 1 row affected (0.05 sec)
mysql> grant all on skyucdb.* to runskyuc@localhost identified by 'admin123';
Query OK, 0 rows affected (0.04 sec)
C、安装web应用
[root@localhost benet]# cd /var/www/benet/skyuc/
[root@localhost skyuc]# mv install/ install.lock
[root@localhost skyuc]# chmod 600 install.lock/
以上文章 属于转载 图片中有防盗链技术 因此可能不完美 多多吐槽!