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

xiaohai 2020-10-17 21:17:24 2868人围观 标签: 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,那么请下载对应的版本,或者下载了你自己的版本,那么就需要对脚本中的文件名进行修改。请将该脚本与软件包放在同一目录下,然后再执行脚本。

二、脚本内容,如下:
  1. #!/bin/sh
  2. #一、全局安装
  3. yum -y install lrzsz git autoconf wget
  4. #二、安装nginx部分脚本
  5. cd ~
  6. yum -y install gcc gcc-c++ pcre pcre-devel zlib zlib-devel
  7. tar -zxf nginx-1.14.2.tar.gz
  8. cd nginx-1.14.2
  9. ./configure --prefix=/home/nginx-1.14.2 && make && make install
  10. cat > /etc/systemd/system/nginx.service<<EOF
  11. [Unit]
  12. Description=nginx
  13. After=network.target
  14. [Service]
  15. Type=forking
  16. ExecStart=/home/nginx-1.14.2/sbin/nginx
  17. ExecReload=/home/nginx-1.14.2/sbin/nginx -s reload
  18. ExecStop=/home/nginx-1.14.2/sbin/nginx -s quit
  19. PrivateTmp=true
  20. [Install]
  21. WantedBy=multi-user.target
  22. EOF
  23. systemctl enable nginx.service
  24. systemctl start nginx.service
  25. ps aux | grep nginx
  26. #三、安装php部分脚本
  27. cd ~
  28. 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
  29. tar -zxf php-7.2.16.tar.gz
  30. cd php-7.2.16
  31. ./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
  32. make && make install
  33. cp php.ini-development /etc/php.ini
  34. groupadd www
  35. useradd -M -g www -s /sbin/nologin www
  36. cd /home/php-7.2.16/etc/
  37. cp php-fpm.conf.default php-fpm.conf
  38. cd php-fpm.d/
  39. cp www.conf.default www.conf
  40. 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
  41. cat > /etc/systemd/system/php-fpm.service<<EOF
  42. [Unit]
  43. Description=php-fpm
  44. After=network.target
  45. [Service]
  46. Type=forking
  47. ExecStart=/home/php-7.2.16/sbin/php-fpm
  48. PrivateTmp=True
  49. [Install]
  50. WantedBy=multi-user.target
  51. EOF
  52. echo 'export PHP_HOME="/home/php-7.2.16"' >> /etc/profile
  53. echo 'export PATH=$PHP_HOME/bin:$PATH' >> /etc/profile
  54. source /etc/profile
  55. systemctl enable php-fpm.service
  56. systemctl start php-fpm.service
  57. ps aux | grep php-fpm
  58. #四、php扩展安装依赖库
  59. cd ~
  60. tar -zxf libevent-2.1.8-stable.tar.gz
  61. cd libevent-2.1.8-stable
  62. ./configure && make && make install
  63. #五、composer安装
  64. #composer下载地址:https://getcomposer.org/download/
  65. wget https://getcomposer.org/download/1.8.6/composer.phar -O /usr/local/bin/composer
  66. chmod a+x /usr/local/bin/composer
  67. #composer config -g repo.packagist composer https://packagist.laravel-china.org
  68. composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
  69. #六、php扩展安装(mongodb、nsq、redis、swoole)
  70. cd /home/php-7.2.16/bin
  71. wget -O go-pear.php http://pear.php.net/go-pear.phar
  72. chmod +x go-pear.php
  73. ./php go-pear.php
  74. ./pecl install mongodb
  75. echo "extension=mongodb.so" >> /etc/php.ini
  76. ./pecl install nsq
  77. echo "extension=nsq.so" >> /etc/php.ini
  78. ./pecl install redis
  79. echo "extension=redis.so" >> /etc/php.ini
  80. ./pecl install swoole
  81. echo "extension=swoole.so" >> /etc/php.ini
  82. systemctl restart php-fpm