jmy网站服务器连接方法


ssh连接

windows打开终端cmd

ssh root@162.241.120.154 -p 22

输入密码

网站密码

SFTP

使用 FileZilla 软件,主机:sftp://162.241.120.154 用户名:root 密码 网站密码 端口 22

点击快速连接,进去之后点击..返回上一级找到 usr 文件夹。

将修改好的website文件放到 /usr/local/nginx/html 文件夹下(直接拖动,原本的website要先删除,点击..是返回上一级)

修改配置文件(可选)

sudo nano /usr/local/nginx/conf/nginx.conf

正常不用修改,两个#的地方保持一致就行

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;# jmbremedy.com www.jmbremedy.com

        location / {
            root   html/website;  # 确保路径正确指向您的网站文件
            index  welcome.html;
        }
    }
}

重启服务

已经全局添加nginx到path

所以这里在cmd里使用

sudo /usr/local/nginx/sbin/nginx -s reload

解决报错

如果打开是空白居多,则是apache服务占用了80端口如下图 [root@162-241-120-154 init.d]# sudo lsof -i :80 后面所示,httpd是apache服务,我们需要停止apache服务 sudo systemctl stop httpd。

[root@162-241-120-154 init.d]# sudo /usr/local/nginx/sbin/nginx -s reload # 先重载nginx发现报错
nginx: [error] invalid PID number "" in "/usr/local/nginx/logs/nginx.pid"
[root@162-241-120-154 init.d]# ps aux | grep nginx
root      2086  0.0  0.0 112812   980 pts/1    S+   11:32   0:00 grep --color=auto nginx
[root@162-241-120-154 init.d]# sudo /usr/local/nginx/sbin/nginx # 启动nginx时报错already被占用
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
[root@162-241-120-154 init.d]# sudo lsof -i :80 # 检查80端口是谁在占用,下面显示被httpd即apache服务占用,需要kill或者停止,然后换成nginx服务
COMMAND   PID   USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
httpd    6691 nobody    3u  IPv4 5049319      0t0  TCP *:http (LISTEN)
httpd    6691 nobody    4u  IPv6 5049320      0t0  TCP *:http (LISTEN)
httpd    6693 nobody    3u  IPv4 5049319      0t0  TCP *:http (LISTEN)
httpd    6693 nobody    4u  IPv6 5049320      0t0  TCP *:http (LISTEN)
httpd   18171 nobody    3u  IPv4 5049319      0t0  TCP *:http (LISTEN)
httpd   18171 nobody    4u  IPv6 5049320      0t0  TCP *:http (LISTEN)
httpd   18322 nobody    3u  IPv4 5049319      0t0  TCP *:http (LISTEN)
httpd   18322 nobody    4u  IPv6 5049320      0t0  TCP *:http (LISTEN)
httpd   21719   root    3u  IPv4 5049319      0t0  TCP *:http (LISTEN)
httpd   21719   root    4u  IPv6 5049320      0t0  TCP *:http (LISTEN)
httpd   21726 nobody    3u  IPv4 5049319      0t0  TCP *:http (LISTEN)
httpd   21726 nobody    4u  IPv6 5049320      0t0  TCP *:http (LISTEN)
httpd   21727 nobody    3u  IPv4 5049319      0t0  TCP *:http (LISTEN)
httpd   21727 nobody    4u  IPv6 5049320      0t0  TCP *:http (LISTEN)
httpd   21728 nobody    3u  IPv4 5049319      0t0  TCP *:http (LISTEN)
httpd   21728 nobody    4u  IPv6 5049320      0t0  TCP *:http (LISTEN)
httpd   23917 nobody    3u  IPv4 5049319      0t0  TCP *:http (LISTEN)
httpd   23917 nobody    4u  IPv6 5049320      0t0  TCP *:http (LISTEN)
httpd   25765 nobody    3u  IPv4 5049319      0t0  TCP *:http (LISTEN)
httpd   25765 nobody    4u  IPv6 5049320      0t0  TCP *:http (LISTEN)
httpd   25810 nobody    3u  IPv4 5049319      0t0  TCP *:http (LISTEN)
httpd   25810 nobody    4u  IPv6 5049320      0t0  TCP *:http (LISTEN)
[root@162-241-120-154 init.d]# sudo systemctl stop httpd # 停止apache服务
[root@162-241-120-154 init.d]# sudo lsof -i :80 # 再次检查80端口已经没有服务
[root@162-241-120-154 init.d]# sudo /usr/local/nginx/sbin/nginx # 重新启动nginx
[root@162-241-120-154 init.d]# ps aux | grep nginx
root      2280  0.0  0.0  18880   608 ?        Ss   11:33   0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody    2281  0.0  0.0  19328  1072 ?        S    11:33   0:00 nginx: worker process
root      2306  0.0  0.0 112812   980 pts/1    S+   11:34   0:00 grep --color=auto nginx
[root@162-241-120-154 init.d]# sudo /usr/local/nginx/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@162-241-120-154 init.d]# sudo /usr/local/nginx/sbin/nginx -s reload # 上面的步骤可以不用,直接重载nginx,不报错网站即可正常使用
[root@162-241-120-154 init.d]# client_loop: send disconnect: Connection reset

C:\Users\AlexLee>ssh root@162.241.120.154 -p 22
root@162.241.120.154's password:
Last failed login: Thu May 23 11:29:12 EDT 2024 from 183.81.169.238 on ssh:notty
There were 5 failed login attempts since the last successful login.
Last login: Thu May 23 10:59:00 2024 from 49.94.165.210
[root@162-241-120-154 ~]# sudo /usr/local/nginx/sbin/nginx -s reload

SSL证书续期

如果显示连接不安全且报出红色,则是证书过期,这里为3个月一续,最近一次更新是9月4日。需要先停止nginx服务,再免费续期

[root@162-241-120-154 ~]# sudo lsof -i :80 # 检查nginx是否在运行,出现如下情况则需要kill
COMMAND   PID   USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
nginx   13285   root    6u  IPv4 73952317      0t0  TCP *:http (LISTEN)
nginx   13295 nobody    6u  IPv4 73952317      0t0  TCP *:http (LISTEN)
[root@162-241-120-154 ~]# sudo kill 13285 # 根据上面实际出现的PID进程号进行kill
[root@162-241-120-154 ~]# sudo kill 13295
[root@162-241-120-154 ~]# sudo certbot renew # 免费续期证书
[root@162-241-120-154 ~]# sudo /usr/local/nginx/sbin/nginx # 重新启动nginx
[root@162-241-120-154 ~]# sudo /usr/local/nginx/sbin/nginx -s reload # 重新加载nginx

使用 Systemd 管理服务

sudo nano /etc/systemd/system/nginx.service

加入以下内容

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
Restart=on-failure
RestartSec=5s

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable nginx
sudo systemctl start nginx
sudo nano /usr/local/nginx/conf/nginx.conf

修改

pid /run/nginx.pid;

重新启动

sudo systemctl restart nginx

查看状态

sudo systemctl status nginx

这样以后不需要再用reload,如果以后再遇到nginx自己停止的情况,则需要使用 crontab 实现定时检查与自动启动。


文章作者: Alex Lee
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Alex Lee !
评论
  目录