云主机修改远程端口的方法
随着互联网的发展,越来越多的企业和个人开始使用云主机来部署自己的网站和应用,云主机的优点之一就是可以随时随地访问,而远程端口的设置则是保证安全访问的关键,本文将详细介绍如何修改云主机的远程端口,以确保您的数据安全。
登录云主机
您需要登录到您的云主机,这通常可以通过SSH客户端完成,在Windows系统中,您可以使用PuTTY;在macOS和Linux系统中,您可以使用终端,以下是在不同操作系统中登录云主机的简要说明:
1、Windows系统:
打开PuTTY,输入云主机的IP地址、用户名和密码,然后点击“Open”按钮。
2、macOS和Linux系统:
打开终端,输入以下命令(将your_username
替换为您的用户名,将your_password
替换为您的密码):
“`
ssh your_username@your_host_ip
“`
然后按回车键,输入密码。
修改防火墙设置
为了允许外部设备访问您的云主机,您需要修改防火墙设置,以下是在Linux系统中修改防火墙设置的方法:
1、安装UFW(Uncomplicated Firewall):
“`
sudo apt-get install ufw
“`
2、启用UFW:
“`
sudo systemctl enable ufw
“`
3、允许特定端口的流量:
“`
sudo ufw allow <port>/tcp
sudo ufw allow <port>/udp
“`
<port>
是您要允许访问的端口号,如果您要允许访问80端口,那么命令应该是:
“`
sudo ufw allow 80/tcp
sudo ufw allow 80/udp
“`
4、检查防火墙设置:
“`
sudo ufw status
“`
修改Web服务器配置文件
大多数云主机都预装了Web服务器软件,如Apache或Nginx,您需要根据所使用的Web服务器软件修改相应的配置文件,以便将请求转发到正确的端口,以下是针对Apache和Nginx的示例:
1、Apache:
在Apache中,您需要编辑httpd.conf
文件(通常位于/etc/httpd/conf/
目录下),找到以下行:
“`
Listen 80
Listen 443 SSL http2defaults ssl-preread on;"SSLSessionCache shmcb:/var/run/apache2/apache2-ssl-cache(512000)" no-server-header on;"SSLSessionCacheTimeout 300" no-static-open "LogLevel alert rewrite:trace7" allow 127.0.0.1;"SetEnvIfNoCase User-Agent "^Mozilla/5.0" no-keepalive cache-control no-store" keepalive_timeout 65 maxkeepaliverequests 100 proxy-read-timeout 300 proxy-connect-timeout 300 proxy-send-timeout 300 proxy-read-timeout 300 send-timeout 300 errorlog /var/log/httpd/error_log loglevel warn output_buffers 16 32k output_handler sys_output gzip on"ServerName www.example.com:80 DocumentRoot "/var/www/html" ErrorLog "logs/error_log" CustomLog "logs/access_log" common" ServerAlias www.example.com:80 DocumentRoot "/var/www/html" ErrorLog "logs/error_log" CustomLog "logs/access_log" common" <Directory "/var/www/html"> AllowOverride None Require all granted</Directory>" LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" WithContext None</VirtualHost>“`
将Listen 80
更改为Listen 新端口号
,
“`
Listen 8080
Listen 443 SSL http2defaults ssl-preread on;"SSLSessionCache shmcb:/var/run/apache2/apache2-ssl-cache(512000)" no-server-header on;"SSLSessionCacheTimeout 300" no-static-open "LogLevel alert rewrite:trace7" allow 127.0.0.1;"SetEnvIfNoCase User-Agent "^Mozilla/5.0" no-keepalive cache-control no-store" keepalive_timeout 65 maxkeepaliverequests 100 proxy-read-timeout 300 proxy-connect-timeout 300 proxy-send-timeout 300 proxy-read-timeout 300 send-timeout 300 errorlog /var/log/httpd/error_log loglevel warn output_buffers 16 32k output_handler sys_output gzip on"ServerName www.example.com:80 DocumentRoot "/var/www/html" ErrorLog "logs/error_log" CustomLog "logs/access_log" common" ServerAlias www.example.com:80 DocumentRoot "/var/www/html" ErrorLog "logs/error_log" CustomLog "logs/access_log" common" <Directory "/var/www/html"> AllowOverride None Require all granted</Directory>" LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" WithContext None</VirtualHost>“`
2、Nginx:
在Nginx中,您需要编辑nginx.conf
文件(通常位于/etc/nginx/conf.d/
目录下),找到以下行:
“`
server {{ server_name_regex | default(”) ~ ‘(.[a-zA-Z]{2,})?’}} {{ listen | default(”) ‘{{ port | default(”) }}’;}} location {{ path ~* ‘^~’ | default(‘/’)}}{{ if (index of req.uri [ ‘/’] > index of req.uri [ ‘/index.html’])}} try_files $uri $uri/ =404;{{ end}}{{ if (!-e $file || index of $file ‘index.html’ != last) && file_exists($root ~ $file) || file_exists($file)}} location ~ $uri $uri {{ if ($query_string ~ ‘(?<=&).*?(?=[&]|$)’)} Strip query string for redirection{{ set $args ”;}}{{ else if ($args ~ ‘(?<=&).*?(?=[&]|$)’)}} Strip query string for redirection and preserve existing GET parameters{{ set $args $args|replace({‘(?<=&).*?(?=[&]|$)’: ”})} return redirect($scheme://$host$request_uri$args);{{ end}}{{ if ($args ~ ‘(?<=&).*?(?=[&]|$)’)}} Add query string back to URL for redirection with GET parameters{{ set $args $args|replace({‘(?<=&).*?(?=[&]|$)’: ”})} return redirect($scheme://$host$request_uri$args);{{ end}}{{ if ($uri ~ ‘.(jpg|jpeg|png|gif|ico|css|js)$’)}} try_files $uri @rewrites;{{ end}}{{ if ($uri ~ ‘.(mp4|avi|flv|mov|wmv)$’)}} try_files $videos_path$uri @rewrites;{{ end}} location @rewrites {{ try_files $uri =404;}} location ~ .php$ {{ include fastcgi_params; fastcgi_pass {{ server_name | default(‘localhost’) }; fastcgi_param REDIRECT_STATUS ~ HTTPStatus1xx ~; fastcgi_param DOCUMENT_ROOT ~ $document_root; fastcgi_param PHP_VALUE "fastcgi.input_buffering=off"; fastcgi_param PHP_VALUE "PHP_OUTPUT_HANDLER=system"; fastcgi_param PHP_VALUE "SCRIPT_FILENAME="$document_root$realpath($_SERVER
原创文章,作者:酷盾叔,如若转载,请注明出处:https://www.kdun.com/ask/134381.html
本网站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本网站。如有问题,请联系客服处理。
发表回复