美文网首页
【nginx安装配置】

【nginx安装配置】

作者: Y了个J | 来源:发表于2018-08-11 16:35 被阅读15次

1.CentOS 7上安装Nginx

使用uname -a 查看linuxe内核是否时2.6及以上版本
因为只有linux2.6及以上版本才支持epoll,能够更大限度发挥nginx的威力


安装gcc gcc-c++(如新环境,未安装请先安装)
GCC(GNU Compile Collection)可以用来编译C语言程序,因为有时候nginx不会直接提供二进制可执行程序,需要自己编译
sudo yum install -y gcc gcc-c++


配置epel yum 源
wget http://dl.Fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -ivh epel-release-latest-7.noarch.rpm


PCRE库
Perl正则兼容表达式包,pcre-devel时PCRE做二次开发时所需要的开发库,也是nginx开发所必需的


zlib库 
nginx.conf中配置了gzip on对Http 包的内容作gzip格式压缩,需要用zlip-devel是二次开发所需要的库
yum install -y gzip gzip-devel


OpenSSL库 
如果想使用更安全的SSL 协议传输HTTP,就需要该包,如果要使用MD5或者SHA1包,也需要Openssl包
yum install -y openssl openssl-devel


sudo yum install -y nginx

查看已开放的端口(默认不开放任何端口)
sudo firewall-cmd --list-ports
开启80端口
sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
# 关闭防火墙
sudo firewall-cmd --permanent --zone=public --add-service=http 
sudo firewall-cmd --permanent --zone=public --add-service=https
重启防火墙
sudo firewall-cmd --reload
停止防火墙
sudo systemctl stop firewalld.service
禁止防火墙开机启动
sudo systemctl disable firewalld.service
删除
sudo firewall-cmd --zone= public --remove-port=80/tcp --permanent

启动Nginx并设置开机自动运行
sudo systemctl start nginx.service
sudo systemctl enable nginx.service

2.mac上安装Nginx

Homebrew安装nginx

brew install nginx 

安装完以后,可以在终端输出的信息里看到一些配置路径:
/usr/local/etc/nginx/nginx.conf (配置文件路径)
/usr/local/var/www (服务器默认路径)
/usr/local/Cellar/nginx/1.12.0 (安装路径)(我安装的是1.12.0,具体参照自己安装的版本)
终端输入nginx 然后浏览器访问localhost:8080,成功说明安装好了。

3.nginx常用命令

判断配置文件是否正确  sudo nginx -t 
启动 sudo nginx
重启 sudo nginx -s reload 
查询nginx主进程号  ps -ef|grep nginx  
停止 sudo nginx -s stop
正常停止   sudo kill -QUIT 主进程号
快速停止   sudo kill -TERM 主进程号
nginx -c filename 指定配置文件
nginx -h 帮助
#重新加载配置|重启|停止|退出 nginx
nginx -s reload|reopen|stop|quit

在电脑里弄两个tomcat,我们分别命名为tomcat1和tomcat2,修改tomcat2的conf下的server.xml里的端口号不与tomcat1冲突

<?xml version='1.0' encoding='utf-8'?>

<Server port="8006" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.apache.catalina.core.JasperListener" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <Service name="Catalina">
    <Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="apache-tomcat-7.0.47">
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

mac下nginx的配置文件在:/usr/local/etc/nginx/nginx.conf


http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    # tomcat的服务
    upstream server_lb{
        server 127.0.0.1:8080;
        server 127.0.0.1:8081;
    }

    server {
        listen       80;

        server_name  localhost;
        #server_name  www.yangjun.com;
        
        location / {
            root   html;
            # 反向代理
            proxy pass http://server_lb;
            index  index.html index.htm;
        }

        #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   html;
        }

        # 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           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
}

相关文章

网友评论

      本文标题:【nginx安装配置】

      本文链接:https://www.haomeiwen.com/subject/ttmdbftx.html