Nginx 入门教程

8 篇文章 0 订阅
3 篇文章 0 订阅

Nginx 编译安装

Nginx是一个性能出色的http服务器和反向代理服务器,占内存小,并发能力强,处理静态页面能力好。

Nginx 由内核和模块组成。

相对于 Apache 优点

  • 高并发响应性能非常好
  • 反向代理性能非常强,(可用于负载均衡)
  • 内存和 CPU 占有率低
  • 对后端服务有健康检查功能
  • 支持 PHP cgi 方式和 fastcgi 方式
  • 配置代码简洁、容易上手

系统环境


  • CentOS 6.8
  • Nginx 1.12.0
  • pcre 支持正则
  • openssl

我们只将Nginx用于纯 Web 服务环境

安装依赖包

$ yum install -y gcc gcc-c++ make pcre-devel openssl-devel
  • pcre 正则处理需要
  • gcc 编译需要
  • openssl 安全链接需要

编译与安装


从 Nginx 官网下载源码包并解压

$ cd /usr/local
$ wget http://nginx.org/download/nginx-1.12.0.tar.gz
$ tar xzvf nginx-1.12.0.tar.gz

进入 Nginx 源码包目录然后开始编译、安装位置一般选 /usr/local 为了方便以后的配置文件修改

$ cd nginx-1.12.0
$ ./configure --prefix=/usr/local/nginx #配置时如果有错误一般都是缺少了什么开发包,yum装一下就行了

开始 Nginx 编译和安装

$ make && make install

手动启动 Nginx 服务

$ cd /usr/local/nginx
$ ./sbin/nginx -t      //检查下 Nginx 配置文件是否有语法问题
$ ./sbin/nginx

Nginx 源码安装完成后默认不会注册为系统服务,所以需手工添加系统服务脚本。

CentOS 6.8 在 /etc/init.d 目录下新建 nginx 文件,并更改权限即可

$ vim /etc/ini.d/nginx
下面的、填一下内容(根据自己实际情况修改)
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# this script create it by caffreyxin at 2007.10.15.
# it is v.0.0.1 version.
# if you find any errors on this scripts, please contact wylbjia.
# and send mail to wylbjia at 736589354@qq.com
#
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
#              It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /var/run/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf

nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/var/run/nginx.pid

RETVAL=0
prog="nginx"

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

[ -x $nginxd ] || exit 0


# Start nginx daemons functions.
start() {

    if [ -e $nginx_pid ];then
        echo "nginx already running...."
        exit 1
    fi

    echo -n $"Starting $prog: "
    daemon $nginxd -c ${nginx_config}
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
    return $RETVAL
}


# Stop nginx daemons functions.
stop() {
    echo -n $"Stopping $prog: "
    killproc $nginxd
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}


# reload nginx service functions.
reload() {

    echo -n $"Reloading $prog: "
    #kill -HUP `cat ${nginx_pid}`
    killproc $nginxd -HUP
    RETVAL=$?
    echo

}

# See how we were called.
case "$1" in
start)
        start
        ;;

stop)
        stop
        ;;

reload)
        reload
        ;;

restart)
        stop
        start
        ;;

status)
        status $prog
        RETVAL=$?
        ;;
*)
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"
        exit 1
esac

exit $RETVAL

修改文件权限、设置为开机启动

$ chmod 755 /etc/init.d/nginx
$ chkconfig nginx on

下面就可以使用Service命令管理 Nginx 了

$ service nginx start | stop | reload

下面在浏览器输入 IP 就可以看到 Nginx 页面了,如果没有记得关闭防火墙和 Selinux

CentOS7.2/Ubuntu 下 Nginx 开机启动

1.在系统服务目录里创建 nginx.service 文件

vi /lib/systemd/system/nginx.service

内容如下

[Unit]
Description=nginx
After=network.target
  
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target
##服务说明
[Unit]:服务的说明
Description:描述服务
After:描述服务类别
[Service]服务运行参数的设置
Type=forking是后台运行的形式
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注意:[Service]的启动、重启、停止命令全部要求使用绝对路径
[Install]运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3

2.设置开机启动

systemctl enable nginx

systemctl start/stop/restart/status nginx

systemctl list-units --type=service

Ngin的页面乱码解决
  • 在server段里加一下两行

    default_type 'text/html';
    charset utf-8;
    

    然后重启

Nginx rewrite

http://www.cnblogs.com/xiaoit/p/3991037.html


这篇文章写得很好

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值