草庐IT

Linux安装Nginx并配置启动命令

萌褚 2023-10-24 原文

镜像下载、域名解析、时间同步请点击 阿里云开源镜像站

安装前准备工作

因为Nginx依赖于gcc的编译环境,所以,需要安装编译环境来使Nginx能够编译起来

yum install gcc-c++

Nginx的http模块需要使用pcre来解析正则表达式,需要安装pcre

yum install -y pcre pcre-devel

安装依赖的解压包

yum install -y zlib zlib-devel

ssl 功能需要 openssl 库,安装 openssl

yum install -y openssl openssl-devel

下载Nginx

可以自己建立一个包,将nginx下载到这个路径,我设置的路径/opt/crm/nginx

如果需要其他nginx版本的可以参考 nginx仓库

wget http://nginx.org/download/nginx-1.10.2.tar.gz

下载完之后解压

tar zxvf nginx-1.10.2.tar.gz

进入到解压之后的nginx目录

[root@localhost src]# cd nginx-1.10.2
[root@localhost nginx-1.10.2]# ./configure && make && make install

如果要使用ssl

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

注意:如果配置了ssl,检查配置文件时报错

nginx -t
nginx:[emerg]unknown directive ssl错误

去到nginx安装的目录
./configure --with-http_ssl_module

注意要把新生成的文件复制到对应目录
cp objs/nginx /usr/local/nginx/sbin/nginx

