centos7 搭建nginx+python+php 环境

12 篇文章 1 订阅
4 篇文章 0 订阅

阿里云服务器:centos7.8 + nginx 1.21.5 + python 3.9 + php7.4

之前服务器使用的是Apache,现在想PHP 和Python 共存,又因为python 方面都是使用flask 框架,flask 中自带的服务器比较差,所以最后决定尝试使用nginx。

虽然是用nginx ,其实它的主要作用是代理的,运行flask 的核心是uwsgi,流程大概是这样:

            

uwsgi 其实也是一个服务器,我只是将它简化了这样显示,实际上不要nginx ,用户直接访问uwsgi 的端口也是可以的,只不过有了nginx,在负载均衡等其他方面会比较好!

PHP 也大概是这样,需要一个php-fpm的核心,通过对nginx的操作来执行工作

因为本机已经安装了python3.9了,所以不贴安装教程了,直接 贴重点

-------------------安装PHP74-------------------
wget https://www.php.net/distributions/php-7.4.0.tar.gz

yum install -y gcc gcc-c++ make cmake bison autoconf wget lrzsz libtool libtool-ltdl-devel freetype-devel libjpeg.x86_64 libjpeg-devel libpng-devel gd-devel python-devel  patch  sudo openssl* openssl openssl-devel ncurses-devel bzip* bzip2 unzip zlib-devel libevent* libxml* libxml2-devel libcurl* curl-devel readline-devel sqlite-devel libsodium-devel https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/o/oniguruma-5.9.5-3.el7.x86_64.rpm https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/o/oniguruma-devel-5.9.5-3.el7.x86_64.rpm


解压、安装、编译
tar -zxvf php-7.4.0.tar.gz
cd php-7.4.0
./configure --prefix=/opt/php74 --enable-fpm
make && make install

复制配置文件
cp /root/php-7.4.0/php.ini-production /opt/php/etc/php.ini
cp /opt/php74/etc/php-fpm.conf.default /opt/php74/etc/php-fpm.conf
cp /opt/php74/etc/php-fpm.d/www.conf.default /oopt/php74/etc/php-fpm.d/www.conf

配置PHP 到profile 中
vim /etc/profile
export PYTHON_HOME=/opt/python3.9/bin
export PHP_HOME=/opt/php74/bin
export NGINX_HOME=/opt/nginx-1.21.5/sbin
export PATH=${PYTHON_HOME}:${PHP_HOME}:${NGINX_HOME}:$PATH
source /etc/profile

测试
php -v

安装在/opt/php74/ 里面,并将php命令写在系统环境中

-------------------安装nginx---------------------
https://nginx.org/en/download.html
查找要下载的版本

下载、解压、安装、编译
wget http://nginx.org/download/nginx-1.21.5.tar.gz
tar -zxvf nginx-1.21.5.tar.gz
cd nginx-1.21.5
./configure --prefix=/opt/nginx-1.21.5
make && make install


配置nginx 到profile 中
vim /etc/profile
export PYTHON_HOME=/opt/python3.9/bin
export PHP_HOME=/opt/php74/bin
export NGINX_HOME=/opt/nginx-1.21.5/sbin
export PATH=${PYTHON_HOME}:${PHP_HOME}:${NGINX_HOME}:$PATH
source /etc/profile

测试
nginx -h
nginx

打开localhost,显示nginx首页

安装nginx,也讲nginx 命令写在系统环境中

-----------------nginx配置PHP------------------------
打开nginx 的配置文件
vim /opt/nginx-1.21.5/conf/nginx.conf 



#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /var/www/html;
            index  index.php index.html index.htm;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           /var/www;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /var/www/html/$fastcgi_script_name;
            include        fastcgi_params;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }



    server {
        listen       5000;
        server_name  localhost;

        location / {
            root   /var/www;
            include uwsgi_params;  
            uwsgi_pass 127.0.0.1:9090;
        }
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

PHP 文件放在/var/www/html 中,放入phpinfo.php,正常输出,还需要安装很多的扩展,以后有需要再开

-----------------nginx配置python------------------------
安装uwsgi
pip3 install uwsgi


在python项目根目录中新建uwsgi_config.ini,内容如下

[uwsgi]
# uwsgi 启动时所使用的地址与端口,也可以使用.sock文件的方式
socket = 127.0.0.1:9090
# 处理器数
processes = 4
# 线程数
threads = 2
#项目flask日志文件
logto = /var/www/html/hifeiji/log.log
#指向网站目录
chdir=/var/www/html/hifeiji
#python 程序内用以启动的application 变量名
callable=app
# python 启动程序文件
wsgi-file = wsgi.py


启动虚拟环境中的uwsgi
/var/www/html/hifeiji/venv/bin/uwsgi /var/www/html/hifeiji/uwsgi_config.ini -production

nginx -s reload

重启后浏览器输入  ip:5000 就可以显示

由nginx conf 的配置可以知道,php-fpm在安装后,需要使用端口,端口我用了9000,uwsgi服务器也是需要使用端口,这里设置了9090,然后在nginx中,nginx使用了80端口在代理了9000,使用了5000代理了9090

服务器重启后,需要重新启动 nginx ,php-fpm,pyhton

nginx:/opt/nginx-1.21.5/sbin/   (已经写入profile 中,启动命令是  nginx)

php-fpm  :/opt/php74/sbin/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值