CentOS7.3编译安装Nginx1.10.1

前端之家收集整理的这篇文章主要介绍了CentOS7.3编译安装Nginx1.10.1前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

编译环境安装

  1. yum -y gcc
  2. yum -y gcc++
  3. yum -y gcc-c++
  4. yum -y install wget

下载安装文件并解压

  1. > 创建文件夹并进入
  2.  
  3. mkdir soft && cd soft
  4. > 下载依赖文件pcre,openssl,zlib
  5.  
  6. wget -c http://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.39.tar.gz
  7. wget -c https://www.openssl.org/source/openssl-1.0.2l.tar.gz
  8. wget -c http://zlib.net/zlib-1.2.11.tar.gz
  9.  
  10. > 下载Nginx
  11.  
  12. wget -c http://Nginx.org/download/Nginx-1.10.1.tar.gz
  13.  
  14. > 解压文件
  15.  
  16. tar -zxvf pcre-8.39.tar.gz
  17. tar -zxvf openssl-1.0.2l.tar.gz
  18. tar -zxvf zlib-1.2.11.tar.gz
  19. tar -zxvf Nginx-1.10.1.tar.gz

创建用户和目录

  1. > 新建系统账号Nginx
  2.  
  3. useradd -r Nginx -s /sbin/nologin -M
  4.  
  5. > 新建Nginx需要的目录
  6.  
  7. mkdir -p /var/tmp/Nginx/{client_body,proxy,fastcgi,uwsgi,scgi}
  8.  
  9. > 递归改变目录所有者
  10.  
  11. chown -R Nginx /var/tmp/Nginx

编译Nginx-1.10.1

  1. cd ~/soft/Nginx-1.10.1
  2.  
  3. ./configure \
  4. --prefix=/usr/local/Nginx \
  5. --sbin-path=/usr/sbin/Nginx \
  6. --conf-path=/etc/Nginx/Nginx.conf \
  7. --error-log-path=/var/log/Nginx/error.log \
  8. --http-log-path=/var/log/Nginx/access.log \
  9. --pid-path=/var/run/Nginx.pid \
  10. --lock-path=/var/lock/Nginx.lock \
  11. --user=Nginx \
  12. --group=Nginx \
  13. --with-http_ssl_module \
  14. --with-http_realip_module \
  15. --with-http_stub_status_module \
  16. --with-http_gzip_static_module \
  17. --with-pcre=../pcre-8.39 \
  18. --with-zlib=../zlib-1.2.11 \
  19. --with-openssl=../openssl-1.0.2l \
  20. --with-debug \
  21. --http-client-body-temp-path=/var/tmp/Nginx/client_body \
  22. --http-proxy-temp-path=/var/tmp/Nginx/proxy \
  23. --http-fastcgi-temp-path=/var/tmp/Nginx/fastcgi \
  24. --http-uwsgi-temp-path=/var/tmp/Nginx/uwsgi \
  25. --http-scgi-temp-path=/var/tmp/Nginx/scgi \
  26. --with-stream
  27.  
  28. make && make install

编辑服务脚本文件

  1. > 创建文件并打开服务脚本文件
  2.  
  3. vim /etc/init.d/Nginx
  4.  
  5. > 编写脚本文件
  6.  
  7. #! /bin/bash
  8. #
  9. # Nginx - this script starts and stops the Nginx daemon
  10. #
  11. # chkconfig: - 85 15
  12. # description: Nginx is an HTTP(S) server,HTTP(S) reverse \
  13. # proxy and IMAP/POP3 proxy server
  14. #
  15. # processname: Nginx
  16. # config: /etc/Nginx/Nginx.conf
  17. # pidfile: /var/run/Nginx.pid
  18.  
  19. # Source function library.
  20. . /etc/rc.d/init.d/functions
  21.  
  22. # Source networking configuration.
  23. . /etc/sysconfig/network
  24.  
  25. # Check that networking is up.
  26. [ "$NETWORKING" = "no" ] && exit 0
  27.  
  28. Nginx="/usr/sbin/Nginx"
  29. prog=$(basename $Nginx)
  30.  
  31. Nginx_CONF_FILE="/etc/Nginx/Nginx.conf"
  32.  
  33. [ -f /etc/sysconfig/Nginx ] && . /etc/sysconfig/Nginx
  34.  
  35. lockfile=/var/lock/Nginx.lock
  36.  
  37. start() {
  38. [ -x $Nginx ] || exit 5
  39. [ -f $Nginx_CONF_FILE ] || exit 6
  40. echo -n "Starting $prog: "
  41. daemon $Nginx -c $Nginx_CONF_FILE
  42. retval=$?
  43. echo
  44. [ $retval -eq 0 ] && touch $lockfile
  45. return $retval
  46. }
  47.  
  48. stop() {
  49. echo -n "Stopping $prog: "
  50. killproc $prog -QUIT
  51. retval=$?
  52. echo
  53. [ $retval -eq 0 ] && rm -f $lockfile
  54. return $retval
  55. }
  56.  
  57. restart() {
  58. configtest || return $?
  59. stop
  60. sleep 1
  61. start
  62. }
  63.  
  64. reload() {
  65. configtest || return $?
  66. echo -n "Reloading $prog: "
  67. killproc $Nginx -HUP
  68. RETVAL=$?
  69. echo
  70. }
  71.  
  72. force_reload() {
  73. restart
  74. }
  75.  
  76. configtest() {
  77. $Nginx -t -c $Nginx_CONF_FILE
  78. }
  79.  
  80. rh_status() {
  81. status $prog
  82. }
  83.  
  84. rh_status_q() {
  85. rh_status >/dev/null 2>&1
  86. }
  87.  
  88. case "$1" in
  89. start)
  90. rh_status_q && exit 0
  91. $1
  92. ;;
  93. stop)
  94. rh_status_q || exit 0
  95. $1
  96. ;;
  97. restart|configtest)
  98. $1
  99. ;;
  100. reload)
  101. rh_status_q || exit 7
  102. $1
  103. ;;
  104. force-reload)
  105. force_reload
  106. ;;
  107. status)
  108. rh_status
  109. ;;
  110. condrestart|try-restart)
  111. rh_status_q || exit 0
  112. ;;
  113. *)
  114. echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
  115. exit 2
  116. ;;
  117. esac

配置Nginx启动脚本

  1. > 改变文件权限
  2.  
  3. chmod +x /etc/init.d/Nginx
  4.  
  5. > 添加到系统服务
  6.  
  7. chkconfig --add Nginx
  8.  
  9. > 设置系统为开机启动
  10.  
  11. chkconfig Nginx on

启动Nginx服务器

  1. systemctl start Nginx

启动服务状态

  1. [root@localhost soft]# systemctl status Nginx.service
  2. Nginx.service - SYSV: Nginx is an HTTP(S) server,HTTP(S) reverse proxy and IMAP/POP3 proxy server
  3. Loaded: loaded (/etc/rc.d/init.d/Nginx; bad; vendor preset: disabled)
  4. Active: active (running) since Tue 2017-06-13 11:47:44 EDT; 22s ago
  5. Docs: man:systemd-sysv-generator(8)
  6. Process: 40801 ExecStart=/etc/rc.d/init.d/Nginx start (code=exited,status=0/SUCCESS)
  7. Main PID: 40808 (Nginx)
  8. Memory: 1.0M
  9. CGroup: /system.slice/Nginx.service
  10. ├─40808 Nginx: master process /usr/sbin/Nginx -c /etc/Nginx/Nginx.conf
  11. └─40809 Nginx: worker process
  12.  
  13. Jun 13 11:47:43 localhost.localdomain systemd[1]: Starting SYSV: Nginx is an HTTP(S) server,HTTP(S) reverse proxy and IMAP/POP3 proxy server...
  14. Jun 13 11:47:44 localhost.localdomain Nginx[40801]: Starting Nginx: [ OK ]
  15. Jun 13 11:47:44 localhost.localdomain systemd[1]: Started SYSV: Nginx is an HTTP(S) server,HTTP(S) reverse proxy and IMAP/POP3 proxy server.
  16. [root@localhost soft]#

