侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

centos6.5服务器安装Nginx设置服务和开机自启的方法

linux  /  管理员 发布于 7年前   264

本文介绍了centos6.5服务器安装Nginx设置服务和开机自启的方法,分享给大家,也给自己留个笔记

1、安装Nginx及其依赖

首先是老套路,使用ssh链接服务器,还记得以前的代码吗?

ssh -t 用户名@服务器IP或者域名 -p 22<!--用户名一般是root,方便操作,我的登录代码如下-->ssh -t [email protected] -p 22

在终端中输入上面命令按下回车,要求我们输入密码,这个密码是不可见的,所以一定要输入正确。

链接到服务器后,我们切换到常用的安装路径,当然我服务器上面的安装路径是/usr/src,接着开始在终端操作:

<!--切换到安装目录下-->cd /usr/src<!--创建Nginx文件夹用来存放Nginx相关的资源和依赖-->mkdir Nginx<!--下载资源和依赖-->yum -y install zlib zlib-devel openssl openssl--devel pcre pcre-devel <!--上面的命令一般来说会是不需要安装什么,不过这都不重要,我们接着会重新安装指定的版本--><!--下载pcre-->wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz<!--解压-->tar -zxvf pcre-8.40.tar.gz<!--切换到pcre目录-->cd pcre-8.40<!--设置-->./configure<!--编译-->make<!--安装-->make install<!--切换到Nginx主目录-->cd ..<!--下载及安装zlib-->wget http://zlib.net/zlib-1.2.11.tar.gz<!--解压-->tar -zxvf zlib-1.2.11.tar.gz<!--切换到zlib目录-->cd zlib-1.2.11<!--设置、编译、安装-->./configuremakemake install<!--切换到Nginx主目录-->cd ..<!--下载及准备ssl-->wget http://www.openssl.org/source/openssl-fips-2.0.14.tar.gz<!--解压-->tar -zxvf openssl-fips-2.0.14.tar.gz<!--yum安装ssl-->yum -y install openssl openssl-devel<!--下载及安装nginx-->wget http://nginx.org/download/nginx-1.4.2.tar.gztar -zxvf nginx-1.4.2.tar.gzcd nginx-1.4.2<!--设置Nginx安装目录/opt/nginx,且添加ssl支持-->./configure --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcremakemake install

到这里来讲,我们的nginx安装完成了,但是我们还需要做更多的事情,那就是配置服务器,添加ssl访问,设置服务和开机启动

2、配置服务器

互联网上关于服务器设置的很多,但是准确阐述的却不是那么多,而我刚好是在看了他们的东西后就呵呵了。正确的配置方法如下:

<!--切换到nginx设置目录-->cd /opt/nginx/conf<!--vim编辑nginx配置文件-->vi nginx.conf

我的nginx.conf如下:

#user nobody;worker_processes 1;#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid    logs/nginx.pid;events {  worker_connections 1024;}http {  include    mime.types;  default_type application/octet-stream;  #log_format main '$remote_addr - $remote_user [$time_local] "$request" '  #         '$status $body_bytes_sent "$http_referer" '  #         '"$http_user_agent" "$http_x_forwarded_for"';  #access_log logs/access.log main;  sendfile    on;  #tcp_nopush   on;  #keepalive_timeout 0;  keepalive_timeout 65;  #gzip on;# 注意这里是设置本机的相关的东西,建议不要更改  server {    listen    80;    server_name localhost;    #charset koi8-r;    #access_log logs/host.access.log main;    location / {    root  html;        index index.html index.htm;    # proxy_pass http://localhost;     # proxy_set_header  Host  $host;    # proxy_set_header  X-Real-IP  $remote_addr;    # proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;    }    #error_page 404       /404.html;    # redirect server error pages to the static page /50x.html    #    error_page  500 502 503 504 /50x.html;    location = /50x.html {      root  html;    }    # proxy the PHP scripts to Apache listening on 127.0.0.1:80    #    #location ~ \.php$ {    #  proxy_pass  http://127.0.0.1;    #}    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000    #    #location ~ \.php$ {    #  root      html;    #  fastcgi_pass  127.0.0.1:9000;    #  fastcgi_index index.php;    #  fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;    #  include    fastcgi_params;    #}    # deny access to .htaccess files, if Apache's document root    # concurs with nginx's one    #    #location ~ /\.ht {    #  deny all;    #}  }  # another virtual host using mix of IP-, name-, and port-based configuration  #  #server {  #  listen    8000;  #  listen    somename:8080;  #  server_name somename alias another.alias;  #  location / {  #    root  html;  #    index index.html index.htm;  #  }  #}# 这里是设置本机的https访问的,这里必须设置才能正确时https  # HTTPS server  #  server {    listen    443;    server_name localhost acheng1314.cn www.acheng1314.cn;    ssl         on;    # 这里是你申请的签名,扔到conf下面的cert目录中    ssl_certificate   cert/214217283570796.pem;    ssl_certificate_key cert/214217283570796.key;    ssl_session_timeout 5m;    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;    ssl_prefer_server_ciphers  on;    location / {    #  root  html;    #  index index.html index.htm;  proxy_pass http://localhost;  proxy_set_header  Host  $host;  proxy_set_header  X-Real-IP  $remote_addr;  proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;    }}# 这里是设置域名跳转的,转发这些域名到本机的8080端口,server {   listen    80;   server_name *.acheng1314.cn acheng1314.cn;   location / {     proxy_pass http://localhost:8080/;     proxy_set_header  Host  $host;     proxy_set_header  X-Real-IP  $remote_addr;     proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;   } }   }

