首页 > 编程语言 >编译部署LNMP-php8.1.18版本

编译部署LNMP-php8.1.18版本

时间:2023-04-25 12:12:18浏览次数:46  
标签:-- 18 LNMP php8.1 nginx usr devel php local

由于mysql编译会非常耗费资源,故咱们这里不做介绍,只介绍nginx的编译、php的编译以及nginx和php的联动,至于mysql如何接入,需要看具体业务,在LNMP中mysql是相对独立的,不需要特别的配置

编译安装nginx

新建www用户

groupadd www
useradd -s /sbin/nologin -g www www

安装必要依赖

yum -y install zlib gcc gcc-c++ libgcc zlib-devel pcre pcre-devel openssl openssl-devel libxslt-devel gd gd-devel GeoIP GeoIP-devel libxml2 libxml2-dev

下载源码包

wget --no-check-certificate https://nginx.org/download/nginx-1.22.1.tar.gz
tar xf nginx-1.22.1.tar.gz

编译安装

cd nginx-1.22.1/
./configure \
--user=www \
--group=www \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/usr/local/nginx/logs/error.log \
--http-log-path=/usr/local/nginx/logs/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/subsys/nginx \
--with-file-aio \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module \
--with-http_image_filter_module \
--with-http_geoip_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_gunzip_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-pcre \
--with-http_v2_module

make -j 4 && make install
ln -sf /usr/local/nginx/sbin/nginx /usr/bin/nginx

修改配置文件

vim /usr/local/nginx/conf/nginx.conf
user www;
worker_processes  auto;
pid        /var/run/nginx.pid;
error_log  logs/error.log  notice;
worker_rlimit_nofile 65535;

events {
	#设置工作模式
    use epoll;
	#设置最大连接数
    worker_connections  65535;
}
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" $scheme  $request_filename $args';

    access_log  logs/access.log  main;
    
	#关闭显示nginx版本号
    server_tokens off;
    
    sendfile        on;
    #防止网络阻塞
    tcp_nopush     on;
    
    #keepalive_timeout  0;
    keepalive_timeout  65;


    #优化相关的配置
    server_names_hash_bucket_size 128;
    client_max_body_size 20m;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    
    #打开压缩
    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
    
    #虚拟主机的开始
    server {
       #指定监听端口
        listen       80;
       #指定ip或者域名
       #server_name  localhost;
       #指定字体 
        #charset koi8-r;
        charset utf-8;
        #指定虚拟主机日志
        access_log  logs/host.access.log  main;
    	#设置访问根时的回应
        location / {
            #设置工作目录
            root   html;
            #设置主页
            index  index.html index.htm index.php;
        }
        #设置404时的页面            /404.html指的是工作目录下的404.html
        error_page  404              /404.html;
    
        #redirect server error pages to the static page /50x.html
        
        #指定5系列状态码时跳转页面
        error_page   500 502 503 504  /50x.html;
        #设置当访问/50x.html时的操作
        location = /50x.html {
            #设置当前的工作目录
            root   html;
        }
    }
}

管理

#前台启动
nginx -g "daemon off;"

#后台启动
nginx

#停止
nginx -s stop

#重新加载配置文件
nginx -s reload

自此,nginx编译安装完成,可以在/usr/local/nginx/html文件夹中放入文本,然后请求,查看是否能正常访问

编译安装php

这里选择的是php8.1.18

出发点是因为部署powerdnsadmin的时候,提示php版本必须大于8.1,所以,选择了8.1

其实到目前为止,大多数服务7.4版本的都可以满足,但是7.4版本的与8.1版本编译的时候有所差异,所以,如果编译7版本的,切勿直接使用以下步骤

编译安装libzip

安装依赖

yum -y install libxml2 libxml2-devel bzip2 bzip2-devel libjpeg-turbo  libjpeg-turbo-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel libcurl libcurl-devel libjpeg libjpeg-devel curl curl-devel openssl openssl-devel sqlite-devel libwebp libwebp-devel

如果原本就有,需要卸载原本的包

yum remove libzip

下载源码包

