|
实验环境:
centos 6.6 【该节点IP为172.16.3.101】
配置好的yum源【可以把yum源指向搜狐或者阿里的镜像站点,也可以是自己的光盘】
编译安装配置nginx1.6
# 安装之前最好先把如下包组安装上
[iyunv@localhost httpd-2.2.29]# yum grouplist | grep -i 'develop'
Additional Development
Development tools
Server Platform Development
Desktop Platform Development
# 安装方法如下:
yum groupinstall 'Additional Development' 'Development tools' 'Server Platform Development' 'Desktop Platform Development'
# 获取nginx1.6二进制源码格式安装包
可以去如下链接下载源码安装包,我这里用的是1.6的,http://nginx.org/en/download.html
# 解压缩
tar xf nginx-1.6.2.tar.gz
cd nginx-1.6.2
# 由于nginx需要nginx系统用户启动,所以此处需要创建系统用户nginx
useradd -r nginx
# 安装三部曲
# ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --user=nginx --group=nginx --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 --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_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/fastcgi
--prefix # nginx安装目录
--conf-path # nginx配置文件路径
--user # 指定启动nginx的系统用户
--group # 指定启动nginx的系统组
--error-log-path # 错误日志文件路径
--http-log-path # 访问日志路径
--pid-path # pid文件路径
--lock-path # 锁文件路径
--with-http_ssl_module # 启用ssl模块
--with-http_stub_status_module # 启用stub_status_module模块
--with-http_gzip_static_module # 启用gzip_static_module模块
--with-http_flv_module # 启用流媒体模块
--with-http_mp4_module # 启用mp4流媒体模块
--http-client-body-temp-path # 客户端请求报文包体临时文件路径
--http-proxy-temp-path # 报文反向代理临时文件路径
--http-fastcgi-temp-path # 报文FastCGI临时文件路径
make
make install
# 添加PATH环境变量
vim /etc/profile.d/nginx.sh
# 向里面加入如下语句
export PATH=/usr/local/nginx/sbin:$PATH
# 然后source一下
. /etc/profile.d/nginx.sh
# 对nginx配置文件进行语法检查
[iyunv@localhost nginx-1.6.2]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] mkdir() "/var/tmp/nginx/client" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed
# 从上面可以看出其配置文件的语法是OK但是,有些目录在编译安装配置nginx的时候没有自动创建,此处需要手动创建其目录
# 创建目录
mkdir -pv /var/tmp/nginx/{client,proxy,fastcgi}
# 再进行检查
[iyunv@localhost nginx-1.6.2]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# 可以看出其语法OK
# 启动nginx
nginx
# 查看80端口是否开启
ss -tnl
# 也可以看一下nginx进程详细信息
ps aux | grep nginx
为nginx提供SysV init脚本:
# 新建文件/etc/rc.d/init.d/nginx.conf
vim /etc/rc.d/init.d/nginx.conf
# 向里面添加如下内容
#!/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/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/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
# 此处注意,定位至如下行
NGINX_CONF_FILE="/usr/local/nginx/nginx.conf"
# 把其修改为如下:主要是要修改配置文件路径
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
# 然后给其执行权限
chmod u+x /etc/nginx/nginx.conf
# 添加至chkconfig列表中
root@localhost nginx]# chkconfig --list nginx
service nginx supports chkconfig, but is not referenced in any runlevel (run 'chkconfig --add nginx')
[iyunv@localhost nginx]# chkconfig --add nginx
# 启动nginx服务
service nginx start
# 可以看到80端口已经打开
ss -tnl
# 测试,此时在远端浏览器中输入:http://172.16.3.101
# 如若看到nginx的欢迎信息则表明安装成功(^_^),反之,没有成功。
为nginx的配置文件设置语法高亮和代码自动缩进
语法高亮的确实很好啊
vim /etc/nginx/nginx.conf
# 此时可以看到文件内容没有语法高亮
# 获取nginx.vim文件
http://www.vim.org/scripts/script.php?script_id=1886
由于官方网站不容易登陆,我已经把它放在附件里面了,有需要的同学可以下载
# 创建目录~/.vim/syntax/,然后把nginx.vim放于其中
mkdir ~/.vim/syntax
mv nginx.vim ~/.vim/systax
vim ~/.vim/filetype.vim
# 向其中添加如下内容
au BufRead,BufNewFile /etc/nginx/*,/usr/local/nginx/conf/* if &ft == '' | setfiletype nginx | endif
# 其中/etc/nginx是nginx配置文件目录
# 保存退出
# 此时再去打开/etc/nginx/nginx.conf文件,可以看到其内容已经语法高亮了
# 还可以对/etc/nginx/nginx.conf文件进行格式自动缩进
vim /etc/nginx/nginx.conf
# 然后输入gg,然后输入=,然后输入G
# 经过上面的操作可以看到,代码的缩进变得很规范
elinks简单用法:
# 安装其安装包
yum -y install elinks
# 该软件是一个文本模式的浏览器
# 可以通过如下方式访问
elinks http://127.0.0.1/
# 该方式访问之后不会直接返回
# 以下方式,可以直接把访问结果输出到屏幕
[iyunv@localhost ~]# elinks -dump http://localhost/
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to [1]nginx.org.
Commercial support is available at [2]nginx.com.
Thank you for using nginx.
References
Visible links
1. http://nginx.org/
2. http://nginx.com/
|
|
|