這部分文檔主要講述了如何在本地VMware虛擬機中配置開發環境,包括部署Ubuntu + Apache + PHP + MySQL的本地服務器環境,項目本地測試成功后,便可以同步至云服務器。同時,還要下載和安裝相應的IDE進行Java、Python和PHP的開發。
實際上我的項目均Ubuntu 18.04 LTS系統上實現的,但是在該文檔中我還是會講述Ubuntu 16.04的相關配置,因為我最初是在Ubuntu 16上進行操作的,是由于存在應用安裝的問題,最終采用了Ubuntu 18系統。需要注意的是,兩者的配置在有些細節上是有差異的。
![Ubuntu桌面][1]
## 開始:下載和安裝Ubuntu 16.04/18.04 LTS鏡像
下載Ubuntu鏡像,可以使用國內,諸如清華的源:
https://mirrors.tuna.tsinghua.edu.cn/ubuntu-releases/
在虛擬機中安裝Ubuntu系統 ( 比如VMware ) ,當然在真機中安裝也是可以的。
在虛擬機中安裝完成后,啟動Ubuntu系統,將軟件源切換至中國,我選在了阿里云的站點。
![軟件源站點的切換][2]
在 **語言支持(Language Support)**
中添加中文支持。
在命令行窗口輸入 `# ibus-setup` 進入ibus的設置界面。然后依次找到 Pinyin mode > add Chinese-Pinyin.
重啟Ubuntu系統,然后為中文添加ibus-pinyin
![ibus-pinyin][3]
在Ubuntu 16中,如果你發現用鼠標選中某一段文字進行編輯時,再次點擊這段選中的文字,文字會消失,你可以通過如下的配置來修正這個問題:
# ibus-setup
關閉選項 `Embed preedit text in application window`,然后你就能解決這個問題。
如果你使用的是Ubuntu18,則上述的配置方法不管用了,你可以直接將`ibus-pinyin`切換成`intelligent-pinyin`即可。
## 1 PHP + Apache + MySQL 環境配置
在命令行以管理員賬戶登錄
$ sudo su
安裝Apache服務器
# apt-get install apache2
安裝完成后你可以在瀏覽器中打開網址 http://localhost 或者 http://127.0.0.1。如果顯示如下的測試頁面,說明你安裝成功了。
![apache-ubuntu][4]
在安裝MySQL之前,請確認一下該平臺是夠支持相應版本的MySQL數據庫
https://www.mysql.com/support/supportedplatforms/database.html
![MySQL支持的平臺][5]
### 在Ubuntu 16中安裝MySQL
# apt-get install mysql-server mysql-client
在彈出的窗口中設置root賬戶的密碼即可。
### 在Ubuntu 18中安裝MySQL
https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-18-04
```
# mysql_secure_installation
mysql > SELECT user,authentication_string,plugin,host FROM mysql.user;
mysql > ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
mysql > FLUSH PRIVILEGES;
mysql> set global validate_password_policy=0;
mysql> SHOW VARIABLES LIKE 'validate_password%';
```
### 將MySQL設置編碼格式為utf8,在Ubuntu16/18中的設置是一樣的
Vim mysql.cnf
```
# vim /etc/mysql/conf.d/mysql.cnf
###在 [mysql]標簽下添加如下的代碼###
default-character-set=utf8
```
Vim mysqld.cnf
```
# vim /etc/mysql/mysql.conf.d/mysqld.cnf
###在 [mysqld]標簽下添加如下的代碼###
character-set-server=utf8
```
重啟MySQL,查看編碼配置是否成功:
# systemctl restart mysql
# mysql -uroot -p
mysql > show variables like '%character%';
如下圖所示,所有的編碼都是uft8,成功!
![mysql-coding][6]
安裝php和php的拓展 ( 我的項目需要 php-mysql, php-curl)
# apt-get install php libapache2-mod-php php-gd php-mysql php-curl
重啟Apache服務
# systemctl restart apache2
## 2 配置Java環境 ( oracle )
添加ppa源
```
# add-apt-repository ppa:webupd8team/java
# apt-get update
```
安裝oracle-java-installer
```
# apt-get install oracle-java8-installer
```
設置系統默認的jdk
```
# update-java-alternatives -s java-8-oracle
```
測試java的安裝是否成功
```
# java -version
# javac -version
```
## 3 Python的切換配置
如果使用的是Ubuntu16,則我們可以安裝python3,而python2是默認安裝的。
# apt-get install python3
如果使用的是Ubuntu18,則系統中默認安裝py3而沒有py2,因此我們可以先安裝py2,以備不時之需。
# apt-get install pyhton
![python-usr-bin][7]
使用Ubuntu的update-alternatives
來實現py2和py3的自由切換。
# update-alternatives --list python
如果顯示 `update-alternatives: error: no alternatives for python` ,這說明python的替代版本還不能別`update-alternatives`命令識別。我們需要使用`update-alternatives`命令來配置py2和py3
```
# update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
# update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2
```
*--install* 命令的參數詳解
--install <link> <name> <path> <priority>
列出所有可替代python版本
```
# update-alternatives --list python
/usr/bin/python2.7
/usr/bin/python3.5
```
對
python的版本進行選擇
# update-alternatives --config python
檢查python的版本
# python --version
其他一些有用的命令:移除update-alternatives
的配置
# update-alternatives --remove-all python
# ln -s /usr/bin/<python2.7|python3.5> /usr/bin/python
## 4 用于開發的工具
所有 **表1** 中的軟件都可以在**Ubuntu Software**中安裝,是不是相當方便,只需要搜索相應的軟件名即可安裝下載。
|名字 | 簡介 | 版本 |
|--------------|------------------| -------------|
|Intellij IDEA Community | Java集成開發環境 | 2018.3.4 |
|Pycharm CE | Python集成開發環境 | 2018.3.4|
|Sublime Text | 代碼、文本編輯器 | 3176|
|Chromlum | 瀏覽器 | 71.0.3578.98 |
|Workbench | MySQL可視化管理工具 | |
|Android Studio | Android的集成開發環境 | |
:-: 表1 .軟件列表
![mysql-workbench][8]
## 5 遇到的問題
因為python3的錯誤,而不能將Ubuntu16升級到Ubuntu18
```
# do-release-upgrade
Checking package manager
Can not upgrade
Your python3 install is corrupted. Please fix the '/usr/bin/python3'
symlink.
```
解決方法可以參看
https://askubuntu.com/questions/1104192/unable-to-upgrade-to-ubuntu-18-04-from-ubuntu-17-10
需要 `/usr/bin/python` 直接指向 `/usr/bin/python3.5` ,而不能采用update-alternatives配置
## 6 一些常用指令的筆記
```
//mysql
mysqldump -u -p <database> > example.sql
mysql -u -p <database> < example.sql
//ssh and sftp
ssh <ip>
sftp <ip>
get
put
cd,lcd
ls,lls
pwd,lpwd
//apt-get
apt-get install
apt-get remove
//other
do-release-upgrade
strace
//chmod, chown
chmod -R 777 <dictionary> //change file's permission
chmod 777 <file>
chown -R <user>:<user group> <folder> //change file's owner
//systemctl
systemctl restart apache2
systemctl restart mysql
systemctl restart networking //hosts file
//tar
tar -xzvf example.tar.gz //x-unpack and z-decompress, v-show info, f-file name
tar -czvf newName.tar.gz folderName //c-pack and z-compress
tar -cvf newName.tar folderName //c-pack not z-compress
//git
git clone
git rm -r --cached <folderName/Filename>
git commit -m 'Add you own description'
git add . #. can match all pattern of files
git pull origin master
git push -u origin master
```
[1]: https://blog-1252789527.cos.ap-shanghai.myqcloud.com/article/Configure%20developing%20environment%20in%20Ubuntu%2016.04%20LTS%20/ubuntu-desktop.jpg
[2]:https://blog-1252789527.cos.ap-shanghai.myqcloud.com/article/Configure%20developing%20environment%20in%20Ubuntu%2016.04%20LTS%20/source-download-site.png
[3]: https://blog-1252789527.cos.ap-shanghai.myqcloud.com/article/Configure%20developing%20environment%20in%20Ubuntu%2016.04%20LTS%20/ibus-pinyin.png
[4]: https://blog-1252789527.cos.ap-shanghai.myqcloud.com/article/Configure%20developing%20environment%20in%20Ubuntu%2016.04%20LTS%20/ubuntu-apache.png
[5]: https://blog-1252789527.cos.ap-shanghai.myqcloud.com/article/Configure%20developing%20environment%20in%20Ubuntu%2016.04%20LTS%20/supported%20platform.png
[6]: https://blog-1252789527.cos.ap-shanghai.myqcloud.com/article/Configure%20developing%20environment%20in%20Ubuntu%2016.04%20LTS%20/mysql-coding.png
[7]: https://blog-1252789527.cos.ap-shanghai.myqcloud.com/article/Configure%20developing%20environment%20in%20Ubuntu%2016.04%20LTS%20/python-usr-bin.png
[8]: https://blog-1252789527.cos.ap-shanghai.myqcloud.com/article/Configure%20developing%20environment%20in%20Ubuntu%2016.04%20LTS%20/mysql-workbench.png