Yi's Blog

Ubuntu 常用安装与设置

2016-10-20

今天买了一台阿里云ECS,系统选择的是Ubuntu14.04,从头到尾的搭建过程就在此记录一下。

修改主机名

阿里云ECS默认的主机名是一串随机字符,看起来很不爽.
执行以下命令

1
hostname 新的主机名

然后修改/etc/hosts文件:

1
2
3
4
5
6
7
8
9
127.0.0.1 localhost
127.0.1.1 localhost.localdomain localhost

# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
#10.27.25.168 iZwz98n4ze1ku85f9dfetfZ (将此行替换为下面一行)
10.27.25.168 aliyun

创建新用户

1
2
useradd -r -m -s /bin/bash 用户名
passwd 用户名

/etc/sudoers中添加:

1
用户名 ALL=(ALL:ALL) ALL

整个文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults env_reset
Defaults mail_badpass
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root ALL=(ALL:ALL) ALL
用户名 ALL=(ALL:ALL) ALL

# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL

# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL

# See sudoers(5) for more information on "#include" directives:

#includedir /etc/sudoers.d

安装virtualenv

安装pip

安装virtualenv之前需要先安装pip工具,用它来安装virtualenv。

切换到非root用户,执行以下命令:

1
2
su 用户名
sudo apt-get install python-pip

查看pip的安装版本:

1
pip --version

我安装的是 pip 1.5.4 from /usr/lib/python2.7/dist-packages (python 2.7)

设置DNS解析

使用pip安装virtualenv时总是报错 Cannot fetch index base URL https://pypi.python.org/simple/

使用wget也总会提示域名无法解析 wget: unable to resolve host address,所以应该是dns设置有问题。

修改/etc/resolv.conf文件:

1
2
3
4
5
6
7
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
options timeout:1 attempts:1 rotate
#nameserver 10.143.22.118
#nameserver 10.143.22.116
nameserver 119.29.29.29
nameserver 182.254.116.116

这里我使用了DNSPod的域名解析服务。

设置pip软件源

使用pip安装时可能会出现连接超时等问题,所以修改设置换用豆瓣提供的软件源。

修改 ~/.pip/pip.conf文件:

1
2
3
4
5
[global]
timeout = 6000
#index-url = http://pypi.v2ex.com/simple
index-url = http://pypi.douban.com/simple
trusted-host = pypi.douban.com

使用pip安装virtualenv

1
pip install virtualenv

如果有报错可以尝试加上以下参数执行:

1
2
sudo -E pip install virtualenv --allow-external virtualenv --allow-unverified virtualenv

安装MySQL

为了学习简单,我没有安装最新的MySQL5.7,而是安装文档资料更全的MySQL5.5。

1
2
sudo apt-get update
sudo apt-get install mysql-server mysql-client

安装过程中会提示创建MySQL的root密码。

安装完成后可以使用命令sudo service mysql restart来重启MySQL服务,以判断是否安装成功。

安装mysql-python

1
pip install mysql-python

遇到报错:

1
2
3
4
5
6
7
8
9
10
11
Complete output from command python setup.py egg_info:
sh: 1: mysql_config: not found
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-build-4xr38J/mysql-python/setup.py", line 17, in <module>
metadata, options = get_config()
File "/tmp/pip-build-4xr38J/mysql-python/setup_posix.py", line 43, in get_config
libs = mysql_config("libs_r")
File "/tmp/pip-build-4xr38J/mysql-python/setup_posix.py", line 25, in mysql_config
raise EnvironmentError("%s not found" % (mysql_config.path,))
EnvironmentError: mysql_config not found

需要安装 libmysqlclient-dev :

1
sudo apt-get install libmysqlclient-dev

继续遇到报错:

1
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

需要安装 python-dev :

1
sudo apt-get install python-dev

解决以上问题后 mysql-python 顺利安装。

安装unzip

下载一些.zip格式的压缩包时需要使用unzip解压。
···
sudo apt-get install unzip
···

Tags: Ubuntu