修改Nginx配置文件

  1. # 运行用户
  2. #user nobody;
  3. # 启动进程,通常设置成和cpu的数据相等
  4. worker_processes 1;
  5.  
  6. # 全局错误日志及PID文件
  7. #error_log logs/error.log;
  8. #error_log logs/error.log notice;
  9. #error_log logs/error.log info;
  10.  
  11. #pid logs/Nginx.pid;
  12.  
  13. # 工作模式及连接数上限
  14. events {
  15. use epoll;
  16.  
  17. worker_connections 1024;
  18. }
  19.  
  20.  
  21. http {
  22. include mime.types;
  23. default_type application/octet-stream;
  24.  
  25. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  26. '$status $body_bytes_sent "$http_referer" '
  27. '"$http_user_agent" "$http_x_forwarded_for"';
  28.  
  29. access_log logs/access.log main;
  30.  
  31. sendfile on;
  32. #tcp_nopush on;
  33.  
  34. #keepalive_timeout 0;
  35. keepalive_timeout 65;
  36. tcp_nodelay on;
  37.  
  38. gzip on;
  39. gzip_disable "MSIE [1-6]";
  40.  
  41. client_header_buffer_size 128k;
  42. large_client_header_buffers 4 128k;
  43.  
  44. server {
  45. listen 80;
  46. server_name www.Nginx.dev;
  47.  
  48.  
  49. #charset koi8-r;
  50.  
  51. access_log logs/Nginx.dev.access.log main;
  52.  
  53. location / {
  54. root /data/www/html;
  55. index index.PHP index.html index.htm;
  56. }
  57.  
  58. #error_page 404 /404.html;
  59.  
  60. # redirect server error pages to the static page /50x.html
  61. #
  62. error_page 500 502 503 504 /50x.html;
  63. location = /50x.html {
  64. root /data/www/html;
  65. }
  66.  
  67. location ~ ^/(images|javascript|js|css|flash|media|static)/ {
  68. expires 30d;
  69. }
  70.  
  71. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  72. #
  73. #location ~ \.PHP$ {
  74. # proxy_pass http://127.0.0.1;
  75. #}
  76.  
  77. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  78. #
  79. location ~ \.PHP$ {
  80. # root html;
  81. fastcgi_pass 127.0.0.1:9000;
  82. fastcgi_index index.PHP;
  83. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  84. include fastcgi_params;
  85. }
  86.  
  87. # deny access to .htaccess files,if Apache's document root
  88. # concurs with Nginx's one
  89. #
  90. location ~ /\.ht {
  91. deny all;
  92. }
  93. }
  94.  
  95.  
  96. # another virtual host using mix of IP-,name-,and port-based configuration
  97. #
  98. #server {
  99. # listen 8000;
  100. # listen somename:8080;
  101. # server_name somename alias another.alias;
  102.  
  103. # location / {
  104. # root html;
  105. # index index.html index.htm;
  106. # }
  107. #}
  108.  
  109.  
  110. # HTTPS server
  111. #
  112. #server {
  113. # listen 443 ssl;
  114. # server_name localhost;
  115.  
  116. # ssl_certificate cert.pem;
  117. # ssl_certificate_key cert.key;
  118.  
  119. # ssl_session_cache shared:SSL:1m;
  120. # ssl_session_timeout 5m;
  121.  
  122. # ssl_ciphers HIGH:!aNULL:!MD5;
  123. # ssl_prefer_server_ciphers on;
  124.  
  125. # location / {
  126. # root html;
  127. # index index.html index.htm;
  128. # }
  129. #}
  130.  
  131. }

CentOS7添加开放80TCP端口

  1. > 加入开放端口到配置文件
  2.  
  3. firewall-cmd --zone=public --add-port=80/tcp --permanent
  4.  
  5. --zone=public 添加时区
  6.  
  7. --add-port=80/tcp 添加端口
  8.  
  9. --permanent 永久生效
  10.  
  11. > 加载防火墙新配置文件( root 身份输入以下命令,重新加载防火墙,并不中断用户连接,即不丢失状态信息. )
  12.  
  13. firewall-cmd --reload

希望本文对你的工作和学习有所帮助

如果觉得还不错怎么感谢我呢? 妈呀! 点赞啊!

Good Luck! from warnerwu at 2017.06.24 PM

猜你在找的CentOS相关文章