TencentOS Server 3.1 安装 Nginx 1.23、PHP 8.2、MariaDB 10.11

Fork Me On Github
zodream 编程技术 2023年02月

CentOS 的命令一样

一些其他辅助命令

   
yum list installed #查询已安装程序

ps auxf |grep nginx # 查询正在运行进程
123

Nginx

安装基本编译环境

        
sudo yum -y install gcc gcc-c++ # nginx编译时依赖gcc环境

sudo yum -y install pcre pcre-devel # 让nginx支持重写功能

sudo yum -y install zlib zlib-devel # zlib库提供了很多压缩和解压缩的方式,nginx使用zlib对http包内容进行gzip压缩

sudo yum -y install openssl openssl-devel # 安全套接字层密码库,用于通信加密
12345678

下载源码包

下载 pcre2

下载 zlib

源码包放到 /usr/local/src 下解压

  
cd /usr/local/src #进入src文件夹
wget https://nginx.org/download/nginx-1.23.3.tar.gz #下载nginx源码包
12

解压三个源码压缩包

   
tar -zxvf pcre2-10.42.tar.gz # 解压缩
tar -zxvf zlib-1.2.13.tar.gz
tar -zxvf nginx-1.23.3.tar.gz
123

编译Nginx

      
cd nginx-1.23.3

./configure --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/usr/local/nginx/logs/nginx.pid --with-http_ssl_module --with-pcre=../pcre2-10.42 --with-zlib=../zlib-1.2.13

make            #编译
make install        #安装
123456

如果后面遇到某个功能没有,只要修改 configure 中的参数就行了,重复执行 make make install 即可

配置启动Nginx

    
/usr/local/nginx/sbin/nginx             #启动服务
/usr/local/nginx/sbin/nginx -s reload   #重新加载服务
/usr/local/nginx/sbin/nginx -s stop     #停止服务
ps -ef | grep nginx                         #查看服务进程
1234

更改配置

 
vi /usr/local/nginx/conf/nginx.conf # 这是站点配置文件
1

开始设置开启启动项

  
vi /usr/lib/systemd/system/nginx.service
12
txt
             
