美文网首页
ubuntu 安装nginx

ubuntu 安装nginx

作者: 一介书生独醉江湖 | 来源:发表于2023-03-20 18:03 被阅读0次

一、卸载nginx

# 如果服务已启动,需要先停掉服务
sudo service nginx stop

# 删除nginx,–purge包括配置文件
sudo apt-get --purge remove nginx

# 自动移除全部不使用的软件包
sudo apt-get autoremove

# 列出与nginx相关的软件 并删除显示的软件
dpkg --get-selections|grep nginx

sudo apt-get --purge remove nginx
sudo apt-get --purge remove nginx-common
sudo apt-get --purge remove nginx-core

# 再次执行
dpkg --get-selections|grep nginx

# 不在显示nginx
which nginx

二、安装nginx

# 安装nginx之前,安装依赖
sudo apt update

sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev zlib1g zlib1g-dev ruby

# nginx下载地址:
http://nginx.org/en/download.html

# 我这里下载稳定版:http://nginx.org/download/nginx-1.22.1.tar.gz

# 拷贝下载的nginx-1.22.1目录到服务器
scp -r nginx-1.22.1 root@*.*.*.*:/usr/local/nginx

# 进入目录
cd nginx-1.22.1/

# http模式配置,--prefix指定安装目录
./configure --prefix=/usr/local/nginx

# 或者
# 带https模块配置 (我使用的这个)
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

#编译
make

#安装
make install

# 验证安装版本
/usr/local/nginx/sbin/nginx -v

三、配置证书(不配域名可忽略)

# 默认配置文件  /usr/local/nginx/conf/nginx.conf

# 创建cert目录,并拷贝证书到该目录下;

cd /usr/local/nginx/conf
mkdir cert

# 这里的证书(9xxx_xxx.xxx.com.*)是在阿里云申请并下载的(服务器类型Nginx)
scp -r 9xxx_xxx.xxx.com.* root@*.*.*.*:/usr/local/nginx/conf/cert

# 编辑配置文件
vi /usr/local/nginx/conf/nginx.conf

# 放开HTTPS server部分,配置证书;

    # HTTPS server

    server {
        listen       443 ssl;
        server_name  bjzx.deepnotary.com;

        ssl_certificate      /usr/local/nginx/conf/cert/9xxx_xxx.xxx.com.pem;
        ssl_certificate_key  /usr/local/nginx/conf/cert/9xxx_xxx.xxx.com.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

# 启动
sudo /usr/local/nginx/sbin/nginx

# 查看启动进程
ps -ef|grep nginx

root     137511      1  0 17:49 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody   137512 137511  0 17:49 ?        00:00:00 nginx: worker process      
root     137514 121909  0 17:49 pts/4    00:00:00 grep --color=auto nginx

# 关闭
kill -9 137511

# 重新载入配置文件
cd /usr/local/nginx/sbin  
./nginx -s reload 

相关文章

网友评论

      本文标题:ubuntu 安装nginx

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