wget --no-check-certificate  https://libzip.org/download/libzip-1.3.2.tar.gz
tar xf libzip-1.3.2.tar.gz

编译安装

cd libzip-1.3.2/
./configure && make -j 4 && make install
export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig/"
ldconfig

安装freetype

可能有人有疑问,前面已经用yum安装了freetype了,为什么这里还要编译安装,因为这个编译安装的,是给php用的,php对freetype要求会稍微高点

下载源码包

wget --no-check-certificate https://download.savannah.gnu.org/releases/freetype/freetype-2.10.4.tar.gz
tar xf freetype-2.10.4.tar.gz

编译安装

cd freetype-2.10.4
./configure --prefix=/usr/local/freetype
make -j 4 && make install

编译libiconv

安装依赖

yum install libjpeg libjpeg-devel libxslt-devel  libxml2 libxml2-devel openssl-devel openssl-perl curl curl-devel libpng libpng-devel freetype freetype-devel libicu-devel -y

下载源码包

wget --no-check-certificate https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.17.tar.gz
tar xf libiconv-1.17.tar.gz

编译安装

cd libiconv-1.17
./configure --prefix=/usr/local/libiconv
make -j 4&& make install

安装remi源

https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/

下载安装源

wget --no-check-certificate https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm
rpm -ivh remi-release-7.rpm

用remi源安装oniguruma5php

yum -y install --enablerepo=remi oniguruma5php oniguruma5php-devel

编译安装PHP

下载源码包

wget --no-check-certificate https://www.php.net/distributions/php-8.1.18.tar.gz
tar xf php-8.1.18.tar.gz

编译安装

cd php-8.1.18
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/conf.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv \
--with-freetype=/usr/local/freetype \
--with-jpeg \
--with-zlib \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--enable-ftp \
--enable-gd \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-zip \
--enable-soap \
--with-gettext \
--enable-opcache \
--with-xsl \
--with-pear \
--with-webp 
make -j 4 && make install

新建相关工具的软连接

非必须

ln -sf /usr/local/php/bin/php /usr/bin/php
ln -sf /usr/local/php/bin/phpize /usr/bin/phpize
ln -sf /usr/local/php/bin/pear /usr/bin/pear
ln -sf /usr/local/php/bin/pecl /usr/bin/pecl
ln -sf /usr/local/php/sbin/php-fpm /usr/bin/php-fpm

配置文件相关

cp php.ini-production /usr/local/php/etc/php.ini
mkdir -p /usr/local/php/{etc,conf.d}

优化相关配置

sed -i 's/post_max_size =.*/post_max_size = 50M/g' /usr/local/php/etc/php.ini
sed -i 's/upload_max_filesize =.*/upload_max_filesize = 50M/g' /usr/local/php/etc/php.ini
sed -i 's/;date.timezone =.*/date.timezone = PRC/g' /usr/local/php/etc/php.ini
sed -i 's/short_open_tag =.*/short_open_tag = On/g' /usr/local/php/etc/php.ini
sed -i 's/;cgi.fix_pathinfo=.*/cgi.fix_pathinfo=0/g' /usr/local/php/etc/php.ini
sed -i 's/max_execution_time =.*/max_execution_time = 300/g' /usr/local/php/etc/php.ini
sed -i 's/disable_functions =.*/disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server/g' /usr/local/php/etc/php.ini

pear config-set php_ini /usr/local/php/etc/php.ini
pecl config-set php_ini /usr/local/php/etc/php.ini

wget --prefer-family=IPv4 --no-check-certificate -T 120 -t3 https://mirrors.aliyun.com/composer/composer.phar -O /usr/local/bin/composer
chmod +x /usr/local/bin/composer
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

整合nginx和php