[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/usr/local/nginx/sbin/nginx -s reload
PrivateTmp=true

[Install]
WantedBy=multi-user.target
12345678910111213

启用开机启动服务

 
systemctl enable nginx.service
1

最终的启动运行命令

     
service nginx start
service nginx stop
service nginx restart
service nginx reload
12345

PHP 8

安装基本编译环境

 
yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel curl curl-devel openssl openssl-devel sqlite-devel gmp-devel oniguruma-devel readline-devel libxslt-devel
1

安装CMake

         
wget https://github.com/Kitware/CMake/releases/download/v3.25.2/cmake-3.25.2.tar.gz

tar -zxvf cmake-3.25.2.tar.gz
cd cmake-3.25.2
./bootstrap
gmake
ln -s /usr/local/src/cmake-3.25.2/bin/cmake /usr/bin/cmake
cmake --version
123456789

安装libzip 必须先安装CMake

下载 libzip

         
yum remove libzip
wget https://libzip.org/download/libzip-1.9.2.tar.gz
tar -zxvf libzip-1.9.2.tar.gz
cd libzip-1.9.2
mkdir build
cd build
cmake ..
make
make install
123456789

安装完成后,查看是否存在/usr/local/lib64/pkgconfig目录,如果存在,执行如下命令来设置PKG_CONFIG_PATH:

  
export PKG_CONFIG_PATH="/usr/local/lib64/pkgconfig/"
12

下载源码包

   
cd /usr/local/src
wget https://www.php.net/distributions/php-8.2.3.tar.gz  # 下载
tar -zxvf php-8.2.3.tar.gz   # 解压缩
123

编译安装

       
cd php-8.2.3

./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-mbstring --enable-ftp --enable-gd --enable-gd-jis-conv --enable-mysqlnd --enable-pdo --enable-sockets --enable-fpm --enable-xml --enable-soap --enable-pcntl --enable-cli --enable-bcmath --with-openssl --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-pear --with-zlib --with-iconv --with-curl --with-zip --with-gettext


make            #编译
make install        #安装
1234567

修改配置文件

            
cd /usr/local/php/etc
cp php-fpm.conf.default php-fpm.conf
cd /usr/local/php/etc/php-fpm.d
cp www.conf.default www.conf


find /usr/local/src/php-8.2.3 -name php.ini*
cp /usr/local/src/php-8.2.3/php.ini-production /usr/local/php/etc/php.ini


vim /usr/local/php/etc/php.ini
123456789101112

;cgi.fix_pathinfo=1 改为 cgi.fix_pathinfo=0

expose_php = On 改为 expose_php = Off 隐藏版本号

 
vim /usr/local/php/etc/php-fpm.conf
1

[global] 下的 pid = run/php-fpm.pid 启用, 后面 php-fpm.service 中的 PIDFile 必须设成一样的路径,否则会出现找不到php-fpm.pid的情况

 
vim /usr/local/php/etc/php-fpm.d/www.conf
1

user = nobody group = nobody 改为 user = www group = www 需要先添加 www 用户和 www

初始命令

   
/usr/local/php/sbin/php-fpm -R   # 这里后面带个 -R  表示用root 用户启动
/usr/bin/pkill -9 php-fpm
pstree -p | grep php
123

设置开机启动项

 
vi /usr/lib/systemd/system/php-fpm.service
1
txt
             
[Unit]
Description=php-fpm
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/php/var/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm
ExecStop=/usr/bin/pkill -9 php-fpm
PrivateTmp=true

[Install]
WantedBy=multi-user.target
12345678910111213
 
systemctl enable php-fpm.service
1

可以使用systemctl命令管理php-fpm:

       
systemctl start php-fpm.service  #启动
systemctl stop php-fpm.service   #停止
service php-fpm start
service php-fpm stop
service php-fpm restart
service php-fpm reload
1234567

搭配Nginx运行PHP站点

配置 Nginx

  
vi /usr/local/nginx/conf/nginx.conf
12

http 下添加

txt
         
client_max_body_size 200M; #配置客户端请求体最大值
client_body_buffer_size 50m; #配置请求体缓存区大小
server_tokens off; # 隐藏响应头中的版本号

gzip on;
gzip_min_length 1k; #不压缩临界值,大于1k的才压缩
gzip_buffers 4 16k;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript text/css application/font-woff; # 需要压缩什么文件就加上文件类型,对于图片还是不要使用gzip压缩,直接在本地修改成其他图片格式效果更好
123456789
conf
                                                   
server {
    server_name  zodream.cn;
    root         /data/www/html; # 设置站点根目录

    location / {
        # root   html;
        index  index.php index.html index.htm;
        try_files $uri $uri/ /index.php?$query_string; # 美化和伪静态需要设置路由重写
    }

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    ## 配置 xxsqladmin 路径指向phpmyadmin
    location ~ ^/xxsqladmin/.*\.php$ {
        root   /data/shop/phpmyadmin;
        set $real_script_name $fastcgi_script_name;
        if ($fastcgi_script_name ~ "^/xxsqladmin(.+?\.php)(.*)$") {
            set $real_script_name $1;
        }
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$real_script_name;
        include        fastcgi_params;
    }

    ## 配置 xxsqladmin 资源文件路径指向phpmyadmin
    location ~ ^/xxsqladmin.* {
        root   /data/shop/phpmyadmin;
        rewrite ^/xxsqladmin(.*)$ /$1 break;
    }

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

    # 添加https支持
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/zodream.cn/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/zodream.cn/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051

修改完保存文件并重启Nginx服务器

MariaDB

安装

 
yum install mariadb-server
1

如果本地已安装 mayql 则会出现冲突 增加 参数即可 --allowerasing

启动和设置开机启动

   
systemctl start mariadb  # 开启服务

systemctl enable mariadb  # 设置为开机自启动服务
123

设置修改密码

 
mysql_secure_installation
1

使用 Certbot 获取 letsencrypt 证书

   
yum install certbot python3-certbot-nginx

certbot --nginx
123

如果出现 找不到 nginx

  
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
ln -s /usr/local/nginx/conf/ /etc/nginx
12

新增用户

       
cat /etc/passwd

# useradd -s /sbin/nologin -M www

groupadd www

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

赋予权限

 
chown -R www:www /run/php-fpm
1

PHP拓展安装

imagick

           
sudo yum -y install ImageMagick-devel
cd /usr/local/src
tar xvf imagick-3.7.0.tgz

cd imagick-3.7.0

/usr/local/php/bin/phpize
./configure  --with-php-config=/usr/local/php/bin/php-config
make
make install
1234567891011

php.ini

 
extension=imagick.so
1

redis

安装 redis 主程序

        
cd /usr/local/src
wget https://download.redis.io/redis-stable.tar.gz
tar -xzvf redis-stable.tar.gz
cd redis-stable
make
make install

redis-server
12345678

现在,我们新建目录 /usr/local/redis ,把./redis.conf,src/redis-server,src/redis-cli 三个文件复制到该目录下

     
mkdir /usr/local/redis
cp redis.conf src/redis-server src/redis-cli /usr/local/redis/
cd /usr/local/redis

vi redis.conf
12345
    
daemonize yes # 开启守护线程

masterauth <redis_password> # 设置密码
1234

启动

 
./redis-server redis.conf 
1

安装 phpredis

          
cd /usr/local/src
tar xvf redis-5.3.7.tgz

cd redis-5.3.7

/usr/local/php/bin/phpize
./configure  --with-php-config=/usr/local/php/bin/php-config
make
make install
12345678910

php.ini

 
extension=redis.so
1

安装多版本php 7

下载源码包

   
cd /usr/local/src
wget https://www.php.net/distributions/php-7.4.33.tar.gz  # 下载
tar -zxvf php-7.4.33.tar.gz   # 解压缩
123

编译安装

-–prefix 是安装目录,--with-config-file-path 是配置文件存放目录

       
cd php-7.4.33

./configure --prefix=/usr/local/php7 --with-config-file-path=/usr/local/php7/etc --enable-mbstring --enable-ftp --enable-gd --enable-gd-jis-conv --enable-mysqlnd --enable-pdo --enable-sockets --enable-fpm --enable-xml --enable-soap --enable-pcntl --enable-cli --enable-bcmath --with-openssl --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-pear --with-zlib --with-iconv --with-curl --with-gettext


make            #编译
make install        #安装
1234567

修改配置文件

同原本的配置方法 [PHP8配置]()

注意将 /usr/local/php7/etc/php-fpm.d/www.conf 中的端口改成其他的,不要和原本的冲突

 
listen = 127.0.0.1:9001
1

设置开机启动项

 
vi /usr/lib/systemd/system/php7-fpm.service
1
txt
             
[Unit]
Description=php7-fpm
After=network.target

[Service]
Type=forking
PIDFile=/usr/local/php7/var/run/php-fpm.pid
ExecStart=/usr/local/php7/sbin/php-fpm
ExecStop=/usr/bin/pkill -9 php7-fpm
PrivateTmp=true

[Install]
WantedBy=multi-user.target
12345678910111213
 
systemctl enable php7-fpm.service
1

可以使用systemctl命令管理php7-fpm:

    
service php7-fpm start
service php7-fpm stop
service php7-fpm restart
service php7-fpm reload
1234

配置站点

/usr/local/nginx/conf/nginx.conffastcgi_pass 配置成PHP7监听的端口

         
    server {
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9001;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

    }
123456789

启动PHP7,重启nginx 即可

点击查看全文
0 418 0