nginx安装到配置文件详解
系统环境为CentOS6.5
准备源码包:nginx1.6
wget http://nginx.org/download/nginx-1.6.3.tar.gz
废话不多说咱们直接上代码
yum -y install prel openssl openssl-devel gcc pcre-devel zlib-devel //先解决以下nginx的依赖问题
useradd -M -s /sbin/nologin nginx //先创建一个nginx程序用户,调用咱们的nginx服务
tar zxvf nginx-1.6.0.tar.gz -C /usr/src/
cd /usr/src/nginx-1.6.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module //将nginx安装在/usr/local/nginx/目录下,后面--with个模块,想添加其他功能的可以去nginx官网看具体支持的模块。
make && make install //编译nginx
于是编译便完成啦!简单吧!
cd /usr/local/nginx
ls
conf:这个目录下面时nginx的一些模块功能
html:这个目录是nginx默认索引文件位置,你可以将自己的网页文件放在html下,nginx默认会去这个目录找网页根目录
logs:这个就是nginx的日志啦
sbin:这个下面是nginx自带的一个启动脚本
ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ //将启动脚本软链接到环境变量中
nginx //启动nginx
现在我们可以在任何目录下启动nginx啦
想要结束nginx比较麻烦,nginx没有自带的停止脚本,我们自己写一个shell脚本控制nginx的启动关闭
touch /etc/rc.d/init.d/nginx
vim /etc/rc.d/init.d/nginx
#!/bin/bash
#chkconfig: - 99 20
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case $1 in
start)
$PROG
echo "奥特曼变身成功!!!"
;;
stop)
kill -s QUIT $(cat $PIDF)
echo "奥特曼死啦!!!"
;;
restart)
$0 stop > /dev/null
$0 start > /dev/null
echo "奥特曼重新变身!!!"
;;
*)
echo "请输入 start|stop|restart"
exit 1
;;
esac
exit 0
保存退出
chmod +x /etc/rc.d/init.d/nginx
service nginx restart
netstat -anpt|grep nginx
echo "welcome to nginx --------咳咳" > /usr/local/nginx/html/index.html
firefox http://127.0.0.1
好啦!配置完成后,我们一起来看一看配置文件吧nginx.conf
vim /usr/local/nginx/conf/nginx.conf //优化nginx
worker_processes 1; //表示nginx服务调用几个进程,根据自己的cpu设置
events {
worker_connections 1024; //表示最大连接数量,默认1024,最大不可超过65535个
}
keepalive_timeout 65; //链接超时时间,单位秒
gzip on; //启动gzip压缩
location / {
root html; //页面根目录,可以更改为自己的项目路径
index index.html index.htm; //根路径下面的索引文件
}
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
d
o
c
u
m
e
n
t
r
o
o
t
document_root
documentrootfastcgi_script_name;
include fastcgi_params;
}