腾讯云Ubuntu服务器上搭建Apache2+MySQL+Python

来源:互联网 发布:手机淘宝增加收货地址 编辑:程序博客网 时间:2024/06/05 03:53

去年底闲来无事搞到了腾讯云的学生优惠,想着搞个服务器玩玩,花了不少时间搭建环境,在此简单记录一下以便不时之需


首先,配置域名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


检验:

sudo service mysql restart

3.安装mod_python

sudo apt-get install libapache2-mod-Python

配置:
1. 在 /etc/apache2/mods-available/ 下新建一个文件python.conf,输入如下内容
<Directory /var/www>AddHandler mod_python .pyPythonHandler testPythonDebug 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!,说明运行成功





0 0
原创粉丝点击