Linux
下配置一个 PHP
+Nginx
+MySQL
/MariaDB
环境
编译安装多版本PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| #!/bin/sh #编译安装多版本PHP, Nginx+PHP使用
#定义函数,默认绿色输出 '#' 开头为红色 function echocolor() { [[ $1 = '#' ]] && echo -e "\033[31m $* \033[0m" || echo -e "\033[32m $* \033[0m" }
#检测网络 ping baidu.com -c 2 &>/dev/null || ping qq.com -c 2 &>/dev/null || { echocolor '#' "网络异常!";exit; }
#设置yum 安装包不删除 目录 /var/cache/yum/ #sed -i 's/keepcache=0/keepcache=1/g' /etc/yum.conf
echocolor '#安装需要的库' yum install epel-release -y yum install gcc bison bison-devel zlib-devel libmcrypt-devel mcrypt mhash-devel openssl-devel libxml2-devel libcurl-devel bzip2-devel readline-devel libedit-devel sqlite-devel jemalloc jemalloc-devel libjpeg-devel libpng-devel libicu-devel openldap-devel ftp -y
echocolor "#设置相关目录" echocolor "#下载目录" read Ddir
echocolor "#安装版本" read Dver Sdir="/usr/local/php/php$Dver"
echocolor "#安装目录:" echocolor "$Sdir" [ -d $Ddir ] || mkdir -p $Ddir [ -d $Sdir/etc ] || mkdir -p $Sdir/etc
#[ -d $Sdir/apache/bin/apxs ] || mkdir -p $Sdir/apache/bin/apxs
# echocolor "#请输入下载链接" read Durl wget -c $Durl -O $Ddir/php.tar.gz
echocolor "添加用户" useradd -s /sbin/nologin -M www id www
echocolor "解压" [ -e $Ddir/php.tar.gz ] && { tar xzvf $Ddir/php*.tar.gz -C $Ddir/;cd $Ddir/php*; } || { echocolor '#' "$Ddir目录无php源码包";exit ; } # [ `uname -m` = "x86_64" ] && LIB=/usr/lib64 || LIB=/usr/lib64 # --with-apxs2=/usr/local/apache/bin/apxs make clean
echocolor 编译 ./configure \ --prefix=$Sdir/ \ --with-config-file-path=$Sdir/etc \ --enable-inline-optimization \ --disable-debug \ --disable-rpath \ --enable-shared \ --enable-opcache \ --enable-fpm \ --with-fpm-user=www \ --with-fpm-group=www \ --with-mysql=mysqlnd \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \ --with-gettext \ --enable-mbstring \ --with-iconv \ --with-mcrypt \ --with-mhash \ --with-openssl \ --enable-bcmath \ --enable-soap \ --with-libxml-dir \ --enable-pcntl \ --enable-shmop \ --enable-sysvmsg \ --enable-sysvsem \ --enable-sysvshm \ --enable-sockets \ --with-curl \ --with-zlib \ --enable-zip \ --with-bz2 \ --enable-ftp \ --with-ldap-dir=/usr/lib \ --with-png-dir=/usr/lib \ --with-jpeg-dir=/usr/lib \ --with-readline [ $? = 0 ] || { echocolor '#' "编译出现问题 !";exit; } echo 编译安装 make -j4 make install make clean make clean all echo #配置PHP cp php.ini-production $Sdir/etc/php.ini cp $Sdir/etc/php-fpm.conf.default $Sdir/etc/php-fpm.conf cp sapi/fpm/init.d.php-fpm $Sdir/bin/php-fpm-$Dver cp $Sdir/etc/php-fpm.d/www.conf.default $Sdir/etc/php-fpm.d/www.conf chmod +x $Sdir/bin/php-fpm-$Dver ln -s $Sdir/bin/php-fpm-$Dver /etc/init.d/php-fpm-$Dver ln -s $Sdir/etc/php-fpm.conf /etc/php-fpm/php-fpm-$Dver.conf ln -s $Sdir/etc/php.ini /etc/php/php-$Dver.ini chkconfig --add php-fpm-$Dver chkconfig php-fpm-$Dver on chkconfig --list php-fpm-$Dver echocolor #显示版本 $Sdir/bin/php -v netstat -antp|grep php-fpm-$Dver echocolor "#php-$Dver已经安装完毕"
|
1 2 3 4 5 6 7 8 9
|
/download
72
/usr/local/php/php72
http://cn2.php.net/get/php-7.2.7.tar.gz/from/this/mirror
|
说明
8.0
: https://downloads.php.net/~pollita/php-8.0.0alpha1.tar.gz
7.4
: https://www.php.net/distributions/php-7.4.7.tar.gz
7.2
: http://cn2.php.net/get/php-7.2.7.tar.gz/from/this/mirror
5.6
: http://cn2.php.net/get/php-5.6.36.tar.gz/from/this/mirror
变量说明:
Ddir
: 下载目录
Dver
: 安装版本
Sdir
: 安装目录
Durl
: 下载链接
下载过程输入:
- 设置目录 - 无需输入, 起提示作用
- 下载目录 - 输入下载目录
- 安装目录 - 输入安装目录 (默认前缀有
php
, 只需要输入类似56
、71
)
- 请输入下载链接 - 输入下载链接(
tar.gz
包), 选择自己需要的PHP
版本
1 2 3 4 5 6 7 8 9 10 11 12
|
/root/linux
72
/usr/local/php/php72
http://cn2.php.net/get/php-7.2.7.tar.gz/from/this/mirror
|
源码解释
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| echocolor 编译 ./configure \ --prefix=$Sdir/ \ --with-config-file-path=$Sdir/etc \ --enable-inline-optimization \ --disable-debug \ --disable-rpath \ --enable-shared \ --enable-opcache \ --enable-fpm \ --with-fpm-user=www \ --with-fpm-group=www \ --with-mysql=mysqlnd \ --with-mysqli=mysqlnd \ --with-pdo-mysql=mysqlnd \ --with-gettext \ --enable-mbstring \ --with-iconv \ --with-mcrypt \ --with-mhash \ --with-openssl \ --enable-bcmath \ --enable-soap \ --with-libxml-dir \ --enable-pcntl \ --enable-shmop \ --enable-sysvmsg \ --enable-sysvsem \ --enable-sysvshm \ --enable-sockets \ --with-curl \ --with-zlib \ --enable-zip \ --with-bz2 \ --enable-ftp \ --with-ldap-dir=/usr/lib \ --with-png-dir=/usr/lib \ --with-jpeg-dir=/usr/lib \ --with-readline [ $? = 0 ] || { echocolor '#' "编译出现问题 !";exit; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| --with-freetype-dir \ --with-jpeg-dir \ --with-png-dir \ --with-curlwrappers \ --with-gd \ --enable-gd-native-ttf \ --with-xmlrpc \ --without-iconv \ --with-ttf \ --with-xsl \ --with-pear \ --enable-calendar \ --enable-exif \ --enable-magic-quotes \
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| --enable-fastCGI \ --enable-force-CGI-redirect \ --with-ncurses \ --with-gmp \ --enable-dbase \ --with-pcre-dir=/usr/local/bin/pcre-config --disable-dmalloc --with-gdbm --enable-sigchild --enable-sysvshm --enable-zend-multibyte \ --enable-wddx --enable-soap
|
编译安装Nginx
环境准备
安装编译工具、依赖包
1 2
| yum -y install gcc gcc-c++ autoconf automake yum -y install zlib zlib-devel openssl openssl-devel pcre-devel
|
以上安装的是一些主要的依赖包,具体可根据自己情况或者报错信息提示安装或修改
新建匿名用户和用户组
1 2 3
| sudo groupadd -r nginx
sudo useradd -s /sbin/nologin -g nginx -r nginx
|
Nginx
编译安装
下载源码包
建议从官网下载
1
| wget https://nginx.org/download/nginx-1.15.1.tar.gz
|
解压并编译
1
| tar -zxvf nginx-1.15.1.tar.gz
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| ./configure --prefix=/usr/local/nginx \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --with-http_ssl_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_realip_module \ --with-http_gzip_static_module \ --with-http_stub_status_module \ --with-mail \ --with-mail_ssl_module \ --with-debug \ --http-client-body-temp-path=/var/tmp/nginx/client \ --http-proxy-temp-path=/var/tmp/nginx/proxy \ --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ --http-scgi-temp-path=/var/tmp/nginx/scgi
|
配置正常的话可以看到这些信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| Configuration summary + using system PCRE library + OpenSSL library is not used + md5: using system crypto library + sha1: using system crypto library + using system zlib library
nginx path prefix: "/usr/local/nginx" nginx binary file: "/usr/local/nginx/sbin/nginx" nginx modules path: "/usr/local/nginx/modules" nginx configuration prefix: "/etc/nginx" nginx configuration file: "/etc/nginx/nginx.conf" nginx pid file: "/usr/local/nginx/logs/nginx.pid" nginx error log file: "/usr/local/nginx/logs/error.log" nginx http access log file: "/usr/local/nginx/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp"
|
以上编译参数只是配置了主要的东西,全部配置参数说明你可以通过这个命令查看: ./configure --help
启动等命令,须进入到/usr/local/nginx/sbin
目录下
- 启动:
nginx
- 停止:
nginx -s stop
- 重启:
nginx -s reload
编译安装MySQL
环境准备
编译环境
1 2
| yum install cmake yum install –y openssl openssl-devel ncurses ncurses-devel boost-devel
|
其他可选准备
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| rpm -qa | grep mysql
[root@shuidianbang ~] libmysqlclient16-5.1.69-1.w6.x86_64 mysql55w-5.5.59-1.w6.x86_64 mysql55w-libs-5.5.59-1.w6.x86_64 mysql55w-server-5.5.59-1.w6.x86_64
[root@shuidianbang ~] perl-DBD-MySQL-4.013-3.el6.x86_64
// 屏幕上将显示已安装的mysql包名如: mysql55w-5.5.59-1.w6.x86_64 ; rpm -e --nodeps mysql-server (nodeps表示强制删除) rpm -e --nodeps mysql
groupadd -r mysql useradd -r -g mysql -d /data/mysql -s /sbin/nologin mysql
mkdir /data/mysql chown -R mysql:mysql /data/mysql
|
相关编译选项
完整的编译配置选项
1 2 3
| -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_FEDEFATED_STORAGE_ENGINE=1
-DWITHOUT_<engine-name>_ENGINE=1
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 -DWITHOUT_FEDERATED_STORAGE_ENGINE=1 -DWITHOUT_PARTITION_STORAGE_ENGINE=1
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| -DMYSQL_TCP_PORT=3306 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DENABLED_LOCAL_INFILE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_DEBUG=0 -DENABLED_PROFILING=1
-DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_LIBWRAP=0
|
编译安装
注意:如果多次编译不成功的话,更改配置后记得rm CMakeCache.txt
清理临时文件
1 2 3
| wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.10.tar.gz tar -xvf mysql-5.7.10.tar.gz cd mysql-5.7.10
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_DATADIR=/data/mysql \ -DSYSCONFDIR=/etc \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ -DWITH_READLINE=1 \ -DWITH_SSL=system \ -DWITH_ZLIB=system \ -DWITH_LIBWRAP=0 \ -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \ -DDEFAULT_CHARSET=utf8 \ -DENABLE_DOWNLOADS=1 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_BOOST=/usr/local/boost # -DDOWNLOAD_BOOST=1 \ 如果没有boost-devel时可以加上该选项 make && make install
|
1
| chown -R mysql:mysql /usr/local/mysql/
|
配置和初始化
初始化
配置文件
关于配置文件的一点说明:
众所周知,MySQL
至关重要的配置文件就是my.cnf
。
该文件可以存在多个,MySQL
启动时候的搜寻路径如下:
/etc/my.cnf
/etc/mysql/my.cnf
/${MYSQL_HOME}/my.cnf
default-extra-file
~/my.cnf
对于配置文件搜寻顺序的说明:
- 即使是在第一个位置找到了配置文件,后续路径中的搜寻仍然会继续
- 如果找到多个配置文件,所有配置文件中的配置都将生效
- 如果在多个文件中找到了同一个配置,将使用最后一个配置
复制配置文件:
1 2 3 4 5 6 7 8 9
| cp support-files/my-default.cnf /etc/my.cnf
[mysqld] basedir =/usr/local/mysql datadir =/data/mysql port =3306
|
将MySQL
做成系统服务
1 2 3 4 5
| # support-files在安装目录和解压后的MySQL目录都有
cp support-files/mysql.server /etc/init.d/mysqld chkconfig chkconfig
|
将MySQL
加入环境变量
1 2 3 4 5 6 7 8 9
| vim /etc/profile.d/mysql.sh
export MYSQL_HOME=/usr/local/mysql export PATH=$PATH:$MYSQL_HOME/bin
source /etc/profile.d/mysql.sh
|
启动MySQL
注意,在这一步可能会遇到很多问题,请确保权限正常
chown -R mysql:mysql /data/mysql/
chown -R mysql:mysql /usr/local/mysql
chown -R mysql:mysql /usr/share/mysql/
做成系统服务后直接启动即可
1 2 3 4 5 6 7 8
| mysql -u root -p
set password=password('新密码');
flush privileges;
|
编译安装MariaDB
MariaDB
数据库管理系统是MySQL
的一个分支,主要由开源社区在维护,采用GPL
授权许可。
开发这个分支的原因之一是:甲骨文公司收购了MySQL
后,有将MySQL
闭源的潜在风险,因此社区采用分支的方式来避开这个风险
环境准备
源码包下载
选择自己需要的版本: https://downloads.mariadb.org/
这里使用:mariadb-10.2.16.tar.gz
编译环境及依赖关系
1 2
| yum groupinstall -y Development Tools yum -y install ncurses-devel zlib-devel
|
1 2
| wget https://cmake.org/files/v3.12/cmake-3.12.0-rc3.tar.gz tar -zxvf cmake-3.12.0-rc3.tar.gz -C /usr/local/
|
1 2
| cd /usr/local/cmake-3.12.0/ ./configure
|
安装MariaDB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| wget https://downloads.mariadb.org/interstitial/mariadb-10.2.16/source/mariadb-10.2.16.tar.gz tar -zxvf mariadb-10.2.16.tar.gz -C /usr/local/mysql
cd /usr/local/mysql
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DSYSCONFDIR=/etc \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_EXTRA_CHARSETS=all \
make && make install
|
创建mysql
用户
1 2
| useradd -M -s /sbin/nologin mysql chown mysql:root /usr/local/mysql/
|
MariaDB
配置文件创建及更改
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| cp support-files/my-medium.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
vim /etc/my.conf
datadir = /usr/local/mysql/data/
innodb_file_per_table = on
skip_name_resolve = on
|
设置Mysql
环境变量
1
| echo "export PATH=$PATH:/usr/local/mysql/bin">>/etc/profile
|
执行脚本初始化数据库
1
| /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/mydata/
|
安全初始化mysql及设定(前提先启动Mysql服务: service mysqld start
)
1
| /usr/local/mysql/bin/mysql_secure_installation
|
常见问题
virtual memory exhausted: Cannot allocate memory
发生该问题的原因是服务器的内存不够,从而导致编译失败。
而购买的Linux
服务器,未给你分配虚拟内存,所以可以通过自行增加虚拟内存的方法予以解决
1 2 3 4
| [root@izj6c6djex81rijczh0t8yz ~] total used free shared buff/cache available Mem: 991 142 664 2 184 696 Swap: 0 0 0
|
1 2 3 4 5 6 7
| [root@izj6c6djex81rijczh0t8yz ~]# mkdir /usr/img/ [root@izj6c6djex81rijczh0t8yz ~]# rm -rf /usr/img/swap [root@izj6c6djex81rijczh0t8yz ~]# dd if=/dev/zero of=/usr/img/swap bs=1024 count=2048000
2048000+0 records in 2048000+0 records out 2097152000 bytes (2.1 GB) copied, 36.0165 s, 58.2 MB/s
|
1 2 3 4 5 6 7 8 9 10 11
| [root@izj6c6djex81rijczh0t8yz ~] Setting up swapspace version 1, size = 2047996 KiB no label, UUID=b2d350d1-b5e4-427b-ae10-6a523121b6b9
[root@izj6c6djex81rijczh0t8yz ~] swapon: /usr/img/swap: insecure permissions 0644, 0600 suggested.
[root@izj6c6djex81rijczh0t8yz ~] total used free shared buff/cache available Mem: 991 144 67 2 779 688 Swap: 1999 0 1999
|
使用完毕后可以关掉swap
1 2
| [root@izj6c6djex81rijczh0t8yz ~]# swapoff swap [root@izj6c6djex81rijczh0t8yz ~]# rm -f /usr/img/swap
|
CMake Error at cmake/boost.cmake:81 (MESSAGE)
- 在/usr/local下创建一个名为boost的文件夹
1
| mkdir -p /usr/local/boost
|
1
| wget http://www.sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
|
1
| tar -xvzf boost_1_59_0.tar.gz
|
1
| -DWITH_BOOST=/usr/local/boost \
|
Starting MySQL..The server quit without updating PID file