[Server Hacks] New Ubuntu 14.04 Server Checklist

file

What have you recently learned?

About server from DO(Digital Ocean) community. Link

  • Initial Server Setup with Ubuntu 14.04
  • Additional Recommended Steps for New Ubuntu 14.04 Servers
  • Install LEMP(Linux\Nginx\Mysql\PHP) stack on Ubuntu 14.04

Initial Server Setup with Ubuntu 14.04

1 root 用户登录

ssh root@SERVER_IP_ADDRESS

2 创建用户

adduser jobs

3 给新用户sudo权限

gpasswd -a jobs sudo

4 添加 Public key 到服务器

copy your ssh public key from your local machine

su - jobs
mkdir .ssh
chmod 700 .ssh
nano .ssh/authorized_keys   # paste your key
chmod 600 .ssh/authorized_keys

5 配置ssh

nano /etc/ssh/sshd_config
  • change ssh port(optinal)
  • restrict root login
service ssh restart

Additional Recommended Steps for New Ubuntu 14.04 Servers

1 Configuring a Basic Firewall

Ubuntu ships with a tool called ufw that can be used to configure your firewall policies.

设置 ssh 端口规则

如果没有修改 ssh 端口:

sudo ufw allow ssh

或者你修改过默认的 ssh 端口

sudo ufw allow 2280/tcp

如果计划运行一个 web server

sudo ufw allow 80/tcp

如果想要运行一个有 SSL/TLS 支持的 web server

sudo ufw allow 443/tcp

SMTP email 支持

sudo ufw allow 25/tcp

查看一下已经开启了那些 firefall 例外

sudo ufw show added

如果一切 ok,启用一下 firewall

sudo ufw enable

2 Configure Timezones and Network Time Protocol Synchronization

这两块设置容易理解,支持动手设置

Configure Timezones

sudo dpkg-reconfigure tzdata

Configure NTP Synchronization

sudo apt-get update
sudo apt-get install ntp

3 Create a swap file

Swap 的概念就不用赘述了吧;

使用 fallocate 工具创建一个 4G 的 swap file

sudo fallocate -l 4G /swapfile

修改一下权限

sudo chmod 600 /swapfile

通知一下系统加载 swap 文件(本次会话有效)

sudo swapon /swapfile

让系统启动时自动挂载 swap file

sudo sh -c 'echo "/swapfile none swap sw 0 0" >> /etc/fstab'

Install LEMP(Linux\Nginx\Mysql\PHP) stack on Ubuntu 14.04

1 更新系统 apt 包,安装 nginx

sudo apt-get update
sudo apt-get install nginx

测试 web 服务器

获取取本机 ip

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

或者

curl http://icanhazip.com

直接访问 ip 测试吧,有问题请自行解决;

2 mysql 数据库服务

安装

sudo apt-get install mysql-server

初始化操作

sudo mysql_install_db

运行安全脚本

sudo mysql_secure_installation

3 PHP 安装

安装 nginx 和 mysql 支持模块

sudo apt-get install php5-fpm php5-mysql

安全配置,将 “cgi.fix_pathinfo=1;” 行前的分号去掉,并将默认值1修改为0

sudo nano /etc/php5/fpm/php.ini
cgi.fix_pathinfo=0

重启php-fpm服务

sudo service php5-fpm restart

4 部署测试站点

修改 /etc/nginx/sites-avaliable/default 文件

sudo nano /etc/nginx/sites-avaliable/default

示例配置

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    server_name server_domain_name_or_IP;

    location / {
        try_files $uri $uri/ =404;
    }

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

重启服务器

sudo service nginx restart

创建一个 php 文件测试配置

sudo nano /usr/share/nginx/html/info.php

php 测试代码

<?php
phpinfo();
?>

在浏览器中访问

http://server_domain_name_or_IP/info.php

测试配置成功后,别忘了删掉测试文件

sudo rm /usr/share/nginx/html/info.php
Remote. Open. Engineer.
本帖已被设为精华帖!
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
讨论数量: 4
Summer

:+1: 酷酷的配图, Markdown 里面太多 H1 了, 看起来排版有点怪怪的.

一个页面只有一个 H1 标签就够了, 文章里面最大的字体应该是 H2 .

9年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!