编译部署LNMP-php8.1.18版本

由于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

posted @ 2023-04-25 12:03  厚礼蝎  阅读(112)  评论(0编辑  收藏  举报