Centos7搭建nginx开发环境

た 入场券 2022-06-06 05:50 293阅读 0赞

Centos7搭建nginx开发环境

1 nginx安装环境

nginx是C语言开发,建议在linux上运行,本教程使用Centos7作为安装环境。

(1) gcc

安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc:yum install gcc-c++

(2) PCRE

PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括perl兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。

yum install -y pcre pcre-devel

注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。

(3) zlib

zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。

yum install -y zlib zlib-devel

(4) openssl

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。

nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。

yum install -y openssl openssl-devel

2 编译安装

将nginx-1.8.0.tar.gz拷贝至linux服务器。

解压:

tar -zxvf nginx-1.8.0.tar.gz

cd nginx-1.8.0

1、 configure

./configure —help查询详细参数(参考本教程附录部分:nginx编译参数)

拷贝以下命令执行:作用是参数设置

./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/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

若嫌弃命令行太长,那就不执行命令行执行该命令

./configure —prefix=/usr/local/nginx

2、 编译安装

make

make install

安装成功查看安装目录 :安装目录对应参数设置里面

  1. --prefix=/usr/local/nginx 如下图路径所示

20171031210652396

3 启动nginx

cd /usr/local/nginx/sbin/

./nginx

nginx进程查看如图所示;master是主进程,worker是工作进程

20171031211011130

注意:执行./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

方式1,快速停止:

cd /usr/local/nginx/sbin

./nginx -s stop

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

方式2,完整停止(建议使用):

cd /usr/local/nginx/sbin

./nginx -s quit

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

5 重启nginx

方式1,先停止再启动(建议使用):

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

如下:

./nginx -s quit

./nginx

方式2,重新加载配置文件:

当nginx的配置文件nginx.conf修改后,要想让配置生效需要重启nginx,使用-s reload不用先停止nginx再启动nginx即可将配置信息在nginx中生效,如下:

./nginx -s reload

6 测试

nginx安装成功,启动nginx,即可访问虚拟机上的nginx:如图所示:

20171031213427741

7 开机自动启动nginx

(1)编写shell脚本

vi /etc/init.d/nginx (输入下面的代码)

  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

:wq 保存并退出

(2)设置文件的访问权限

chmod a+x /etc/init.d/nginx (a+x ==> all user can execute 所有用户可执行)

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

20171031213903473

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

(3)开机启动,加入rc.local文件中

  1. vi /etc/rc.local

加入一行 /etc/init.d/nginx start

保存并退出,下次重启系统会生效。

8 开启http服务(自定义路径,和ftp服务配合使用)

执行命令vi /usr/local/nginx/conf/nginx.conf

修改45行处的路径为自定义路径之后,然后重启nginx服务,此时的路径/home/ftpuser就是http根路径

20171102005055419

而ftp的路径是/home/ftpuser/images,关于ftp的搭建可以参照博客 http://blog.csdn.net/u013144287/article/details/78408512

20171102005254517

9 访问方式

访问路径是http://192.168.142.133/images/1.jpg,如图所示:

20171102005453270

至此http访问方式就成功了

发表评论

表情:
评论列表 (有 0 条评论,293人围观)

还没有评论,来说两句吧...

相关阅读