显示成功就搞定
[root@iZ2ze02hshpth1x0vxo8r6Z 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
[root@iZ2ze02hshpth1x0vxo8r6Z sbin]# 

安装完之后查看安装目录

[root@izbp10k7vskcf4soxxbp5gz /]# whereis nginx
nginx: /usr/local/nginx
[root@izbp10k7vskcf4soxxbp5gz /]# 

通过查找文件名方式

[root@izbp10k7vskcf4soxxbp5gz /]# find / -name nginx
/opt/crm/nginx
/opt/crm/nginx/nginx-1.10.2/objs/nginx
/usr/local/nginx
/usr/local/nginx/sbin/nginx
[root@izbp10k7vskcf4soxxbp5gz /]# 

直接执行

[root@izbp10k7vskcf4soxxbp5gz /]# /usr/local/nginx/sbin/nginx
[root@izbp10k7vskcf4soxxbp5gz /]# ps -ef | grep nginx
root      4666     1  0 09:32 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody    4667  4666  0 09:32 ?        00:00:00 nginx: worker process
root      5028 29443  0 09:40 pts/0    00:00:00 grep --color=auto nginx
[root@izbp10k7vskcf4soxxbp5gz /]# 

在浏览器输入服务器IP地址

file

增加systemctl命令方式启动

直接启动和关闭nginx的方式

启动nginx的命令为     /usr/local/nginx/sbin/nginx  
停止nginx的命令为    /usr/local/nginx/sbin/nginx -s stop
重启nginx的命令为    /usr/local/nginx/sbin/nginx -s reload

配置方式 去到/usr/lib/systemd/system/目录新建一个nginx服务,给予执行权限

vim /usr/lib/systemd/system/nginx.service
chmod +x /usr/lib/systemd/system/nginx.service

打开文件nginx.service新建内容

[Unit]                                                                                      
Description=nginx - high performance web server              
After=network.target remote-fs.target nss-lookup.target   

[Service]                                                                                 
Type=forking                                                                        
PIDFile=/usr/local/nginx/logs/nginx.pid                               
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf   
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf           
ExecReload=/usr/local/nginx/sbin/nginx -s reload                                                 
ExecStop=/usr/local/nginx/sbin/nginx -s stop                                                       
ExecQuit=/usr/local/nginx/sbin/nginx -s quit                                                        
PrivateTmp=true                                                                  

[Install]
WantedBy=multi-user.target 

保存之后重载Ststemctl命令

在启动服务之前,需要先重载systemctl命令
systemctl daemon-reload

配置完之后

systemctl status nginx
systemctl start nginx
systemctl stop nginx
systemctl restart nginx

附上配置

#user  nobody;
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 {
    worker_connections  65535;
}


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;

    gzip    on;
    #允许压缩的最小字节数
    gzip_min_length 1k;
    #4个单位为16k的内存作为压缩结果流缓存
    gzip_buffers 4 16k;
    #设置识别HTTP协议版本,默认是1.1
    gzip_http_version 1.1;
    #gzip压缩比,可在1~9中设置,1压缩比最小,速度最快,9压缩比最大,速度最慢,消耗CPU
    gzip_comp_level 2;
    #压缩的类型
    gzip_types text/plain application/x-javascript text/css application/xml;
    #让前端的缓存服务器混村经过的gzip压缩的页面
    gzip_vary   on;


    # 配置转发到8700 端口
    upstream  huida{
      server  127.0.0.1:8700;
    }

    server {
        listen       80;
        listen       443 ssl;                    # 配置https,监听433端口
        server_name  xxx.xxx;                    # 注意如果申请了域名配置再此,如果配置了证书才能https访问

        error_page 405 =200 $request_uri;
       
        ssl_certificate  cert/7629385.pem;
        ssl_certificate_key cert/7629385.key;

        client_max_body_size 50m;
        underscores_in_headers on;

        proxy_set_header Host      $host;
        proxy_set_header  X-Real-IP        $remote_addr;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        index index.htm index.html index.php;

        proxy_connect_timeout 60; #建立tcp协议的连接时间
        proxy_send_timeout 60;    #发送接口的时间
        proxy_read_timeout 60;    #读取时间(接口响应时间)

        #charset koi8-r;
        #access_log  logs/host.access.log  main;

        
        # 配置转发
      location /huida/ {

                 add_header 'Access-Control-Allow-Origin' '*';
                 add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
                 add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';
                 add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';

              proxy_pass http://huida;
         }


        location / {
            root   /home/html/huida/;
            index  index.html index.htm;
        }


        #静态文件交给nginx处理 代理前端静态资源
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
        {

         root /home/html/huida/;
                expires 12;
        }

        #静态文件交给nginx处理
        location ~ .*\.(js|css)?$
        {
          root /home/html/huida/;

            expires 15d;
        }

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


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

原文链接:https://blog.csdn.net/YMZ8848/article/details/123438614

有关Linux安装Nginx并配置启动命令的更多相关文章

  1. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  2. ruby - 在 Ruby 中编写命令行实用程序 - 2

    我想用ruby​​编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序

  3. ruby - 完全离线安装RVM - 2

    我打算为ruby​​脚本创建一个安装程序,但我希望能够确保机器安装了RVM。有没有一种方法可以完全离线安装RVM并且不引人注目(通过不引人注目,就像创建一个可以做所有事情的脚本而不是要求用户向他们的bash_profile或bashrc添加一些东西)我不是要脚本本身,只是一个关于如何走这条路的快速指针(如果可能的话)。我们还研究了这个很有帮助的问题:RVM-isthereawayforsimpleofflineinstall?但有点误导,因为答案只向我们展示了如何离线在RVM中安装ruby。我们需要能够离线安装RVM本身,并查看脚本https://raw.github.com/wayn

  4. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  5. ruby - 如何为 emacs 安装 ruby​​-mode - 2

    我刚刚为fedora安装了emacs。我想用emacs编写ruby。为ruby​​提供代码提示、代码完成类型功能所需的工具、扩展是什么? 最佳答案 ruby-mode已经包含在Emacs23之后的版本中。不过,它也可以通过ELPA获得。您可能感兴趣的其他一些事情是集成RVM、feature-mode(Cucumber)、rspec-mode、ruby-electric、inf-ruby、rinari(用于Rails)等。这是我当前用于Ruby开发的Emacs配置:https://github.com/citizen428/emacs

  6. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  7. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  8. ruby-on-rails - 独立 ruby​​ 脚本的配置文件 - 2

    我有一个在Linux服务器上运行的ruby​​脚本。它不使用rails或任何东西。它基本上是一个命令行ruby​​脚本,可以像这样传递参数:./ruby_script.rbarg1arg2如何将参数抽象到配置文件(例如yaml文件或其他文件)中?您能否举例说明如何做到这一点?提前谢谢你。 最佳答案 首先,您可以运行一个写入YAML配置文件的独立脚本:require"yaml"File.write("path_to_yaml_file",[arg1,arg2].to_yaml)然后,在您的应用中阅读它:require"yaml"arg

  9. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

  10. ruby - 如何在 Lion 上安装 Xcode 4.6,需要用 RVM 升级 ruby - 2

    我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121

随机推荐