centos7源码编译安装nginx1.17

源码下载地址:http://nginx.org/download/

集成安装

yum -y install gcc gcc-c++ autoconf automake
yum -y install zlib zlib-devel openssl openssl-devel pcre-devel

先把1.17.10版本的安装包上传到服务器

#1.解压

tar -zxvf nginx-1.17.10.tar.gz

#2.配置

使用默认配置

./configure

#3.然后进行编译

make

 #4.安装

make install

安装完成后,会上一级出现这些目录

#5.创建软链接

ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
 cd /usr/local/nginx/sbin

#6.开机自启动

创建该文件

vim /etc/init.d/nginx

文件的内容: 

 
  1. #!/bin/sh

  2. #

  3. # nginx - this script starts and stops the nginx daemon

  4. #

  5. # chkconfig: - 85 15

  6. # description: NGINX is an HTTP(S) server, HTTP(S) reverse \

  7. # proxy and IMAP/POP3 proxy server

  8. # processname: nginx

  9. # config: /usr/local/nginx/conf/nginx.conf

  10. # config: /etc/sysconfig/nginx

  11. # pidfile: /usr/local/nginx/logs/nginx.pid

  12. # Source function library.

  13. . /etc/rc.d/init.d/functions

  14. # Source networking configuration.

  15. . /etc/sysconfig/network

  16. # Check that networking is up.

  17. [ "$NETWORKING" = "no" ] && exit 0

  18. nginx="/usr/local/nginx/sbin/nginx"

  19. prog=$(basename $nginx)

  20. NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

  21. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

  22. lockfile=/var/lock/subsys/nginx

  23. make_dirs() {

  24. # make required directories

  25. user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`

  26. if [ -z "`grep $user /etc/passwd`" ]; then

  27. useradd -M -s /bin/nologin $user

  28. fi

  29. options=`$nginx -V 2>&1 | grep 'configure arguments:'`

  30. for opt in $options; do

  31. if [ `echo $opt | grep '.*-temp-path'` ]; then

  32. value=`echo $opt | cut -d "=" -f 2`

  33. if [ ! -d "$value" ]; then

  34. # echo "creating" $value

  35. mkdir -p $value && chown -R $user $value

  36. fi

  37. fi

  38. done

  39. }

  40. start() {

  41. [ -x $nginx ] || exit 5

  42. [ -f $NGINX_CONF_FILE ] || exit 6

  43. make_dirs

  44. echo -n $"Starting $prog: "

  45. daemon $nginx -c $NGINX_CONF_FILE

  46. retval=$?

  47. echo

  48. [ $retval -eq 0 ] && touch $lockfile

  49. return $retval

  50. }

  51. stop() {

  52. echo -n $"Stopping $prog: "

  53. killproc $prog -QUIT

  54. retval=$?

  55. echo

  56. [ $retval -eq 0 ] && rm -f $lockfile

  57. return $retval

  58. }

  59. restart() {

  60. configtest || return $?

  61. stop

  62. sleep 1

  63. start

  64. }

  65. reload() {

  66. configtest || return $?

  67. echo -n $"Reloading $prog: "

  68. killproc $nginx -HUP

  69. RETVAL=$?

  70. echo

  71. }

  72. force_reload() {

  73. restart

  74. }

  75. configtest() {

  76. $nginx -t -c $NGINX_CONF_FILE

  77. }

  78. rh_status() {

  79. status $prog

  80. }

  81. rh_status_q() {

  82. rh_status >/dev/null 2>&1

  83. }

  84. case "$1" in

  85. start)

  86. rh_status_q && exit 0

  87. $1

  88. ;;

  89. stop)

  90. rh_status_q || exit 0

  91. $1

  92. ;;

  93. restart|configtest)

  94. $1

  95. ;;

  96. reload)

  97. rh_status_q || exit 7

  98. $1

  99. ;;

  100. force-reload)

  101. force_reload

  102. ;;

  103. status)

  104. rh_status

  105. ;;

  106. condrestart|try-restart)

  107. rh_status_q || exit 0

  108. ;;

  109. *)

  110. echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"

  111. exit 2

  112. esac

#7.赋予脚本可执行权限

chmod a+x /etc/init.d/nginx

#8.将nginx服务加入chkconfig管理列表

chkconfig --add /etc/init.d/nginx

chkconfig nginx on

#9.启动

systemctl start nginx

#10.测试

curl localhost:80

#11.常用命令

 
  1. # 启动

  2. systemctl start nginx

  3. # 查看状态

  4. systemctl status nginx

  5. # 停止

  6. systemctl stop nginx

  7.  
  8. # 重载配置

  9. nginx -s reload

  10. # 测试配置是否正确

  11. nginx -t

猜你喜欢

转载自blog.csdn.net/weixin_37989267/article/details/111873535