CentOS7.2编译配置LNMP环境(MySQL5.7.20,PHP7.0.24)
一, 查看系统版本及内核版本
二, 编译安装nginx
1, 新建nginx用户
useradd -s /sbin/nologin -M nginx
2, 安装支持包
yum -y install gcc pcre pcre-devel openssl-devel
3, 下载nginx并且解压
wget http://nginx.org/download/nginx-1.10.3.tar.gz
tar -xf nginx-1.10.3.tar.gz
4, 进入解压目录编译安装
./configure --with-http_ssl_module --with-http_stub_status_module --user=nginx --group=nginx --prefix=/usr/local/nginx-1.10.3
make && make install
5, 建立软链接
ln -s /usr/local/nginx-1.10.3/ /usr/local/nginx
6, 关闭防火墙及senlinux
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed 's#enforcing#disabled#g' /etc/selinux/config -i
7, 启动nginx
/usr/local/nginx/sbin/nginx
8, 验证是否启动成功
在页面输入服务器IP地址出现以下页面代表安装nginx启动成功
三, 使用rpm包安装mysql5.7.20
从MySQL官网下载mysql-5.7.20-1.el7.x86_64.rpm-bundle.tar
1, 解压
tar –xf mysql-5.7.20-1.el7.x86_64.rpm-bundle.tar
2, 移除老版本mysql-libs(此步骤必须操作否则rpm安装时候报错)
yum remove mysql-libs
3, 按顺序安装以下解压的rpm包
rpm -ivh mysql-community-common-5.7.20-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.20-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.20-1.el7.x86_64.rpm
4, 安装server依赖包
yum install perl
yum install numactl
yum install libaio
yum -y install net-tools
5, 安装mysql server
rpm -ivh mysql-community-server-5.7.20-1.el7.x86_64.rpm
6, 启动mysql
systemctl start mysqld
7, 登录mysql
第一次登录初始密码在以下文件
cat /var/log/mysqld.log |grep password
mysql –u root –p
输入初始密码
8, 修改密码(密码必须满足一定复杂度要求否则修改报错)
mysql> SET PASSWORD = PASSWORD('Mysql123456!');
四,编译安装php7.0.27
1, php官网下载php7.0.27
2, 安装依赖包
yum -y install freetype-devel libjpeg-turbo-devel libpng-devel gd-devel libcurl-devel libxslt-devel
3, 编译
./configure --prefix=/usr/local/php7.0.27 --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr/ --enable-xml --disable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-short-tags --enable-zend-multibyte --enable-static --with-xsl --with-fpm-user=nginx --with-fpm-group=nginx --enable-ftp --enable-xml
4, 安装
make
make test
make install
5, 建立软链接
ln -s /usr/local/php7.0.27/ /usr/local/php
6, 拷贝配置文件
cp php.ini-production /usr/local/php/lib/php.ini
7, 修改默认配置文件名
cd /usr/local/php/etc
cp php-fpm.conf.default php-fpm.conf
cd /usr/local/php/etc/php-fpm.d/
mv www.conf.default www.conf
8, 启动php-fpm进程
/usr/local/php/sbin/php-fpm
查看端口9000是否启动
lsof –i:9000
四, 配置php和nginx结合
1, 修改nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
在server里面加入以下代码
location ~ .*\.(php|php5)?$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
2, 新建站点根目录测试页面
vim /usr/local/nginx/html/test.php
内容为
<?php
phpinfo();
?>
3,重启nginx然后在浏览器输入地址加test.php出现以下界面代表成功
/usr/local/nginx/sbin/nginx -s reload
五, 安装mysql扩展
1, 去php网站上下载mysql扩展,http://git.php.net/?p=pecl/database/mysql.git;a=summary
2, 解压并且使用phpize工具初始化
tar -xf mysql-45881bd.tar.gz
cd mysql-45881bd
yum -y install autoconf
/usr/local/php/bin/phpize
./configure --with-php-config=/opt/php/bin/php-config --with-mysql=mysqlnd
Make && make install
3, 编辑php.ini文档在尾部添加
vim /usr/local/php/lib/php.ini
extension = mysql.so
4, 重启php-fpm
pkill php-fpm
/usr/local/php/sbin/php-fpm
测试Mysql扩展是否成功
vim /usr/local/nginx/html/mysql.php
<?php
$mysqli = new mysqli("localhost", "root", "Mysql123456!");
if(!$mysqli) {
echo"database error";
}else{
echo"MySQL successful";
}
$mysqli->close();
?>
在页面输入地址访问http://192.168.0.224/mysql.php