简介:本文为新手提供云服务器搭建的完整流程,涵盖云服务商选择、操作系统安装、基础环境配置、安全加固及项目部署等关键步骤,帮助零基础用户快速掌握云服务器管理技能。
当前主流云服务商包括阿里云、腾讯云、华为云等,均提供弹性计算服务。建议优先选择提供免费试用(如腾讯云90天试用)或学生优惠的厂商,降低初期成本。需重点关注:
以腾讯云为例演示操作流程:
- 地域:选择就近区域- 镜像:推荐CentOS 8或Ubuntu 20.04 LTS- 实例类型:s5.small(1核2G)- 系统盘:默认50GB高性能云盘- 购买时长:1个月(适合新手体验)
123.123.123.123)使用SSH工具(如Xshell、Termius)连接服务器:
ssh root@123.123.123.123# 首次连接需验证指纹,输入yes后输入密码
连接成功后执行基础初始化:
# 修改root密码(可选)passwd# 创建普通用户(安全最佳实践)adduser deploypasswd deployusermod -aG wheel deploy # CentOS系统# 或 usermod -aG sudo deploy # Ubuntu系统# 更新系统包yum update -y # CentOS# 或 apt update && apt upgrade -y # Ubuntu
配置开发环境(以LNMP栈为例):
# 安装Nginxyum install epel-release -yyum install nginx -ysystemctl start nginxsystemctl enable nginx# 安装MySQL 8.0wget https://dev.mysql.com/get/mysql80-community-release-el8-3.noarch.rpmrpm -ivh mysql80-community-release-el8-3.noarch.rpmyum install mysql-community-server -ysystemctl start mysqldmysql_secure_installation # 执行安全配置# 安装PHP 8.0yum install php php-fpm php-mysqlnd php-zip php-gd php-mbstring -ysystemctl start php-fpmsystemctl enable php-fpm
使用firewalld(CentOS)或ufw(Ubuntu)管理端口:
# CentOS配置firewall-cmd --permanent --add-service=httpfirewall-cmd --permanent --add-service=httpsfirewall-cmd --permanent --remove-service=ssh # 建议限制SSH访问firewall-cmd --reload# Ubuntu配置ufw allow 80/tcpufw allow 443/tcpufw deny 22/tcp # 需配合安全组使用ufw enable
禁用密码登录,改用SSH密钥:
ssh-keygen -t ed25519 -C "deploy@yourdomain.com"
ssh-copy-id -i ~/.ssh/id_ed25519.pub deploy@123.123.123.123
/etc/ssh/sshd_config):
PasswordAuthentication noChallengeResponseAuthentication noPubkeyAuthentication yes
systemctl restart sshd
将HTML文件上传至/usr/share/nginx/html/:
# 使用scp命令上传scp -r ./website/* deploy@123.123.123.123:/usr/share/nginx/html/# 配置Nginx虚拟主机vi /etc/nginx/conf.d/mysite.conf
配置示例:
server {listen 80;server_name example.com;root /usr/share/nginx/html;index index.html;location / {try_files $uri $uri/ =404;}}
重启Nginx生效:
nginx -t # 测试配置systemctl restart nginx
wget https://wordpress.org/latest.tar.gztar -xzf latest.tar.gzmv wordpress /var/www/chown -R nginx:nginx /var/www/wordpress # CentOS# 或 chown -R www-data:www-data /var/www/wordpress # Ubuntu
CREATE DATABASE wordpress;CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'StrongPassword123!';GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';FLUSH PRIVILEGES;
配置Nginx:
server {listen 80;server_name blog.example.com;root /var/www/wordpress;index index.php;location / {try_files $uri $uri/ /index.php?$args;}location ~ \.php$ {fastcgi_pass 127.0.0.1:9000;include fastcgi_params;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;}}
安装htop和nmon进行资源监控:
yum install htop nmon -y # CentOS# 或 apt install htop nmon -y # Ubuntu
配置Nginx日志轮转:
/etc/logrotate.d/nginx:
/var/log/nginx/*.log {dailymissingokrotate 14compressdelaycompressnotifemptycreate 640 nginx admsharedscriptspostrotate[ -s /run/nginx.pid ] && kill -USR1 `cat /run/nginx.pid`endscript}
logrotate -f /etc/logrotate.d/nginx
netstat -tulnp检查服务是否监听正确端口root路径是否正确bind-address = 0.0.0.0)mysqldump + 对象存储)通过以上步骤,新手可在3小时内完成从云服务器创建到项目部署的全流程。建议首次操作时使用测试环境,待熟练后再应用于生产环境。定期关注云服务商的安全公告,及时修补系统漏洞,保障服务器稳定运行。