Linux安装Nginx并配置启动命令_linux启动nginx命令-程序员宅基地

技术标签: Linux  nginx  运维  linux  

安装前准备工作

因为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地址
在这里插入图片描述

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

解决nginx:unknown directive ssl错误

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/YMZ8848/article/details/123438614

智能推荐

春松客服部署-程序员宅基地

文章浏览阅读2k次。[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fEWZETaq-1628470395423)(file:///C:\Users\student\AppData\Local\Temp\ksohtml25096\wps1.jpg)]如何在Linux服务器上安装https://www.cnblogs.com/kingsonfu/p/11576797.htmldocker1、首先得是64位的系统,内核版本大于3.10。用root用户操作内核版本查看指令uname -r2

logstash->es错误-程序员宅基地

文章浏览阅读2.5k次。[2018-08-16T23:41:32,721][WARN ][logstash.outputs.elasticsearch] Could not index event to Elasticsearch. {:status=>400, :action=>["index", {:_id=>"22c6c6e8398049f783fe0862c08f71bb", :_index=...

如何准确判断 WebView 加载完成_js如何判断webview加载完成-程序员宅基地

文章浏览阅读1w次。如何准确判断 WebView 加载完成原文地址:http://www.jianshu.com/p/897e2d82ee43正常情况下我们把处理网页加载完毕的代码放在- (void)webViewDidFinishLoad:(UIWebView *)webView 里。但 WebViewDidFinishLoad 时网页真的加载完了吗?官方文档并没有说明 WebViewDidFinish..._js如何判断webview加载完成

从源码编译安装msp430-gcc 4.7.2_msp430-gcc --version-程序员宅基地

文章浏览阅读1k次。文章目录Install essenstialRemove Latest TEXINFO package using Synaptics.remove old gcc-mspBuild GCC-MSPInstall essenstialsudo apt-get install patch ncurses-dev build-essential bison flex libgmp3-dev li..._msp430-gcc --version

黑马程序员__基础视频day6-程序员宅基地

文章浏览阅读352次。---------------------- android培训、java培训、期待与您交流! ---------------------- 看完了第六天的视频内容,感觉收获好多了,光是在本子上做的笔迹就有4页。越学越觉得java好强大,能实现的功能好多啊。由于最近学习任务挺重的所以能抽出来看视频的时间也没那么集中了,第六天的视频也是分两天才看完的。废话不多说,下面就总结下第六天视频的一

微信小程序报错 Now you can provide attr `wx:key` for a `wx:for` to improve performance._微信小程序now you can provide attr `wx:key` for a `wx:f-程序员宅基地

文章浏览阅读4k次,点赞5次,收藏3次。出现黄色警告Now you can provide attr wx:key for a wx:for to improve performance.不影响运行效果只需要在 wx:for 后面添加 wx:key=“key” 可消除警告。如下:<block wx:for="{{news}}" wx:key="key"></block>_微信小程序now you can provide attr `wx:key` for a `wx:for` to improve perfor

随便推点

环形链表(算法java)_java 实现环形链表-程序员宅基地

文章浏览阅读196次。环形链表(算法java)的两种解决方法_java 实现环形链表

docker部署Airflow(修改URL-path、更换postgres -->myslq数据库、LDAP登录)_airflow docker-程序员宅基地

文章浏览阅读5.7k次。Airflow什么是 Airflow?Airflow 的架构Airflow 解决哪些问题一、docker-compose 安装airflow(postgres)1、创建启动文件airflow-docker-compose.yml.1.1、添加挂载卷,需要修改airflow-docker-compose.yml的位置2、创建本地配置文件airflow.cfg2.1、如果想修改WEB URL地址,需要修改airflow.cfg中以下两个地方3、之后up -d直接启动即可web访问地址:二、存储数据库更换post_airflow docker

计算机毕业设计springboot高校教务管理系统532k79【附源码+数据库+部署+LW】-程序员宅基地

文章浏览阅读28次。选题背景:随着社会的发展和教育的普及,高校教务管理系统在现代高等教育中扮演着至关重要的角色。传统的手工管理方式已经无法满足高校日益增长的规模和复杂的管理需求。因此,开发一套高效、智能的教务管理系统成为了当今高校管理的迫切需求。选题意义:高校教务管理系统的开发具有重要的意义和价值。首先,它可以提高高校教务管理的效率和准确性。通过自动化处理学生选课、排课、考试安排等繁琐的事务,大大减轻了教务人员的工作负担,提高了工作效率。同时,系统可以实时更新学生信息和课程信息,减少了数据错误和冗余,保证了管理的准确性

javaint接收float_Java Integer转换double,float,int,long,string-程序员宅基地

文章浏览阅读132次。首页>基础教程>常用类>常用 Integer类Java Integer转换double,float,int,long,stringjava中Integer类可以很方便的转换成double,float,int,long,string等类型,都有固定的方法进行转换。方法double doubleValue() //以 double 类型返回该 Integer 的值。flo..._java integet接收float类型的参数

zabbix其他常用自定义监控项_proc.cpu.util-程序员宅基地

文章浏览阅读1w次。redis相关的自定义项vim /usr/local/zabbix/etc/zabbix_agentd.conf.d/redis.confUserParameter=Redis.Status,/usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379 ping |grep -c PONGUserParameter=Redis_conn[*],/usr/local/redis/bin/redis-cli -h $1 -p $2 info | grep -w "_proc.cpu.util

RocketMQ关于 Broker 启动失败解决方案_option usebiasedlocking was deprecated-程序员宅基地

文章浏览阅读7k次。 如要在 CentOS 系统上安装 RocketMQ 服务,则可参考文章 :RocketMQ 安装详细说明 ============================================== 以下内容是 我自己在观看上面分享的文章,RocketMQ启动操作中,Broker启动失败,"RocketMQ 安装详细说明" 文章中讲的将 runserver.sh 的JVM内存..._option usebiasedlocking was deprecated