您的位置:首页 > 运维架构 > Nginx

linux安装nginx及nginx常用命令和配置nginx开机自启

2018-11-27 21:09 561 查看

 

操作系统: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]

下面是一整条命令,\表示命令没有写完,

  1. ./configure \
  2. --prefix=/usr/local/nginx \
  3. --pid-path=/var/run/nginx/nginx.pid \
  4. --lock-path=/var/lock/nginx.lock \
  5. --error-log-path=/var/log/nginx/error.log \
  6. --http-log-path=/var/log/nginx/access.log \
  7. --with-http_gzip_static_module \
  8. --http-client-body-temp-path=/var/temp/nginx/client \
  9. --http-proxy-temp-path=/var/temp/nginx/proxy \
  10. --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
  11. --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
  12. --http-scgi-temp-path=/var/temp/nginx/scgi
[/code]

 注意:启动nginx之前,上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录

[root@localhost nginx-1.8.0]# mkdir /var/temp/nginx/client -p
[/code]

2.4编译安装

  1. [root@localhost nginx-1.8.0]# make
  2. [root@localhost nginx-1.8.0]# make install
[/code]

至此安装完成

3.启动nginx

  1. [root@localhost ~]# cd /usr/local/nginx/sbin/
  2. [root@localhost sbin]# ./nginx
[/code]

如果出现

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端口,增加到防火墙配置中

  1. [root@localhost sbin]# /sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT
  2. [root@localhost sbin]# /etc/rc.d/init.d/iptables save
  3. [root@localhost sbin]# /etc/rc.d/init.d/iptables restart
[/code]

如果是阿里云服务器还需要添加安全组规则 

直接输入ngnix服务器的ip地址,即可直接访问

5.停止nginx

5.1快速停止

  1. [root@localhost ~]# cd /usr/local/nginx/sbin
  2. [root@localhost sbin]# ./nginx -s stop
[/code]

此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

5.2完整停止(建议使用)

  1. [root@localhost ~]# cd /usr/local/nginx/sbin
  2. [root@localhost sbin]# ./nginx -s quit
[/code]

此方式停止步骤是待nginx进程处理任务完毕进行停止。

6.重启nginx

6.1先停止再启动(建议使用)

对nginx进行重启相当于先停止nginx再启动nginx,即先执行停止命令再执行启动命令。

如下:

  1. [root@localhost ~]# cd /usr/local/nginx/sbin
  2. [root@localhost sbin]# ./nginx -s quit
  3. [root@localhost sbin]# ./nginx
[/code]

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退出

  1. #!/bin/bash
  2. # nginx Startup script for the Nginx HTTP Server
  3. # it is v.0.0.2 version.
  4. # chkconfig: - 85 15
  5. # description: Nginx is a high-performance web and proxy server.
  6. # It has a lot of features, but it's not for everyone.
  7. # processname: nginx
  8. # pidfile: /var/run/nginx.pid
  9. # config: /usr/local/nginx/conf/nginx.conf
  10. nginxd=/usr/local/nginx/sbin/nginx
  11. nginx_config=/usr/local/nginx/conf/nginx.conf
  12. nginx_pid=/var/run/nginx.pid
  13. RETVAL=0
  14. prog="nginx"
  15. # Source function library.
  16. . /etc/rc.d/init.d/functions
  17. # Source networking configuration.
  18. . /etc/sysconfig/network
  19. # Check that networking is up.
  20. [ ${NETWORKING} = "no" ] && exit 0
  21. [ -x $nginxd ] || exit 0
  22. # Start nginx daemons functions.
  23. start() {
  24. if [ -e $nginx_pid ];then
  25. echo "nginx already running...."
  26. exit 1
  27. fi
  28. echo -n $"Starting $prog: "
  29. daemon $nginxd -c ${nginx_config}
  30. RETVAL=$?
  31. echo
  32. [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
  33. return $RETVAL
  34. }
  35. # Stop nginx daemons functions.
  36. stop() {
  37. echo -n $"Stopping $prog: "
  38. killproc $nginxd
  39. RETVAL=$?
  40. echo
  41. [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
  42. }
  43. # reload nginx service functions.
  44. reload() {
  45. echo -n $"Reloading $prog: "
  46. #kill -HUP `cat ${nginx_pid}`
  47. killproc $nginxd -HUP
  48. RETVAL=$?
  49. echo
  50. }
  51. # See how we were called.
  52. case "$1" in
  53. start)
  54. start
  55. ;;
  56. stop)
  57. stop
  58. ;;
  59. reload)
  60. reload
  61. ;;
  62. restart)
  63. stop
  64. start
  65. ;;
  66. status)
  67. status $prog
  68. RETVAL=$?
  69. ;;
  70. *)
  71. echo $"Usage: $prog {start|stop|restart|reload|status|help}"
  72. exit 1
  73. esac
  74. exit $RETVAL
[/code]

7.2设置文件访问权限

a+x ==> all user can execute  所有用户可执行

[root@localhost sbin]# chmod a+x /etc/init.d/nginx   
[/code]

这样在控制台就很容易的操作nginx了:查看Nginx当前状态、启动Nginx、停止Nginx、重启Nginx…

  1. #查看nginx运行状态
  2. /etc/init.d/nginx status
  3. #启动nginx
  4. /etc/init.d/nginx start
  5. #停止nginx
  6. /etc/init.d/nginx stop
  7. #重启nginx
  8. /etc/init.d/nginx restart
[/code]

如果修改了nginx的配置文件nginx.conf,也可以使用上面的命令重新加载新的配置文件并运行,可以将此命令加入到rc.local文件中,这样开机的时候nginx就默认启动了

7.3设置开机自启

[root@localhost sbin]# vi /etc/rc.local
[/code]

加入一行  /etc/init.d/nginx start    保存并退出,下次重启会生效。

 

 

 

 

**加粗样式**
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: