安装nginx之前需要准备一些环境。
1.编译依赖gcc环境,如果没有gcc环境,需要安装gcc:
yum install gcc-c++
2.nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。
yum install -y pcre pcre-devel
3.nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
yum install -y zlib zlib-devel
4.nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。
yum install -y openssl openssl-devel
安装环境Centos6.8 nginx-1.8.0
将nginx-1.8.0.tar.gz拷贝至linux服务器
解压:
tar -zxvf nginx-1.8.0.tar.gz
进入解压的目录
cd nginx-1.8.0
填入参数设置:
./configure \ --prefix=/usr/local/nginx \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/temp/nginx/client \ --http-proxy-temp-path=/var/temp/nginx/proxy \ --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \ --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \ --http-scgi-temp-path=/var/temp/nginx/scgi
注意:上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录使用命令mkdir命令创建目录
mkdir -P /var/temp/nginx
进入解压的目录、就会出现Makefile文件,我们就可以编译安装了
make make install
启动,停止nginx
启动方式 cd /usr/local/nginx/sbin/ ./nginx 快速停止: cd /usr/local/nginx/sbin ./nginx -s stop 此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。 完整停止(建议使用): cd /usr/local/nginx/sbin ./nginx -s quit 此方式停止步骤是待nginx进程处理任务完毕进行停止。
nginx默认的端口号是80,输入ip就可以访问了,要注意的是防火墙要开放80端口,可以参考这篇开放8080端口
https://blog.csdn.net/BushQiang/article/details/81535956
使用脚本来启动nginx,这样就不用每次去目录下启动了, init.d下面放着所有服务
vim /etc/init.d/nginx
#!/bin/bash # nginx Startup script for the Nginx HTTP Server # it is v.0.0.2 version. # chkconfig: - 85 15 # description: Nginx is a high-performance web and proxy server. # It has a lot of features, but it's not for everyone. # processname: nginx # pidfile: /var/run/nginx.pid # config: /usr/local/nginx/conf/nginx.conf nginxd=/usr/local/nginx/sbin/nginx nginx_config=/usr/local/nginx/conf/nginx.conf nginx_pid=/var/run/nginx.pid RETVAL=0 prog="nginx" # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ ${NETWORKING} = "no" ] && exit 0 [ -x $nginxd ] || exit 0 # Start nginx daemons functions. start() { if [ -e $nginx_pid ];then echo "nginx already running...." exit 1 fi echo -n $"Starting $prog: " daemon $nginxd -c ${nginx_config} RETVAL=$? echo [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx return $RETVAL } # Stop nginx daemons functions. stop() { echo -n $"Stopping $prog: " killproc $nginxd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid } # reload nginx service functions. reload() { echo -n $"Reloading $prog: " #kill -HUP `cat ${nginx_pid}` killproc $nginxd -HUP RETVAL=$? echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) stop start ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|reload|status|help}" exit 1 esac exit $RETVAL
把nginx添加到服务列表,并添加可执行权限
1. chkconfig --add nginx 2. chmod 755 nginx