Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。
nginx的模块需要第三方库的支持,检查是否安装下列库:zlib、zlib-devel(nginx扩展,gzip压缩)、openssl、openssl-devel(nginx扩展)、prce、prce-devel(重写rewrite、支持nginx伪静态);Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个。
1、安装编译环境
[[email protected] ~]# yum -y install gcc gcc-c++ automake autoconf libtool make
2、安装PCRE库
[[email protected] src]# cd /usr/local/src/ [[email protected] src]# wget [[email protected] pcre-8.36]# cd pcre-8.36 [[email protected] pcre-8.36]# ./configure --prefix=/usr/local/pcre [[email protected] pcre-8.36]# make [[email protected] pcre-8.36]# make install
3、安装zlib库
[[email protected] pcre-8.36]# cd /usr/local/src/ [[email protected] src]# wget [[email protected] src]# cd zlib-1.2.8 [[email protected] zlib-1.2.8]# ./configure --prefix=/usr/local/zlib [[email protected] zlib-1.2.8]# make [[email protected] zlib-1.2.8]# make install
4、安装ssl
[[email protected] zlib-1.2.8]# cd /usr/local/src/ [[email protected] src]# wget http://www.openssl.org/source/openssl-1.0.1j.tar.gz [[email protected] src]# tar zxvf openssl-1.0.1j.tar.gz [[email protected] src]# cd openssl-1.0.1j [[email protected] openssl-1.0.1j]# ./config --prefix=/usr/local/openssl [[email protected] openssl-1.0.1j]# make [[email protected] openssl-1.0.1j]# make install
5、安装nginx
[[email protected] openssl-1.0.1j]# cd ..
--with-openssl=/usr/local/src/openssl-1.0.1j --with-zlib=/usr/local/src/zlib-1.2.8 --with-pcre=/usr/local/src/pcre-8.36指向的是源码包解压的路径,而不是安装的路径,否则会报错
检查是否安装成功
[[email protected] sbin]# ./nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: [emerg] getgrnam("wwww") failed nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed [[email protected] sbin]#
编译时候指定组成wwww,重新修改下即可
[[email protected] nginx]# usermod -g wwww www [[email protected] nginx]# id www uid=500(www) gid=501(wwww) groups=501(wwww) [[email protected] nginx]# cd /usr/local/nginx/sbin/ [[email protected] sbin]# ./nginx -t -c /usr/local/nginx/conf/nginx.conf 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 [[email protected] sbin]#
参数"-c"指定了配置文件的路径,如果不加‘-c"参数, nginx,会默认加载其安装目录中conf子目录中的nginx.conf文件。
启动nginx
[[email protected] sbin]# ./nginx [[email protected] sbin]# /usr/local/nginx/sbin/nginx nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) nginx: [emerg] still could not bind() [[email protected] sbin]# lsof -i :80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME clock-app 7093 root 21u IPv4 53701 0t0 TCP 10.15.24.112:52763->63.238.2.211:http (ESTABLISHED) nginx 10145 root 6u IPv4 53797 0t0 TCP *:http (LISTEN) nginx 10146 www 6u IPv4 53797 0t0 TCP *:http (LISTEN) [[email protected] sbin]# kill -9 7093 [[email protected] sbin]# kill -9 10145 [[email protected] sbin]# kill -9 10146 [[email protected] sbin]# /usr/local/nginx/sbin/nginx [[email protected] sbin]# netstat -ano|grep -i ":80" tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN off (0.00/0/0) [[email protected] sbin]# ps -ef|grep "nginx" root 10167 1 0 05:17 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx www 10168 10167 0 05:17 ? 00:00:00 nginx: worker process root 10174 1882 0 05:18 pts/0 00:00:00 grep nginx [[email protected] sbin]#
执行/usr/local/nginx/sbin/nginx时提示端口被占用,因为前面我已经启动过了./nginx导致端口被占用,
重启nginx
[[email protected] sbin]# /usr/local/nginx/sbin/nginx -s reload 或者 [[email protected] sbin]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid `
打开浏览器访问此机器的 IP,如果浏览器出现 Welcome to nginx! 则表示 Nginx 已经安装并运行成功。
设置nginx开机启动
通过脚本来实现
[[email protected] ~]# vim /etc/rc.d/init.d/nginx [[email protected] ~]# cat /etc/rc.d/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: /usr/local/nginx/conf/nginx.conf # pidfile: /usr/local/nginx/logs/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/conf/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‘ -` if [ -z "`grep $user /etc/passwd`" ]; then useradd -M -s /bin/nologin $user fi 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 [[email protected] ~]# chmod 755 /etc/rc.d/init.d/nginx [[email protected] ~]# chkconfig nginx on [[email protected] ~]# service nginx restart Stopping nginx: [ OK ] Starting nginx: [ OK ] [[email protected] ~]#
至此完成!
时间: 2024-10-11 23:52:20