cd /usr/local/php/
cp etc/php-fpm.conf.default etc/php-fpm.conf
cat /usr/local/php/etc/php-fpm.conf | egrep -v '((^$)|^;)'
[global]
include=/usr/local/php/etc/php-fpm.d/*.conf

#修改 php-fpm 的配置文件
cd etc/php-fpm.d/
cp www.conf.default www.conf
vim /usr/local/php/etc/php-fpm.d/www.conf
[www]
#同主机尽量使用同用户,不能使用root用户
user = www
group = www
listen = 127.0.0.1:9000
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 30
listen.owner = www
listen.group = www
listen.mode = 0666
pm.max_requests = 1024
pm.process_idle_timeout = 10s
request_terminate_timeout = 100
request_slowlog_timeout = 0
slowlog = var/log/slow.log

在nginx中添加一个代理php结尾的请求

vim /usr/local/nginx/conf/nginx.conf
# 在server下配置个location
location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name; #这里的/usr/local/nginx/html是存放php的目录,如果是在同一主机,只需要指定到nginx的html目录就行 如果不在同一主机,需要做共享目录或者把php文件放到这个目录
    include        fastcgi_params;
}

一些管理脚本相关

cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
cp sapi/fpm/php-fpm.service	 /usr/lib/systemd/system/
chmod +x /etc/init.d/php-fpm

重启服务

php-fpm reload
nginx -s reload

可以使用前台运行的形式测试,方便重启

/usr/local/php/sbin/php-fpm -F
/usr/local/nginx/sbin/nginx -g 'daemon off;'

制作docker镜像

因为LNMP用的还算常见,但是部署起来比较重,所以,这里制作成镜像,利用docker-compose部署,非常方便

nginx的Dockerfile

from centos7.9:v1
MAINTAINER hexug
add ./nginx-1.22.1.tar.gz /root
workdir /root/nginx-1.22.1
run yum -y install zlib gcc gcc-c++ libgcc zlib-devel pcre pcre-devel openssl openssl-devel libxslt-devel gd gd-devel GeoIP GeoIP-devel libxml2 libxml2-dev kde-l10n-Chinese glibc-common && localedef -c -f UTF-8 -i zh_CN zh_CN.utf8 && \
    ./configure \
--user=root \
--group=root \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/usr/local/nginx/logs/error.log \
--http-log-path=/usr/local/nginx/logs/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/subsys/nginx \
--with-file-aio \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module \
--with-http_image_filter_module \
--with-http_geoip_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_gunzip_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--with-pcre \
--with-http_v2_module && \
  make -j 4 && make install
env LC_ALL zh_CN.utf8
copy ./nginx.conf /usr/local/nginx/conf/nginx.conf
cmd ["/usr/local/nginx/sbin/nginx","-g","daemon off;"]
HEALTHCHECK --interval=5s --timeout=3s \
  CMD curl -fs http://localhost/ || exit 1
EXPOSE 80 443

制作镜像

docker build -t nginx1.22.1:v1 .

PHP的Dockerfile

from centos7.9:v1
MAINTAINER hexug
add ./freetype-2.10.4.tar.gz /root
add ./libiconv-1.17.tar.gz /root
add ./libzip-1.3.2.tar.gz /root
add ./php-8.1.18.tar.gz /root
copy ./composer /usr/local/bin/composer
env PKG_CONFIG_PATH="/usr/local/lib/pkgconfig/"
workdir /root/libzip-1.3.2
run yum -y install libxml2 libxml2-devel bzip2 bzip2-devel libjpeg-turbo  libjpeg-turbo-devel libpng libpng-devel freetype freetype-devel zlib zlib-devel libcurl libcurl-devel libjpeg libjpeg-devel curl curl-devel openssl openssl-devel sqlite-devel libwebp libwebp-devel  libxslt-devel  libxml2 libxml2-devel openssl-perl libpng libpng-devel libicu-devel && \
  yum remove libzip && \
  ./configure && make && make install && \
  ldconfig

workdir /root/freetype-2.10.4
run ./configure --prefix=/usr/local/freetype && make && make install

workdir /root/libiconv-1.17
run ./configure --prefix=/usr/local/libiconv && make && make install

copy ./remi-release-7.rpm /root/remi-release-7.rpm
workdir /root
run yum -y install epel-release && rpm -ivh remi-release-7.rpm && yum -y install --enablerepo=remi oniguruma5php oniguruma5php-devel

workdir /root/php-8.1.18
run ./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/conf.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-iconv \
--with-freetype=/usr/local/freetype \
--with-jpeg \
--with-zlib \
--enable-xml \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--enable-ftp \
--enable-gd \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-zip \
--enable-soap \
--with-gettext \
--enable-opcache \
--with-xsl \
--with-pear \
--with-webp && make && make install && ln -sf /usr/local/php/bin/php /usr/bin/php && \
  ln -sf /usr/local/php/bin/phpize /usr/bin/phpize && \
  ln -sf /usr/local/php/bin/pear /usr/bin/pear && \
  ln -sf /usr/local/php/bin/pecl /usr/bin/pecl && \
  ln -sf /usr/local/php/sbin/php-fpm /usr/bin/php-fpm && \
  cp php.ini-production /usr/local/php/etc/php.ini && \
  mkdir -p /usr/local/php/{etc,conf.d} && \
  sed -i 's/post_max_size =.*/post_max_size = 50M/g' /usr/local/php/etc/php.ini && \
  sed -i 's/upload_max_filesize =.*/upload_max_filesize = 50M/g' /usr/local/php/etc/php.ini && \
  sed -i 's/;date.timezone =.*/date.timezone = PRC/g' /usr/local/php/etc/php.ini && \
  sed -i 's/short_open_tag =.*/short_open_tag = On/g' /usr/local/php/etc/php.ini && \
  sed -i 's/;cgi.fix_pathinfo=.*/cgi.fix_pathinfo=0/g' /usr/local/php/etc/php.ini && \
  sed -i 's/max_execution_time =.*/max_execution_time = 300/g' /usr/local/php/etc/php.ini && \
  sed -i 's/disable_functions =.*/disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server/g' /usr/local/php/etc/php.ini && \
  pear config-set php_ini /usr/local/php/etc/php.ini && \
  pecl config-set php_ini /usr/local/php/etc/php.ini && \
  chmod +x /usr/local/bin/composer && \
  composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/ && \
  cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
