騰訊雲Ubuntu伺服器上搭建Apache2+MySQL+Python
阿新 • • 發佈:2019-01-09
去年底閒來無事搞到了騰訊雲的學生優惠,想著搞個伺服器玩玩,花了不少時間搭建環境,在此簡單記錄一下以便不時之需
首先,配置域名DNS,給域名新增一個解析,指向自己伺服器的公網IP……
1. 安裝Apache2
sudo apt-get install apache2
配置:
1. 在 /etc/apache2/ 下,修改apache2.conf,新增如下兩行:
ServerName localhost:80
DirectoryIndex index.html index.htm index.php
第二行作用是設定目錄預設頁面
2. 在 /etc/apache2/sites-available/ 下,修改000-default.conf:
找到<VirtualHost *:80>,將DocumentRoot改為 /var/www
這樣網站的預設路徑就是在www資料夾下了
檢驗:
1. sudo service apache2 restart
2. 在瀏覽器中輸入伺服器的公網IP,如果彈出“It Works!”的頁面,說明成功
可能出現的問題:Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
解決:在apache2.conf中修改ServerName localhost:80
2. 安裝MySQL
sudo apt-get install mysql-server
然後就是設定root的密碼,這沒啥難度
配置:
修改 /etc/mysql/my.cnf,在[mysqld]中加入character-set-server=utf8
檢驗:
3.安裝mod_python
sudo apt-get install libapache2-mod-Python 配置: 1. 在 /etc/apache2/mods-available/ 下新建一個檔案python.conf,輸入如下內容<Directory /var/www>
AddHandler mod_python .py
PythonHandler test
PythonDebug On
</Directory>
其中PythonHandler就是你要處理的.py檔案的檔名
2. 將 /etc/apache2/mods-available/ 下的python.load和python.conf加入到啟用列表
sudo ln -s /etc/apache2/mods-available/python.load /etc/apache2/mods-enabled/
sudo ln -s /etc/apache2/mods-available/python.conf /etc/apache2/mods-enabled/
檢驗:
在 /var/www 下新建一個test.py,輸入如下內容:
from mod_python import apache
def handler(req):
req.content_type="text/plain"
req.write("hello!")
return apache.OK
在瀏覽器輸入公網IP/test.py,如果顯示hello!,說明執行成功