编译安装nginx+php和php扩展脚本

xiaohai 2020-10-17 21:17:24 1796人围观 标签: PHP  Nginx  php 
简介本文主要记录在Centos7下编译安装nginx和php,还有php的扩展和Composer的安装。但是需要注意的是需要自己对脚本进行修改,还需要下载对应的包。
一、脚本使用说明

本脚本使用的软件版本如下:

  • nginx-1.14.2.tar.gz
  • php-7.2.16.tar.gz
  • libevent-2.1.8-stable.tar.gz

如果你需要使用该脚本安装nginx和php,那么请下载对应的版本,或者下载了你自己的版本,那么就需要对脚本中的文件名进行修改。请将该脚本与软件包放在同一目录下,然后再执行脚本。

二、脚本内容,如下:
#!/bin/sh
#一、全局安装
yum -y install lrzsz git autoconf wget

#二、安装nginx部分脚本
cd ~
yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel

tar -zxf nginx-1.14.2.tar.gz

cd nginx-1.14.2

./configure --prefix=/home/nginx-1.14.2 && make && make install


cat > /etc/systemd/system/nginx.service<<EOF
[Unit]
Description=nginx
After=network.target
  
[Service]
Type=forking
ExecStart=/home/nginx-1.14.2/sbin/nginx
ExecReload=/home/nginx-1.14.2/sbin/nginx -s reload
ExecStop=/home/nginx-1.14.2/sbin/nginx -s quit
PrivateTmp=true
  
[Install]
WantedBy=multi-user.target
EOF


systemctl enable nginx.service

systemctl start nginx.service

ps aux | grep nginx

#三、安装php部分脚本
cd ~
yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel


tar -zxf php-7.2.16.tar.gz


cd php-7.2.16


./configure  --prefix=/home/php-7.2.16 --enable-fpm --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-openssl --with-zlib --with-curl --enable-ftp --with-gd --with-xmlrpc --with-jpeg-dir --with-png-dir --with-freetype-dir  --enable-mbstring --enable-zip --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql-sock=/var/lib/mysql/mysql.sock --without-pear --enable-bcmath --enable-opcache


make && make install

cp php.ini-development /etc/php.ini

groupadd www

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

cd /home/php-7.2.16/etc/


cp php-fpm.conf.default php-fpm.conf

cd php-fpm.d/

cp www.conf.default www.conf

sed -i "s/user = nobody/user = www/g;s/group = nobody/group = www/g;s/pm = dynamic/pm = static/g;s/pm.max_children = 5/pm.max_children = 500/g" www.conf


cat > /etc/systemd/system/php-fpm.service<<EOF
[Unit]

Description=php-fpm
After=network.target

[Service]
Type=forking
ExecStart=/home/php-7.2.16/sbin/php-fpm
PrivateTmp=True

[Install]
WantedBy=multi-user.target
EOF

echo 'export PHP_HOME="/home/php-7.2.16"' >> /etc/profile
echo 'export PATH=$PHP_HOME/bin:$PATH' >> /etc/profile
source /etc/profile

systemctl enable php-fpm.service

systemctl start php-fpm.service

ps aux | grep php-fpm

#四、php扩展安装依赖库
cd ~

tar -zxf libevent-2.1.8-stable.tar.gz
cd libevent-2.1.8-stable
./configure && make && make install


#五、composer安装
#composer下载地址:https://getcomposer.org/download/
wget https://getcomposer.org/download/1.8.6/composer.phar -O /usr/local/bin/composer

chmod a+x /usr/local/bin/composer

#composer config -g repo.packagist composer https://packagist.laravel-china.org
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/


#六、php扩展安装(mongodb、nsq、redis、swoole)
cd /home/php-7.2.16/bin

wget -O go-pear.php http://pear.php.net/go-pear.phar

chmod +x go-pear.php

./php go-pear.php 


./pecl install mongodb
echo "extension=mongodb.so" >> /etc/php.ini


./pecl install nsq
echo "extension=nsq.so" >> /etc/php.ini

./pecl install redis
echo "extension=redis.so" >> /etc/php.ini 

./pecl install swoole
echo "extension=swoole.so" >> /etc/php.ini 

systemctl restart php-fpm