在Centos上编译安装nginx

简介:

实验环境:

    OS: CentOS 6.6 

   nginx:nginx-1.6.2.tar.gz


前期准备:
安装开发包组件
[root@1inux tmp]# yum -y groupinstall "Development tools" "Server Platform Development"
[root@1inux tmp]# yum -y install pcre-devel

一、 编译安装: 


[root@1inux tmp]# useradd -r nginx    //添加nginx系统用户
[root@1inux tmp]# tar xf nginx-1.6.2.tar.gz 
[root@1inux tmp]# cd nginx-1.6.2
[root@1inux nginx-1.6.2]# ./configure --help        //获取帮助

[root@1inux nginx-1.6.2]#  mkdir -pv /var/tmp/nginx/{client,proxy,fastcgi,uwsgi}    //创建编译安装需要的目录

1
[root@1inux nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --user=nginx --group=nginx --error-log-path=/ var /log/nginx/error.log --http-log-path=/ var /log/nginx/access.log --pid-path=/ var /run/nginx/nginx.pid --lock-path=/ var /lock/nginx.lock --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_mp4_module --http-client-body-temp-path=/ var /tmp/nginx/client --http-proxy-temp-path=/ var /tmp/nginx/proxy --http-fastcgi-temp-path=/ var /tmp/nginx/fastcgi --http-uwsgi-temp-path=/ var /tmp/nginx/uwsgi

添加path路径

1
[root@1inux nginx]#  echo   "export PATH=/usr/local/nginx/sbin/nginx:$PATH"  > /etc/profile.d/nginx.sh

加载:

1
[root@1inux nginx]# . /etc/profile.d/nginx.sh

启动nginx

1
2
3
4
[root@1inux nginx]# /usr/local/nginx/sbin/nginx
[root@1inux nginx]# ss -tunlp | grep :80
tcp    LISTEN     0      128                    *:80                    *:*      users:(( "nginx" ,52985,6),( "nginx" ,52986,6))
[root@1inux nginx]#

查看nginx启动进程情况

1
2
3
4
5
[root@1inux nginx]# ps aux | grep nginx
root      52985  0.0  0.1  45044  1064 ?        Ss   03:54   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     52986  0.0  0.1  45472  1636 ?        S    03:54   0:00 nginx: worker process      
root      52991  0.0  0.0 103252   836 pts/8    S+   03:55   0:00 grep nginx
[root@1inux nginx]#
1
2
3
4
5
6
7
8
9
10
11
12
13
[root@1inux nginx]# /usr/local/nginx/sbin/nginx -h    //查看nginx 选项
nginx version: nginx/1.6.2
Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
   -?,-h         : this help
   -v            : show version  and  exit
   -V            : show version  and  configure options then  exit
   -t            : test configuration  and  exit
   -q            : suppress non-error messages during configuration testing
   -s signal     : send signal to a master process: stop, quit, reopen, reload
   -p prefix     : set prefix path ( default : /usr/local/nginx/)
   -c filename   : set configuration file ( default : /etc/nginx/nginx.conf)
   -g directives : set  global  directives out of configuration file

常用配置指令:
    server {
                }  //定义一个虚拟主机
                
    2、listen   //定义监听端口
                listen address[:port];
                listen port;
                
    3、server_name  NAME [...];         【定义服务器主机名----只能用在server中】        
            后可跟多个主机名:名称还可以使用正则表达式(~)或通配符,检查标准如下
                    (1)先做精确匹配检查;
                    (2)左侧通配符匹配检查;*.1inux.com 
                    (3) 右侧通配符匹配检查;如 mail.*  
                    (4) 正则表达式匹配检查:如 ~^.*\.1inux.com\.com$
                    (5) default_server;
                
--------------------------------------------------------
编辑配置文件  在http {} 中添加如下:

        server {
                listen 888;
                server_name 888.1inux.com;
                root "/vhost/888/html/";
        }

重新加载
[root@1inux nginx]# /usr/local/nginx/sbin/nginx -s reload
[root@1inux nginx]# ss -tnlp | grep nginx
LISTEN     0      128                       *:888                      *:*      users:(("nginx",6568,11),("nginx",7659,11))
LISTEN     0      128                       *:80                       *:*      users:(("nginx",6568,6),("nginx",7659,6))
[root@1inux nginx]# 

添加主页面 

 # echo "<h1> This is 888.1inux.com </h1>" > /vhost/888/html/index.html

然后访问如图

wKioL1VyUtiABYvYAADIcc1KRag782.jpg
--------------------------------------------------------

    4、root path;
            设置资源路径映射;用于指明请求的URL所对应的资源所在的文件系统上的起始路径;
                【其使用范围:http, server, location,   if in location                 location 内的 优先级高于server】


    5、location [  = | ~ | ~* | ^~ ] uri { ... } 
                location @name { ... } 
            使用范围: server, location 
                
            功能:允许根据用户请求的URL来匹配定义的各location; 匹配到时,此请求将被响应的location配置块中的配置所处理,例如做访问控制等功能
                
                = : 精确匹配检查;
                ^~: RUI的前半部分匹配,不支持正则表达式;
                ~ : 正则表达式模式匹配检查,区分字符大小写;
                ~*: 正则表达式模块匹配检查,不区分字符大小写;

    匹配的优先级:   精确匹配(=)   ^~     ~    ~*   不带任何符号的location;
-------------------------
eg:
创建目录及文件 
[root@1inux /]# mkdir /vhost/{www,images/img} -pv
mkdir: created directory `/vhost'
mkdir: created directory `/vhost/www'
mkdir: created directory `/vhost/images'
mkdir: created directory `/vhost/images/img'
[root@1inux /]# echo "<h1> This is www.1inux.com </h1>" >/vhost/www/index.html

[root@1inux vhost]# tree /vhost
/vhost
|-- images
|   `-- img
|       |-- 1.jpg
|       |-- 2.jpg
|       `-- mylinux2.jpg
`-- www
    `-- index.html

在配置文件中添加如下:

1
2
3
4
5
6
7
8
9
10
         server {
                 listen 888;
                 server_name www.1inux.com;
                 location / {
                         root  "/vhost/www/" ;
                         }
                 location /img/ {
                         root  "/vhost/images/" ;
                         }
         }

        

1
2
3
4
5
[root@1inux nginx]# /usr/local/nginx/sbin/nginx -s reload
[root@1inux nginx]# ss -tnlp | grep  "nginx"
LISTEN     0      128                       *:888                      *:*      users:(( "nginx" ,18499,11),( "nginx" ,18803,11))
LISTEN     0      128                       *:80                       *:*      users:(( "nginx" ,18499,6),( "nginx" ,18803,6))
[root@1inux nginx]#

注意 此时 使用root  定义  访问 http://www.1inux.com:888/img/1.jpg  实际访问的Web服务器路径是:/vhost/images/目录下的/img/1.jpg
--------------------------------

    6、alias path;
        用于location配置段,定义路径别名

                location /img/ {
                        root "/vhost/images/";
                        }
                        
        //http://www.1inux.com:888/img/2.jpg        ====》  /vhost/images/img/2.jpg 
        //即  访问路径中的/img/对应的是   Web本地/vhost/images/目录下的目录
                        
                location /pic/ {
                        alias "/vhost/picture/";
                        }

                //http://www.1inux.com:888/pic/2.jpg        ====》  /vhost/picture/2.jpg
                //即  访问路径中的/pic/目录对应的是   Web本地/vhost/picture/目录


    7、index file;
                默认主页面;
                    index index.php index.html;

本文转自 1inux 51CTO博客,原文链接:http://blog.51cto.com/1inux/1659050


相关文章
|
16天前
|
负载均衡 Java 应用服务中间件
nginx安装在linux上
nginx安装在linux上
42 2
|
22天前
|
应用服务中间件 nginx
树莓派安装Nginx服务结合内网穿透实现无公网IP远程访问
树莓派安装Nginx服务结合内网穿透实现无公网IP远程访问
|
1月前
|
应用服务中间件 Linux 网络安全
CentOS 7.4源码编译nginx1.12 并且隐藏nginx的版本
CentOS 7.4源码编译nginx1.12 并且隐藏nginx的版本
17 0
|
16天前
|
存储 Ubuntu 应用服务中间件
【Nginx】centos和Ubuntu操作系统下载Nginx配置文件并启动Nginx服务详解
【Nginx】centos和Ubuntu操作系统下载Nginx配置文件并启动Nginx服务详解
22 1
|
2天前
|
应用服务中间件 nginx Docker
docker安装nginx
`docker search`找镜像,`pull`下载,后台 `-d` 运行容器,命名 `--name`,映射端口 `-p`。本机测试,确保服务器安全组开放端口,公网通过`http://ip:port`访问。用`docker stop id`停止容器。[查看详情](https://blog.csdn.net/javayoungcoolboy/article/details/134976510)
|
3天前
|
应用服务中间件 网络安全 nginx
nginx(1.13.7)首次安装出现:【make: *** 没有规则可以创建“default”需要的目标“build” 问题】解决措施
nginx(1.13.7)首次安装出现:【make: *** 没有规则可以创建“default”需要的目标“build” 问题】解决措施
|
5天前
|
Ubuntu 应用服务中间件 nginx
ubuntu编译安装nginx及安装nginx_upstream_check_module模块
以上是编译安装Nginx和安装 `nginx_upstream_check_module`模块的基本步骤。根据你的需求和环境,你可能需要进一步配置Nginx以满足特定的要求。
18 3
|
11天前
|
弹性计算 应用服务中间件 Shell
一键编译安装Nginx脚本
【4月更文挑战第30天】
18 1
|
11天前
|
关系型数据库 MySQL 应用服务中间件
centos7在线安装jdk1.8+tomcat+mysql8+nginx+docker
现在,你已经成功在CentOS 7上安装了JDK 1.8、Tomcat、MySQL 8、Nginx和Docker。你可以根据需要配置和使用这些服务。请注意,安装和配置这些服务的详细设置取决于你的具体需求。
37 2
|
16天前
|
负载均衡 前端开发 应用服务中间件
Nginx安装与使用
Nginx安装与使用
40 0