第一次写博客,肯能会有很多不足,主要记录自己的一点点成长。
我用的是阿里云 所以基本上的编译依赖都自带安装好了。如果没有安装编译所需的依赖,那么就自己手动安装一下。
我们约定所有源码包放在 /usr/local/src/ 目录下,编译安装在 /usr/local/ 目录下。
#编译前准备,更新yum源包
yum update
#创建用户组www,创建用户www 所属组 www 设置不能登录
groupadd www && useradd -g www www -s /bin/false
#安装nginx编译所需依赖包
#http_image_filter_module模块需要gd库
#这里是安装二进制包,也可以下载源码包编译安装
yum -y install zlib zlib-devel openssl* pcre pcre-devel gd-devel
#进入下载源码包目录
#下载源码包
#解压源码包
cd /usr/local/src wget http://nginx.org/download/nginx-1.12.2.tar.gz tar -zxvf nginx-1.12.2.tar.gz
#创建安装目录
mkdir /usr/local/nginx
#进入解压好的源码包准备编译安装
cd nginx-1.12.2
#编译nginx
#因为上面的依赖包是二进制安装 所以--with-pcre --with-zlib --with-openssl 不需要加到参数里,如果非二进制包则要指定路径 --with-pcre=/usr/local/pcre/
./configure --prefix=/usr/local/nginx/ \ --user=www \ --group=www \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-http_sub_module \ --with-http_realip_module \ --with-http_image_filter_module
需要什么模块功能可以自己选择 ./configure --help 命令可以查看所有模块
注意一下配置时有没有报错,报错的话缺什么补什么
#如果没有报错 则进行编译安装
make && make install
#安装成功后,进入安装目录
#备份原配置
#修改nginx.conf
cd /usr/local/nginx/conf/
cp nginx.conf nginx.conf.bak
vim nginx.conf
修改前: 修改后:
注意每行结束的 “;”分号,不然开启的时候会报错。
#配置Nginx控制脚本
vim /etc/rc.d/init.d/nginx
写入以下脚本:
注意安装路径不一样的话,需要自己更改脚本
ps:脚本代码是从别人那里拿回来的

#!/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
#设置脚本权限
#设置脚本开机启动
#启动nginx
chmod 775 /etc/rc.d/init.d/nginx chkconfig nginx on /etc/rc.d/init.d/nginx start
以后可以直接使用命令 service nginx (start|stop|restart)
#把nginx加入系统变量
echo 'export PATH=$PATH:/usr/local/nginx/sbin'>>/etc/profile && source /etc/profile
最后,打开浏览器,输入服务器IP地址,出现下图说明安装成功
谢谢!