CentOS7 で LAMP 環境を構築 (centos+httpd+mariadb+python)

1. 環境の準備

# systemctl stop firewalld
# systemctl disable firewalld
# sed -i '/^SELINUX=.*/c SELINUX=disabled' /etc/selinux/config
# setenforce 0

2. apache および mod_wsgi モジュールをインストールして構成する

# yum -y install httpd
# yum -y install mod_wsgi
# vim /etc/httpd/conf/httpd.conf
LoadModule wsgi_module modules/mod_wsgi.so      //模块装载的路径
WSGIScriptAlias /test /var/www/test.wsgi        //端口映射出var/www/test.wsgi的内容

3.mariadb をインストールする

# yum -y install mariadb-server
# systemctl start mariadb
# mysql_secure_instal lation

データベース構成

# mysql -u root -p
> create database master1;
> use master1;
> create table master1.dump_to_slave(id int);
> insert into master1.dump_to_slave values(10);
> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '000000';
> flush privileges;

4. MySQLdb モジュールをインストールします (Python に接続します)。

# yum -y install MySQL-python

5.スクリプトファイルの作成

# vim /var/www/test.wsgi

import MySQLdb
db=MySQLdb.connect("192.168.10.20","root","000000","master1")
cursor=db.cursor()
cursor.execute("select id from dump_to_slave limit 1")
data=cursor.fetchone()
  #以上代码目的是在Mysql数据库中取数据


def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World! %s' % data
    response_headers = [('Content-type', 'text/plain'),('Content-Length', str(len(output)))]
    start_response(status, response_headers)
    return [output]
   #以上代码按照WSGI接口规范定义函数,并将数据库数据返回页面

6.サービスを再起動します

# systemctl restart mariadb
# systemctl restart httpd

7. アクセス

http://192.168.10.20/test

おすすめ

転載: blog.csdn.net/l876460925/article/details/127409467