其实在写这个的时候必须注意的是,不管什么应用程序端口不能冲突!比如说我的nginx是绑定的80端口,如果tomcat再设定80端口,那么我的设置就算是绑定到localhost去也是会转发失败的!毕竟网络端口只能一个应用程序占用。

<!--检查我们的设置是否正确,正确或者错误都有对应的提示-->/opt/nginx/sbin/nginx -t<!--配置正确,则启用nginx-->/opt/nginx/sbin/nginx<!--重新载入配置文件-->/opt/nginx/sbin/nginx -t<!--当然到了这里的时候肯定还不能通行,毕竟我们防火墙还把443端口拦截的,所以接着走起来。--><!--添加443端口到防火墙-->/sbin/iptables -I INPUT -p tcp --dport 443 -j ACCEPT<!--保存防火墙配置-->/etc/rc.d/init.d/iptables save<!--是配置文件生效-->/etc/init.d/iptables status

走到这一步,我们可以测试一下服务器了,按照正常的来讲,我现在的服务器已经是http和https都已经完全支持了。

3、设置服务和自启

其实说来,这里基本也没啥注意的,只要nginx路径设置正确即可。

#!/bin/sh# Name:nginx4comex# nginx - this script starts and stops the nginx daemon## description: Nginx is an HTTP(S) server, HTTP(S) reverse \#        proxy and IMAP/POP3 proxy server# processname: nginx# config:   /opt/nginx/conf/nginx.conf# pidfile:   /comexHome/nginx/nginx.pid## Created By http://comexchan.cnblogs.com/# Source function library.. /etc/rc.d/init.d/functions# Source networking configuration.. /etc/sysconfig/network# Check that networking is up.[ "$NETWORKING" = "no" ] && exit 0NGINX_DAEMON_PATH="/opt/nginx/sbin/nginx"NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"NGINX_LOCK_FILE="/var/lock/subsys/nginx4comex"prog=$(basename $NGINX_DAEMON_PATH)start() {  [ -x $NGINX_DAEMON_PATH ] || exit 5  [ -f $NGINX_CONF_FILE ] || exit 6  echo -n $"Starting $prog: "  daemon $NGINX_DAEMON_PATH -c $NGINX_CONF_FILE  retval=$?  echo  [ $retval -eq 0 ] && touch $NGINX_LOCK_FILE  return $retval}stop() {  echo -n $"Stopping $prog: "  killproc $prog -QUIT  retval=$?  echo  [ $retval -eq 0 ] && rm -f $NGINX_LOCK_FILE  return $retval}restart() {  configtest || return $?  stop  start}reload() {  configtest || return $?  echo -n $"Reloading $prog: "  killproc $NGINX_DAEMON_PATH -HUP  RETVAL=$?  echo}force_reload() {  restart}configtest() { $NGINX_DAEMON_PATH -t -c $NGINX_CONF_FILE}rh_status() {  status $prog}rh_status_q() {  rh_status >/dev/null 2>&1}case "$1" in  start)    rh_status_q && exit 0    $1    ;;  stop)    rh_status_q || exit 0    $1    ;;  restart|configtest)    $1    ;;  reload)    rh_status_q || exit 7    $1    ;;  force-reload)    force_reload    ;;  status)    rh_status    ;;  condrestart|try-restart)    rh_status_q || exit 0      ;;  *)    echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"    exit 2esac

上面的代码就是用来创建服务的代码,将他们保存在nginx4comex文件中(这个文件我仍在了/opt/nginx目录下,一样使用vim编写)。注意下面的代码和你的配置对应即可。

NGINX_DAEMON_PATH="/opt/nginx/sbin/nginx"NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"

接着我们继续终端指令操作。

<!--授权nginx4comex可执行-->chmod u+x nginx4comex<!--拷贝nginx4comex到/etc/init.d目录-->cp nginx4comex /etc/init.d<!--检查运行状态-->service nginx4comex status<!--添加到启动,先vim打开启动文件,然后添加启动代码-->vim /etc/rc.local<!--添加的启动代码如下-->/etc/init.d/nginx4comex start<!--至此我们的启动也添加完成,现在重启检查效果-->reboot

最后来说我们已经设置了nginx代理tomcat,还设置了对应的服务器程序自启动。

注意!nginx4comex是不能被chkconfig的,具体原因我也不清楚,但是原作者的文章中确实用了chkconfig的方法加入了启动,对linux有兴趣的可以去试一试。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    Linux中tcpdump命令实例详解
    下一条:
    CentOS 7 安装vsftpd 服务器的具体操作步骤
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在Linux系统中使用Iptables实现流量转发功能流程步骤(0个评论)
    • vim学习笔记-入门级需要了解的一些快捷键(0个评论)
    • 在centos7系统中实现分区并格式化挂载一块硬盘到/data目录流程步骤(0个评论)
    • 在Linux系统种查看某一个进程所占用的内存命令(0个评论)
    • Linux中grep命令中的10种高级用法浅析(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-11
    • 2017-07
    • 2017-10
    • 2017-11
    • 2018-01
    • 2018-02
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-03
    • 2022-04
    • 2022-08
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-06
    • 2023-07
    • 2023-10
    • 2023-12
    • 2024-01
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客