linux安装nginx及nginx常用命令和配置nginx开机自启
操作系统:centos6.4
工具:xshell、xftp
1.配置安装环境
gcc
安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装
[root@localhost ~]# yum install gcc-c++[/code]
PCRE
PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析 正则表达式,所以需要在linux上安装pcre库。
[root@localhost ~]# yum install -y pcre pcre-devel[/code]
注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。
zlib
zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。
[root@localhost ~]# yum install -y zlib zlib-devel[/code]
openssl
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。
nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。
[root@localhost ~]# yum install -y openssl openssl-devel[/code]
2.编译安装
2.1上传nginx源码包
使用xftp上传源码包
2.2解压
[root@localhost ~]# tar -zxvf nginx-1.8.0.tar.gz[/code]
2.3configure
先进入nginx目录
[root@localhost ~]# cd nginx-1.8.0[/code]
下面是一整条命令,\表示命令没有写完,
- ./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
注意:启动nginx之前,上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录
[root@localhost nginx-1.8.0]# mkdir /var/temp/nginx/client -p[/code]
2.4编译安装
- [root@localhost nginx-1.8.0]# make
- [root@localhost nginx-1.8.0]# make install
至此安装完成
3.启动nginx
- [root@localhost ~]# cd /usr/local/nginx/sbin/
- [root@localhost sbin]# ./nginx
如果出现
nginx: [emerg] mkdir() "/var/temp/nginx/client" failed (2: No such file or directory)
需要手动创建/var/temp/nginx/client目录
[root@localhost nginx-1.8.0]# mkdir /var/temp/nginx/client -p[/code]
查看nginx进程
[root@localhost sbin]# ps aux|grep nginx[/code]
第一个是nginx主进程,第二个是是nginx工作进程。
第三个是我们敲的命令
注意:执行./nginx启动nginx,这里可以-c指定加载的nginx配置文件,如下:
./nginx -c /usr/local/nginx/conf/nginx.conf
如果不指定-c,nginx在启动时默认加载conf/nginx.conf文件,此文件的地址也可以在编译安装nginx时指定./configure的参数(--conf-path= 指向配置文件(nginx.conf))
4.测试访问
注意:nginx使用80端口号,为了能正常使用nginx,我们需要打开80端口。
防火墙开放80端口,增加到防火墙配置中
- [root@localhost sbin]# /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
- [root@localhost sbin]# /etc/rc.d/init.d/iptables save
- [root@localhost sbin]# /etc/rc.d/init.d/iptables restart
如果是阿里云服务器还需要添加安全组规则
直接输入ngnix服务器的ip地址,即可直接访问
5.停止nginx
5.1快速停止
- [root@localhost ~]# cd /usr/local/nginx/sbin
- [root@localhost sbin]# ./nginx -s stop
此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
5.2完整停止(建议使用)
- [root@localhost ~]# cd /usr/local/nginx/sbin
- [root@localhost sbin]# ./nginx -s quit
此方式停止步骤是待nginx进程处理任务完毕进行停止。
6.重启nginx
6.1先停止再启动(建议使用)
对nginx进行重启相当于先停止nginx再启动nginx,即先执行停止命令再执行启动命令。
如下:
- [root@localhost ~]# cd /usr/local/nginx/sbin
- [root@localhost sbin]# ./nginx -s quit
- [root@localhost sbin]# ./nginx
6.2重新加载配置文件
当nginx的配置文件nginx.conf修改后,要想让配置生效需要重启nginx,使用-s reload不用先停止nginx再启动nginx即可将配置信息在nginx中生效,如下:
[root@localhost sbin]# ./nginx -s reload[/code]
7开机自启nginx
7.1编写shell脚本
[root@localhost sbin]# vi /etc/init.d/nginx[/code]
shell代码
i进入编辑,:wq退出
- #!/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
7.2设置文件访问权限
a+x ==> all user can execute 所有用户可执行
[root@localhost sbin]# chmod a+x /etc/init.d/nginx[/code]
这样在控制台就很容易的操作nginx了:查看Nginx当前状态、启动Nginx、停止Nginx、重启Nginx…
- #查看nginx运行状态
- /etc/init.d/nginx status
- #启动nginx
- /etc/init.d/nginx start
- #停止nginx
- /etc/init.d/nginx stop
- #重启nginx
- /etc/init.d/nginx restart
如果修改了nginx的配置文件nginx.conf,也可以使用上面的命令重新加载新的配置文件并运行,可以将此命令加入到rc.local文件中,这样开机的时候nginx就默认启动了
7.3设置开机自启
[root@localhost sbin]# vi /etc/rc.local[/code]
加入一行 /etc/init.d/nginx start 保存并退出,下次重启会生效。
**加粗样式**
- linux,Centos 6.3 yum安装Nginx+php+Mysql,配置防火墙,开启,关闭端口命令
- 学习笔记,Linux常用命令--MySQL下载安装配置
- Linux操作系统基础学习笔记(1)——Linux的介绍、安装、配置,常用的远程工具及常见命令
- linux下redis安装配置及redis常用命令
- linux下redis安装配置及redis常用命令
- Nginx_安装配置及常用命令
- Linux(Red Hat Enterprise)下安装nginx与sticky模块,并配置开机自启动图文教程
- Centos7.x下Nginx安装及SSL配置与常用命令详解
- nginx 安装配置和常用命令
- Linux下安装、配置、卸载IBM DB2及常用命令
- linux下redis安装配置及redis常用命令(实战详细版)
- linux(centos6.6) 下安装,配置nginx, 及开机自启动
- Linux之CentOS--Nginx的安装、测试详解以及关于Nginx的常用命令介绍
- linux下redis安装配置及redis常用命令
- linux下redis安装配置及redis常用命令
- Nginx安装及配置以及常用命令
- Linux上安装Nginx及常用命令
- Linux下载安装Nginx并配置,以及启动/停止/重启命令
- linux下redis安装配置及redis常用命令
- Linux(Ubuntu) Mysql的安装配置例子以及常用命令