centos7 nginx编译安装

11 篇文章 0 订阅
6 篇文章 0 订阅

如果安装包下载地址下载不了,请自己搜索包名下载

首先:配置防火墙,使80端口可以通过外部访问
在centos7中

[root@localhost ~]# mkdir -p /web   ###创建web目录

###因为SELinux的关系,即便nginx程序有r权限读取/web下的文件,也是会读取失败的。我们需要semanage这个工具来更改/web目录的默认SELinux设置,通过命令yum provides semanage查找到这个工具是由policycoreutils-python这个软件包提供的,因此:
[root@localhost ~]# yum install policycoreutils-python

[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?"    ### 将/web目录(及其子目录和文档)的默认SELinux类型设定为nginx可以读取的httpd_sys_content_t:

[root@localhost ~]# semanage fcontext -l | grep '/web'   ###确认上面的规则是否添加成功:

[root@localhost ~]# restorecon -Rv /web   ###让/data/www目录的SELinux类型恢复成上面设定的默认值

[root@localhost ~]# ls -dZ /web   ###确认/data/www目录的SELinux类型是否的确为httpd_sys_content_t:

[root@localhost ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent    让防火墙放通tcp的 80端口:--permanent是永久生效,没有他关机重启就失效了

[root@localhost ~]# firewall-cmd --reload   ###重启防火墙以让更改生效:

[root@localhost ~]# firewall-cmd --list-all   ###使用以下命令确认防火墙配置是否成功:

一、安装nginx
1.安装pcre
下载:https://ftp.pcre.org/pub/pcre/pcre-8.40.tar.gz
首先检查服务器是否已安装pcre
[root@localhost tmp]# rpm -qa | grep pcre

上面查到了pcre已存在,但是版本太旧的,需要安装一个更新版本的

 编译安装pcre        

(1)首先解压 tar -zxf pcre-8.40.tar.gz
(2)进入解压之后的目录 cd pcre-8.40
(3)编译配置 ./configure –prefix=/usr/local/pcre
如果编译最后出现 configure: error: You need a C++ compiler for C++ support. 就yum安装gcc和gcc-c++
(4)make && make install

2.安装zlib
下载:https://excellmedia.dl.sourceforge.net/project/libpng/zlib/1.2.11/zlib-1.2.11.tar.gz
编译安装zlib

(1)首先解压 tar -zxf zlib-1.2.11.tar.gz
(2)进入解压之后的目录 cd zlib-1.2.11
(3)编译配置 ./configure –prefix=/usr/local/zlib
(4)make && make install

3.安装openssl
下载:https://www.openssl.org/source/openssl-1.1.0e.tar.gz
注意:这里安装是用config不是configure哦~~编译相对比上面久一点
编译安装openssl

(1)首先解压 tar -zxf openssl-1.1.0e.tar.gz
(2)进入解压之后的目录 cd openssl-1.1.0e
(3)编译配置 ./config –prefix=/usr/local/openssl
(4)make && make install

4.安装gd库

[root@localhost tmp]# yum -y install gd-devel​

5.安装nginx
编译安装nginx

(1)首先解压 tar -zxf nginx-1.10.3.tar.gz
(2)进入解压之后的目录 cd nginx-1.10.3
[root@localhost nginx-1.10.3]# groupadd​ ngin
[root@localhost nginx-1.10.3]# useradd -g nginx nginx -s /bin/false
(3)编译配置 [root@localhost nginx-1.10.3]# ./configure –prefix=/usr/local/nginx –user=nginx –group=nginx –with-http_ssl_module –with-http_realip_module –with-http_image_filter_module –with-http_sub_module –with-http_gzip_static_module –with-http_stub_status_module –with-pcre=/tmp/pcre-8.40 –with-zlib=/tmp/zlib-1.2.11 –with-openssl=/tmp/openssl-1.1.0e
注释:关联其他文件时,是关联解压之后./configure时候得目录(源码目录)
(4)make && make install

修改配置文件

[root@localhost nginx-1.10.3]# vi /usr/local/nginx/conf/nginx.conf


###nginx.conf
user  nginx nginx;
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 {
    use epoll;
    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;

    tcp_nodelay  on;

    gzip  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    client_header_buffer_size  1k;
    large_client_header_buffers  4 4k;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        access_log  logs/host.access.log  main;

        location / {
            root   /web;
            index  index.html index.htm index.php;
        }

        #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   /web;
        }

        location ~ ^/(images|javascript|js|css|flash|media|static)/{
            root /var/www/virtual/hedocs;
            expires 24h;
        }

        # 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           /web;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            #fastcgi_pass   127.0.0.1:9000;
            fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
            # the better form of fastcgi_pass than before
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /$document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        location /NginxStatus{
            stub_status  on;
            access_log   on;
            auth_basic   on;
            auth_basic_user_file  conf/htpasswd;
        }

        # 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 server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

启动nginx

[root@localhost nginx]# /usr/local/nginx/sbin/nginx

Nginx安装完毕,这个是在虚拟机上面安装的,ip地址是192.168.1.168,这是我们用浏览器访问192.168.1.202,访问失败,什么原因,你应该很快想到是防火墙

增加开机自启动
1.新增shell脚本
在/etc/​rc.d/init.d/下面增加一个脚本,名称 nginx

[root@localhost nginx]# vi /etc/rc.d/init.d/nginx
###脚本内容如下


#! /bin/bash
# chkconfig: 35 85 15  
# description: Nginx is an HTTP(S) server, HTTP(S) reverse
set -e
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
SCRIPTNAME=/etc/init.d/$NAME
test -x $DAEMON || exit 0
d_start(){
    $DAEMON || echo -n " already running"
}
d_stop() {
    $DAEMON -s quit || echo -n " not running"
}
d_reload() {
    $DAEMON -s reload || echo -n " counld not reload"
}
case "$1" in
start)
    echo -n "Starting $DESC:$NAME"
    d_start
    echo "."
;;
stop)
    echo -n "Stopping $DESC:$NAME"
    d_stop
    echo "."
;;
reload)
    echo -n "Reloading $DESC configuration..."
    d_reload
    echo "reloaded."
;;
restart)
    echo -n "Restarting $DESC: $NAME"
    d_stop
    sleep 2
    d_start
    echo "."
;;
*)
    echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
    exit 3
;;
esac
exit 0

2.加入系统服务并开机自启动

 chmod +x /etc/rc.d/init.d/nginx (设置可执行权限)
 chkconfig --add nginx (添加系统服务)
 chkconfig --level 35 nginx on (开机自启动)

加入环境变量并更新生效

[root@localhost init.d]# echo -e '\nexport PATH=/usr/local/nginx/sbin:$PATH\n'   >>   /etc/profile && source /etc/profile

# 1.启动nginx
nginx
# 可通过ps -ef | grep nginx查看nginx是否已启动成功
# 2.停止nginx
nginx -s stop
# 3. 重新启动
nginx -s reload

顺便提一下另外一个方法,但是不建议采用该方法:
方法二:
一.nginx安装
1.下载对应当前系统版本的nginx rpm包(package)
wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2.建立nginx的yum仓库(默认yum是没有nginx的)
rpm -ivh nginx-release-centos-7-0.el7.ngx.noarch.rpm
3.下载并安装nginx
yum install nginx
4.nginx启动(nginx安装目录下-/usr/sbin/)
systemctl start nginx.service

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值