【linode vps】搭建centos 7

【linode vps】搭建centos 7

之前每件事都差不多,直到现在才发现差很多。

现在才发现理清一件事的原委是多么快乐的一件事,我们共同勉励。

懒得扯淡,直接正题

关于注册 linode 账户和如何选择 vps,相关教程已经有很多了,在下讲解一下如何搭建 LAMP 和 node 的 web server。

注意,本文针对centos 7进行讲解

Get Start

参考 官网 Get Start

# 连接服务器
$ ssh root@xx.xx.xx.xx
# 输入密码
# Install Software Updates
$ yum update
# Setting the Hostname
$ hostnamectl set-hostname example_hostname
# 查看刚才设置的 hostname
$ hostname
# Set the TimezonePermalink
$ timedatectl set-timezone 'Asia/Hong_Kong'
# 查看当前系统时间
$ date

Secure Your Server

参考Secure Your Server

# 添加用户
$ useradd example_user && passwd example_user
# 设置权限:增加新用户到 wheel 用户组,以获得执行 sudo 的权限
$ usermod -aG wheel example_user
# 使用 SSH 密钥认证
# 本地生成ssh 密钥
# server 创建目录
$ mkdir .ssh
$ 客户端公钥拷贝到server
$ scp ~/.ssh/id_rsa.pub example_user@123.456.78.90:.ssh/authorized_keys
# 修改 .ssh 的所有者(注意:所有者不要是 root 帐号)和执行权限(仅所有者可读写)
$ chown -R example_user:example_user .ssh
$ chmod 700 .ssh
$ chmod 600 .ssh/authorized_keys
# 现在试一下无密码ssh吧

搭建shadowsocks

参考官网shadowsocks搭建

按照官网搭建一直有坑,没有解决,本文参考Shadowsocks Server搭建。

服务器端配置

安装应用

sudo yum install m2crypto python-setuptools -y
sudo easy_install pip
sudo pip install shadowsocks

vi /etc/shadowsocks.json

{
"server":"your_server_ip",
"server_port":8388,
"local_port":1080,
"password":"your_passwd",
"timeout":600,
"method":"aes-256-cfb"
}

开启 & 停止 & 重启 服务

# 开启
sudo ssserver -c /etc/shadowsocks.json -d start
# 停止
sudo ssserver -d stop
# 重启
sudo ssserver -c /etc/shadowsocks.json -d restart

未解决问题

  • 系统启动自动运行
# vi /etc/rc.local
# 以下为添加内容
/usr/bin/ssserver -c /etc/shadowsocks.json -d start

# vi /usr/lib/systemd/system/rc-local.service
# 以下为添加内容
[Install]
WantedBy=multi-user.target

# 启动并查看rc.local服务运行状态
$ systemctl start rc-local
$ systemctl status rc-local
#设置开机自启动
$ systemctl enable rc-local

客户端配置

参考官网shadowsocks搭建,此处提醒一下shadowsocksX添加服务器的主机名不能是ip。

LAMP

# 1. apache
$ sudo yum install httpd
# Edit httpd.conf
$ 备份/etc/httpd/conf/httpd.conf
# vi /etc/httpd/conf/httpd.conf
# 追加内容如下
KeepAlive Off


<IfModule prefork.c>
    StartServers        4
    MinSpareServers     20
    MaxSpareServers     40
    MaxClients          200
    MaxRequestsPerChild 4500
</IfModule>

# Configure Name-based Virtual HostsPermalink
# vi /etc/httpd/conf.d/vhost.conf
# 文件内容如下
NameVirtualHost *:80

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/html/example.com/public_html/
    ErrorLog /var/www/html/example.com/logs/error.log
    CustomLog /var/www/html/example.com/logs/access.log combined
</VirtualHost>

# 创建web目录
$ sudo mkdir -p /var/www/html/example.com/{public_html,logs}
# Enable Apache to start at boot, and restart the service for the above changes to take place
$ sudo systemctl enable httpd.service
$ sudo systemctl restart httpd.service


2. mysql
$ sudo yum install mariadb-server
$ sudo systemctl enable mariadb.service
$ sudo systemctl start mariadb.service
$ mysql_secure_installation
# 连接数据库
$ mysql -u root -p


3. php
$ sudo yum install php php-pear
$ sudo yum install php-mysql
# vi /etc/php.ini
# 文件内容
error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR
error_log = /var/log/php/error.log
max_input_time = 30
$ sudo mkdir /var/log/php
$ sudo chown apache /var/log/php
$ sudo systemctl reload httpd

在/var/www/html/example.com/public_html 下面新建一个index.html,通过域名和ip就可以访问了

域名解析

在线在阿里万网上买的域名,直接添加一个A记录解析到我们的linode vps的ip上就好了

https

本文https使用let's encrtypt

# Installing dependent modules
$ sudo yum install epel-release mod_ssl
# Downloading the Let’s Encrypt client
$ sudo yum install python-certbot-apache
# Setting up the SSL certificate
$ certbot --apache -d example.com -d www.example.com
# Setting up auto renewal of the certificate
$ sudo certbot renew
# 定时任务更新
$ crontab -e
# 脚本内容
0 0 * * 1 /usr/bin/certbot renew >> /var/log/sslrenew.log

samba

$ yum install samba samba-client samba-common -y
# NMB对客户端提供NetBIOS服务
$ systemctl start smb nmb 
$ systemctl enable smb nmb     
$ systemctl status smb nmb

rz/sz

$ sudo yum install lrzsz

引用

LAMP on CentOS 7
Get Start
官网shadowsocks搭建
let's encrypt
[]()

问题

  • Q: ssh 登陆服务器后,警告如下:

    -bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8): No such file or directory

    A: vi /etc/environment

    LANG=en_US.utf-8
    LC_ALL=en_US.utf-8

todo

  • shadowsocks 不能开机自动启动
  • samba 一直配置有问题

防火墙

CentOS 7.0默认使用的是firewall作为防火墙,使用iptables必须重新设置一下

# 安装
$ yum install firewalld firewall-config
# 查看防火墙状态
$ systemctl status firewalld
# 启动
$ systemctl start firewalld
# 停止
$ systemctl stop firewalld
# 开机禁用
$ systemctl disable firewalld
# 更新防火墙规则
$ firewall-cmd --reload
# 查看版本
$ firewall-cmd --version
# 查看帮助
$ firewall-cmd --help
# 设置 iptables service
yum -y install iptables-services
# 如果要修改防火墙配置,如增加防火墙端口3306
# vi /etc/sysconfig/iptables
增加规则
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
# 重启防火墙使配置生效
$ systemctl restart iptables
# 设置防火墙开机启动
$ systemctl enable iptables

selinux

Redhat使用了SELinux来增强安全,关闭的办法为:
1. 永久有效
修改 /etc/selinux/config 文件中的 SELINUX="" 为 disabled ,然后重启。
2. 即时生效
$ setenforce 0

selinux状态可以通过以下命令查看:
$ sestatus

相关推荐