nginx安装

傻男人 1年前 ⋅ 714 阅读

nginx安装

一、docker的安装方式

创建配置文件(方便将文件持久化操作)

mkdir -p /usr/local/nginx/html /usr/local/nginx/conf /usr/local/nginx/conf.d /usr/local/nginx/logs /usr/local/nginx/project /usr/local/nginx/cert /usr/local/nginx/conf/vhost

创建index.html

  • 进入首页的文件目录
    cd /usr/local/nginx/html;
  • 直接编辑
vim index.html;
# 如果没有内容则添加如下的内容,存在则直接退出
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>我的nginx</title>
</head>
<body>
    <h1>我是最帅的男人</h1>
    <p>第一个页面</p>
</body>
</html>

添加nginx的nginx.conf配置文件

  • 进入配置文件的目录
    cd /usr/local/nginx/conf
  • 直接编辑
vim nginx.conf;
# 如果没有内容则添加如下的内容,存在则直接退出
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  20000;
}

http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;

    keepalive_timeout  65;

    include /usr/local/nginx/conf/vhost/*.conf;
}

添加nginx的default.conf配置文件

  • 进入配置文件的目录
    cd /usr/local/nginx/conf/vhost
  • 直接编辑
vim nginx.conf;
# 如果没有内容则添加如下的内容,存在则直接退出
server {
    listen       80;
    server_name  localhost;

    location / {
        root   /usr/local/nginx/html;
        index  index.html index.htm;
    }

    location /api{
        proxy_pass http://192.168.1.1:9999/api;
        # access_log "logs/test.log";
    }
}

启动nginx

docker run --name nginx --restart=always \
  -d -p 80:80 -p 443:443 \
  -v /usr/local/nginx/html:/etc/nginx/html \
  -v /usr/local/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
  -v /usr/local/nginx/conf.d:/etc/nginx/conf.d \
  -v /usr/local/nginx/project:/usr/local/nginx/project \
  -v /usr/local/nginx/cert:/usr/local/nginx/cert \
  -v /usr/local/nginx/logs:/var/log/nginx  nginx

二、普通的安装方式

安装支持nginx软件包

不执行这步,后面的编译不会通过

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

进入到nginx安装目录

$ cd /usr/local

创建nginx的存放目录

$ mkdir nginx

官网下载nginx ,并上传到/usr/local/nginx

解压上传的nginx的安装包

$ tar -zxvf nginx-1.9.9.tar.gz

进入nginx目录

$ cd nginx-1.9.9

配置nginx

$ ./configure --add-module=/usr/local/nginx/module/nginx-http-flv-module --with-http_ssl_module --with-stream --with-debug --with-http_gzip_static_module

--prefix=/usr/local/nginxTemp 配置安装路径,不安装到默认路径,以便安装文件移植和卸载,是一个可选参数 --add-module=/usr/local/nginx/module/nginx-rtmp-module 这个是配置nginx-rtmp-module插件,插件的下载地址https://github.com/arut/nginx-rtmp-module nginx-http-flv-module的插件地址 git clone https://github.com/winshining/nginx-http-flv-module.git nginx-http-flv-module 包含nginx-rtmp-module的所有 两个最好不要一起编译安装

编译并安装

$ make && make install

启动nginx

$ /usr/local/nginx/sbin/nginx -s reload

验证nginx是否启动成功

sudo curl http://localhost:80

如果出现以下的内容则证明启动nginx成功

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
  • 如果出现报错:nginx: [error] open() "/usr/local/nginx/logs/nginx.pid" failed 则运行:
    /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

三、nginx的常用命令

测试配置文件,不运行,而仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件

./nginx -t

快速关闭Nginx,可能不保存相关信息,并迅速终止web服务。

./nginx -s stop

重启

./nginx -s reload

显示版本

./nginx -v

显示版本,编译器版本和配置参数。

./nginx -V

指定一个配置文件,来代替缺省的。

./nginx -c filename

重新打开日志文件

./nginx -s reopen

平稳关闭Nginx,保存相关信息,有安排的结束web服务。

./nginx -s quit


全部评论: 0

    我有话说: