ContOS+Apache+Wsgi+python+Django

it2024-07-31  37

ContOS+Apache+Wsgi+python+Django

基础环境:

系统:ContOS7

Apache版本:2.4.6

 

Wsgi版本:4.7.1

python版本:python3.5

Django版本:1.11.29

 

对于本次项目基础环境搭建的心德:

         如果不太熟练的话,一定要在环境搭建之前对你的虚拟机,照一个快照,用于搞崩了恢复用,本人恢复了4次,心酸啊!

对于这次项目的搭建,需要用到选择版本的有,python解释器和Django,而对于wsgi并不需要选择版本,将python安装完之后安装的wsgi是通过pip命令安装的,我觉得python解释器会自动选择适合解释器的最新版本的wsgi;还有apache,在linux里叫httpd,也是直接用yum命令安装由于yum底层也是通过python运行的,所以也会选择适合的最新版本安装;最后不要忘了mysql,在contos7中叫做mariadb,是mysql的一个分支,性能上优于mysql,命令几乎和mysql一样,安装时也是用yum命令安装,不需要选择版本。

         错误心德:(所有的程序命令顺序必须一致,包括参照链接里的,)

                  对于本次项目遇到了的错误结点就是wsgi,之前由于太注重某一个对应的版本,涉及到卸载python3.5,并重新安装;卸载python时,一定要注意不要把contos7里自带的python2.7卸掉,否则你的yum也得卸掉,会造成很多不必要的麻烦。

安装前提:

1、关闭防火墙和SELinux(强制访问控制系统)

# 临时关闭防火墙 systemctl stop firewalld.servcie # 防止开机自起,将firewalld服务的运行级别关闭 systemctl disable firewalld.service # 关闭SELinux,需要修改配置文件,所以修改后需要重启 vim /etc/selinux/config # 将里面的 SELINUX 的值修改成 disabled

查看防火墙是否关闭:

systemctl status firewalld.service

查看SELinux是否关闭:

sestatus

 

 

2、需要安装gcc,它是虚拟机中安装软件的前提;哦!对了这两个全部都需要安装

也可用命令:

yum install gcc

 

我的是在CentOS文件里找到的安装包:home相当于windows中的我的电脑

 

1、首先安装apache:

我安装时参照的网址:https://www.cnblogs.com/qi-yuan/p/11906101.html

yum install httpd yum istall httpd-devel

如果还不好使那么就得需要继续去搜索了

推荐地址:https://blog.csdn.net/qq_41772936/article/details/79683134

启动服务:

systemctl start httpd

进入到/var/www/html下创建一个index.html文件并在里面随便输入点字

再去去虚拟机中测试一下是否安装成功;

 

2、下载与安装python

注意由于CentOS7里自带python2.7,所以要将这个版本的升级成3.5的

参考安装:https://www.cnblogs.com/z-joshua/p/5710698.html

下载地址:https://www.python.org/ftp/python/

1)首先将软件下载下来,放到虚拟机中自己熟悉的文件夹下,我放的是tmp文件夹下

 

2)安装Python3.5

(1)在安装python之前,因为linux系统下默认没有安装wget,gcc,首先安装wget(由于gcc开始就安装了可以略过):

[root@zlc tmp]# yum install wget

(2)安装依赖:

!!!在编译之前需要安装一些必须的依赖,否则当报错的时候还得重新编译 

yum install openssl-devel   -y yum install zlib-devel  -y

(3)将自己之前传到虚拟机中的python3.5.9的安装文件并解压缩(如果没有用wget命令下载:wget https://www.python.org/ftp/python/3.5.9/Python-3.5.9.tgz):

[root@zlc tmp]# tar xvf Python-3.5.9.tgz  [root@zlc tmp]# ls Python-3.5.9 Python-3.5.9.tgz  [root@zlc Python-3.5.9]# cd Python-3.5.9

(4)编译:

这里需要注意了,我之前为了安装特定版本的wsgi,由于安装python时没有用共享安装的命令(--enable-shared)导致了python重装

这里参考的网站:(我的错误就是从这里知道的)

         出错时解决官网:https://modwsgi.readthedocs.io/en/develop/user-guides/installation-issues.html

./configure --prefix=/usr/local/python3.5  --enable-shared    --enable-loadable-sqlite-extensions make make install mv /usr/bin/python /usr/bin/python_bak ln -s /usr/local/python3.5/bin/python3.5/usr/bin/python

输入python,测试是否安装成功,如果成功就会进入到python的shell界面:

我的运行到这步报了一个错误:(没有遇到就略过)

报错信息:

python3: error while loading shared libraries:libpython3.5m.so.1.0: cannot open shared object file:

 

错误原因:

这在错误原因,是centos系统默认加载/usr/lib,/lib下面库文件,python默认安装到非此类文件夹。不过可以通过添加库配置信息

解决方案:

         步骤如下:

cd /etc/ld.so.conf.d vim python3.conf

 ->编辑 添加库文件路径/opt/Python3.5/lib

 ->退出保存

 ->运行ldconfig

ldconfig

(5)此时执行phthon可以看到3.5已经运行:

[root@node6 tmp]# python Python 3.5.9 (default, Jul 27 2016,09:34:49)  [GCC4.8.5 20150623 (Red Hat 4.8.5-4)] on linux Type "help","copyright", "credits" or "license" for moreinformation. >>>

(6) 此时yum的运行会报错,如下操作可以恢复yum:

修改yum文件

cd /usr/bin ls -l yum*

会显示出“yum、yum-builddep、yum-config-manager、yum-debug-dump、yum-debug-restore、yumdownloader、yum-groups-manager”这7个yum开头的文件,这7个文件都是脚本文件,其第一行为:

#!/usr/bin/python

#!/usr/bin/python -tt

将“python”改为“python2.7”。

再将另外一个也改一下

vim /usr/libexec/urlgrabber-ext-down

修改/usr/bin/python为/usr/bin/python2.7

3)安装pip3