copy ./www.conf /usr/local/php/etc/php-fpm.d/www.conf
workdir /usr/local/php
run rm -rf /root/* && groupadd -r www && useradd -r -g www www && chown -R www:www /usr/local/php
cmd ["/usr/local/php/sbin/php-fpm","-F"]
EXPOSE 9000

制作镜像

docker build -t php.8.1.18:v1 .

需要注意的是,这里需要几个配置文件 几个包,在上面分步骤编译安装中都有涉及到,这里就不再赘述了

docker-compose运行

version: '3'
services:
  nginx:
    image: nginx-1.22.1:v3
    container_name: nginx
    ports:
    - 80:80
    restart: always
    volumes:
    - ./nginx.conf:/usr/local/nginx/conf/nginx.conf
    - ./html:/usr/local/nginx/html
    networks:
    - lnmp
    depends_on:
    - php
  php:
    image: php-8.1.18:v11
    container_name: php
    restart: always
    ports:
    - 9000:9000
    volumes:
    - ./www.conf:/usr/local/php/etc/php-fpm.d/www.conf
    - ./html:/usr/local/php/html
    networks:
    - lnmp
  mysql:
    image: mysql:8.0
    container_name: mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: 111111
      #MYSQL_ALLOW_EMPTY_PASSWORD: yes
      MYSQL_DATABASE: powerdns
      MYSQL_USER: powerdns
      MYSQL_PASSWORD: powerdns
    networks:
    - lnmp
    volumes:
    - ./db:/var/lib/mysql
    - ./etc/my.cnf:/etc/my.cnf
    - /usr/share/zoneinfo/Asia/Shanghai:/etc/localtime
    ports:
    - 3306:3306
    command:
      - --default-authentication-plugin=mysql_native_password  
networks:
  lnmp:

注意:根据自己的实际编译镜像来选择镜像,然后映射的文件结构以及端口也需要根据实际情况来,这里选择的8.0的mysql,因为8.0的默认不允许root远程访问数据库,所以这里需要加一个命令 --default-authentication-plugin=mysql_native_password

标签:--,18,LNMP,php8.1,nginx,usr,devel,php,local
From: https://www.cnblogs.com/guangdelw/p/17351888.html

相关文章

  • ERROR 1820 (HY000): You must reset your password using ALTER USER statement befo
    场景安装后首次运行mysql命令报错误:ERROR1820(HY000):YoumustresetyourpasswordusingALTERUSERstatementbeforeexecutingthisstatement修改密码mysql>alteruseruser() identifiedby‘admin@123456’;QueryOK,0rowsaffected(0.03sec)mysql>flushp......
  • Codeforces 1804G - Flow Control(势能分析)
    成功把这道小清新题做成了一道大数据结构题,我的评价是我是小丑。首先显然要离散化对时间轴扫描线。这个除以\(2\)下取整的操作显然启示我们往势能的方向思考,也就是我们希望能够找到某个变量,使得这个变量的均摊变化次数在可接受范围内。但是直接以每个元素的值为势能好像也不太......
  • ACM International Collegiate Programming Contest, Amman Collegiate Programming C
    Youaregivenan × mgrid,yourgoalistofindagroupoflinessuchthatthefollowingconditionsaremet:Notwolinesaretouching.Eachcellinthegridhasoneofitssidescoveredbyatleastonelineinthegroup.Alineisaborderofacellin......
  • [Week 18] 每日一题(C++,动态规划,线段树,数学)
    目录[Daimayuan]T1最长公共子序列(C++,DP,二分)输入格式输出格式数据范围输入样例输出样例解题思路[Daimayuan]T2喵喵序列(C++,序偶)题目描述输入格式输出格式样例输入样例输出样例说明数据范围双倍经验解题思路:[Daimayuan]T3漂亮数(C++,字符串)输入描述输出描述输入样例输出样例解题......
  • [CMU 15-418] (Lecture4) Parallel Programming Basics
    本系列文章为CMU15-418/15-618:ParallelComputerArchitectureandProgramming,Fall2018课程学习笔记课程官网:CMU15-418/15-618:ParallelComputerArchitectureandProgramming参考文章:CMU15-418notes相关资源与介绍:CMU15-418/StanfordCS149:ParallelComput......
  • MySQL 错误1418 的解决方法
    使用mysql创建、调用存储过程,函数以及触发器的时候会有错误符号为1418错误。mysql开启了bin-log,我们就必须指定我们的函数是否是哪种类型:1DETERMINISTIC不确定的2NOSQL没有SQl语句,当然也不会修改数据3READSSQLDATA只是读取数据,当然也不会修改数据4MODIFIESSQLD......
  • CF1806E
    题面看起来是个DS题,事实上是个乱搞题,做法挺多的。由于它给的这个结构看起来就不好优化,所以考虑随机化。由于两个点到达LCA后剩下的贡献就是LCA到根的每个点权值的平方,这部分可以\(O(n)\)预处理,所以只需要考虑两个点之间的路径所产生的贡献。在树上随机撒\(\sqrtn\)......
  • ctfshow web入门 sql注入 web 183-186
    web183-web186涉及盲注,不管是时间盲注还是布尔盲注,若用手工,会非常耗时,通常使用脚本重点:​ 1、了解python脚本编写​ 2、了解条件语句(where、having)区别​ 3、了解sql语句位运算符​ 4、了解mysql特性​ 5、扩展了解简单爬虫目录web183web184web185web186web183//拼......
  • P4180 [BJWC2010] 严格次小生成树
    P4180[BJWC2010]严格次小生成树/*建立一个最小生成树维护最大值和严格次小值然后直接查询就可以了56121132243354343456*/#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongusingpii=pair<int,int>;constintN=1e......
  • codeforces 118D D. Caesar's Legions(dp)
    题目链接:codeforces118D题目大意:给出n1个1,n2个2,给出k1和k2代表连续的1和2的最大长度,问能够构造的合法的不同串的数量。题目分析:能够递推,所以想到能够利用dp做。首先我们定义状态,dp[i][j][k][2]代表以1或2结尾,结尾相同的元素的数量为k,1的总数是j的当前序列长度为i的串的数量。首先......