Nginx安装

Nginx安装与配置

Nginx安装

进入/usr/local/src目录下,下载一个Nginx的稳定版。

[root@shuai-01 ~]# cd /usr/local/src
[root@shuai-01 src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz

解压压缩包:

[root@shuai-01 src]# tar zxvf nginx-1.12.2.tar.gz

编译:
进入Nginx目录下
编译根据自己的要求来编译,选择一些模块。
我这里只指定文件路径:

[root@shuai-01 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx

接着make:

[root@shuai-01 nginx-1.12.2]# make

make install:

[root@shuai-01 nginx-1.12.2]# make install

[root@shuai-01 nginx-1.12.2]# ls /usr/local/nginx/
conf  html  logs  sbin

conf : 配置文件目录
html : 网页样例
logs: 日志
sbin: 进程,核心文件

给配置文件做一个启动脚本(/etc/init.d/nginx):

vim /etc/init.d/nginx

将这段写入脚本:

#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start() 
{
    echo -n $"Starting $prog: "
    mkdir -p /dev/shm/nginx_temp
    daemon $NGINX_SBIN -c $NGINX_CONF
    RETVAL=$?
    echo
    return $RETVAL
}
stop() 
{
    echo -n $"Stopping $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -TERM
    rm -rf /dev/shm/nginx_temp
    RETVAL=$?
    echo
    return $RETVAL
}
reload()
{
    echo -n $"Reloading $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -HUP
    RETVAL=$?
    echo
    return $RETVAL
}
restart()
{
    stop
    start
}
configtest()
{
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
}
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac
exit $RETVAL

更改配置文件权限:

[root@shuai-01 nginx-1.12.2]# chmod 755 /etc/init.d/nginx 

添加为系统服务,并设置为开机自启动:

[root@shuai-01 nginx-1.12.2]# chkconfig --add nginx
[root@shuai-01 nginx-1.12.2]# chkconfig nginx on

配置Nginx的配置文件:
Nginx的conf目录下有nginx.conf这个文件了。

[root@shuai-01 nginx-1.12.2]# cd /usr/local/nginx/conf
[root@shuai-01 conf]# 
[root@shuai-01 conf]# ls
fastcgi.conf            koi-win             scgi_params
fastcgi.conf.default    mime.types          scgi_params.default
fastcgi_params          mime.types.default  uwsgi_params
fastcgi_params.default  nginx.conf          uwsgi_params.default
koi-utf                 nginx.conf.default  win-utf

不用,用自己创建的配置文件。
先将nginx.conf改名

[root@shuai-01 conf]# mv nginx.conf nginx.conf.1

自己创建

[root@shuai-01 conf]# vim nginx.conf

写入:

user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
    use epoll;
    worker_connections 6000;
}
http
{
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm 
    application/xml;
    server
    {
        listen 80;
        server_name localhost;
        index index.html index.htm index.php;
        root /usr/local/nginx/html;
        location ~ \.php$ 
        {
            include fastcgi_params;
            fastcgi_pass unix:/tmp/php-fcgi.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
        }    
    }
}

编辑配置文件后,检查语法:

[root@shuai-01 conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

启动nginx

[root@shuai-01 conf]# /etc/init.d/nginx start
Starting nginx (via systemctl):                            [  确定  ]

user 用来定义启动nginx服务的是那个用户。
worker_processes 2; //子进程有两个

error_log /usr/local/nginx/logs/nginx_error.log crit; //错误日志

worker_rlimit_nofile 51200;//nginx最多打开多少个文件
events
{
use epoll;//使用epoll模式
worker_connections 6000;//最多的连接数
}

server 和Apache配置文件中的virtual hosts 一样,对应虚拟主机。

    server
    {
        listen 80; //监听80端口
        server_name localhost; //域名
        index index.html index.htm index.php;//默认页
        root /usr/local/nginx/html; //网站根目录所在
        location ~ \.php$ 
        {
            include fastcgi_params;
            fastcgi_pass unix:/tmp/php-fcgi.sock; //指定PHP-fpm的监听端口或者监听sock
                监听127.0.0.1:9000
                fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
        } // 配置解析PHP的部分
    }

nginx配置详解:
http://www.ha97.com/5194.html
https://my.oschina.net/duxuefeng/blog/34880

nginx默认虚拟主机

### 不同操作系统上的 Nginx 安装方法 #### Ubuntu 和 Debian 系统 对于基于 Debian 的 Linux 发行版,可以使用 APT 包管理器来安装 Nginx。 ```bash sudo apt update sudo apt install nginx ``` 这会自动下载并安装最新稳定版本的 Nginx 及其依赖项[^1]。 #### CentOS, RHEL 或 Fedora 系统 在 Red Hat 类似发行版中,YUM 是默认的包管理系统。可以通过 YUM 来安装 Nginx: ```bash sudo yum install epel-release sudo yum install nginx ``` 上述命令先启用了 EPEL 仓库,因为官方源可能不包含最新的 Nginx 版本;接着通过 `yum` 下载并安装 Nginx[^4]。 #### 麒麟操作系统 (Kylin OS) 针对麒麟操作系统的环境,有特定的方式来进行 Nginx 的离线安装。首先需要获取到 Nginx 的 RPM 文件,之后再执行本地安装过程: ```bash yum -y install --downloadonly --downloaddir=/home/nginx_install nginx # 获取离线安装包 rpm -ivh /home/nginx_install/nginx*.rpm # 执行RPM安装 ``` 完成安装后,可通过如下方式启动服务以及验证是否正常工作: ```bash systemctl start nginx # 启动Nginx服务 curl http://localhost # 测试访问 ``` 另外,在银河麒麟系统中也可以直接进入解压后的目录并通过脚本启动 Nginx 实例: ```bash cd nginx/sbin ./nginx # 使用自带脚本启动Nginx ``` 此时可以在浏览器里输入服务器 IP 地址来确认 Web 页面能否被正确展示出来[^3]。 #### macOS 环境下 Homebrew 方式 如果是在 Mac 上,则推荐利用 Homebrew 工具简化整个流程: ```bash brew install nginx # 利用Homebrew安装Nginx ``` 以上就是在多种主流平台上部署 Nginx 的基本指导说明。每种平台都有各自的特点和最佳实践方案可供选择。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值