(1)首先下载,安装依赖setuptools

wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-19.6.tar.gz#md5=c607dd118eae682c44ed146367a17e26 tar -zxvf setuptools-19.6.tar.gz cd setuptools-19.6.tar.gz python setup.py build python setup.py install

(2)安装pip3  wget --no-check-certificate  http://yum.baseurl.org/wiki/Faq

wget --no-check-certificatehttps://pypi.python.org/packages/source/p/pip/pip-8.0.2.tar.gz#md5=3a73c4188f8dbad6a1e6f6d44d117eeb tar -zxvf pip-8.0.2.tar.gz cd pip-8.0.2 python setup.py build python setup.py install

安装完成之后我们再来查看python3.5的bin目录下都有什么东西:

[root@zlc usr]# cd /usr/local/python3.5/bin/  [root@zlc bin]# ls 2to3     easy_install      idle3    pip  pip3.5  pydoc3.5  python3.5         python3.5m         python3-config  pyvenv-3.5 2to3-3.5 easy_install-3.5  idle3.5  pip3 pydoc3  python3   python3.5-config  python3.5m-config  pyvenv

为pip3创建链接:

[root@vnode33 bin]# ln -s/usr/local/python3.5/bin/pip3 /usr/bin/pip

测试是否安装成功:

 

修改pip下载源:

临时修改(在安装命令后加上指定源选项):pip install 模块名字 -i https://mirrors.aliyun.com/pypi/simple/

国内源:

清华:https://pypi.tuna.tsinghua.edu.cn/simple

阿里云:https://mirrors.aliyun.com/pypi/simple/

中国科技大学https://pypi.mirrors.ustc.edu.cn/simple/

华中理工大学:http://pypi.hustunique.com/

山东理工大学:http://pypi.sdutlinux.org/ 

豆瓣:http://pypi.douban.com/simple/

3、Django安装(如果安装错误版本卸载命令pip uninstall django)

pip  install -i https://pypi.doubanio.com/simple/ --trusted-host pypi.doubanio.com django==1.11.29

 

4、安装mod_wsgi

方法一(这种比较麻烦):

首先查找对应的版本,对版本的依赖很高,如果安装错误的版本,项目是不好使的

两个思路:一个是上pypi查找,另一个是去官网查找

下载网址: https://pypi.org/search/?q=mod_wsgi

将下载后的文件放到虚拟机中tmp文件夹下,当然也可以放到其他文件夹下

执行解压缩命令,进入到解压后的文件里:(上面安装python也说了,python必须得是共享安装的方式,否则这种安装就会报错)

tar xvfz mod_wsgi-4.7.1.gz cd mod_wsgi ./configure --with-apxs=/usr/bin/apxs  --with-python=/usr/local/python3.5/bin/python3.5 make make install

会显示

 

Libraries have been installed in:

/usr/lib64/httpd/modules

If you ever happen to want to link againstinstalled libraries

in a given directory, LIBDIR, you musteither use libtool, and

specify the full pathname of the library,or use the `-LLIBDIR'

flag during linking and do at least one ofthe following:

- add LIBDIR to the `LD_LIBRARY_PATH'environment variable

during execution

- add LIBDIR to the `LD_RUN_PATH'environment variable

during linking

- use the `-Wl,-rpath -Wl,LIBDIR' linkerflag

- have your system administrator add LIBDIRto `/etc/ld.so.conf'

See any operating system documentationabout shared libraries for

more information, such as the ld(1) andld.so(8) manual pages.

这些是告诉你安装成功后应如何继续操作

其最简单的办法就是在 /etc/ld.so.conf.d 下新建一个文件 local.conf,里边写上 /usr/local/lib。然后以 root 权限执行 ldconfig。这样你的系统才会找得到安装到 /usr/local/lib 下的库文件。

方法二(我用的方法):

pip install mod_wsgi

就是这么简单!

5、配置wsgi和apache(httpd)

参考网址:https://www.jianshu.com/p/d209897663c9

参考最后面的第7步就可以了,当然参考归参考,完全照抄那就不可能对

我的对于它的有所改动,改动位置:配置httpd.conf文件    (其余的可以按照他的做)

 

改动原因:

因为那个网址里说的配置有些和配置文件里的配置重复了,会导致重启httpd失败;还有就是路径必须用双引号括起来;最重要的就是将每个代码后面的注释删掉,因为这样也会导致重启服务报错

6、数据库的安装在另一篇博文里,配置好这些,还要记得配置setting文件里的ALLOWED_HOSTS、DEBUG、DATABASES


最新回复(0)