LNMP搭建
LNMP之nginx安装
编译安装
1、准备yum仓库
:~# cd /etc/yum.repos.d/
:~# wget http://mirrors.aliyun.com/repo/epel-6.repo
:~# wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
2、编译安装的基础环境
:~# yum install -y vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net- tools iotop bc zip unzip zlib-devel bash-completion nfs-utils automake libxml2 libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed
3、下载二进制编译包并编译安装
:~# useradd nginx
:~# cd /usr/src/
:~# wget -p https://nginx.org/download/nginx-1.16.1.tar.gz
:~# tar xf nginx-1.16.1.tar.gz
:~# cd nginx-1.16.1/
:~# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
:~# make
:~# make install
4、验证版本及编译参数
:~# /usr/local/sbin/nginx -V
5、启动nginx
:~# /usr/local/nginx/sbin/nginx
LNMP之MySQL安装
二进制安装
1、获取二进制包
:~# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
2、创建mysql用户
:~# groupadd mysql
:~# useradd -s /sbin/nologin -g mysql -M mysql
3、执行安装
:~# tar xf mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz -C /usr/local
:~# mv /usr/local/mysql-5.6.46-linux-glibc2.12-x86_64 /usr/local/mysql
注:二进制包直接解压即可,无需再进行make、make install等操作
4、初始化MySQL配置文件my.cnf
:~# /bin/cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
5、初始化数据库文件
图一:~# mkdir -p /usr/local/mysql/data
:~# chown -R mysql.mysql /usr/local/mysql
:~# yum -y install libaio autoconf
:~# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
注:初始化内容如下图一
6、启动MySQL数据库
:~# /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
:~# chmod +x /etc/init.d/mysqld
:~# /etc/init.d/mysqld start
:~# ps -ef | grep mysql
7、mysql登录测试
图二:~# ln -s /usr/local/mysql/bin/* /usr/local/bin/
:~# which mysql
:~# mysql
注:登录结果如下图二
8、设置数据库登录密码
:~# mysqladmin -u root password '123123'
LNMP之PHP安装
编译安装
1、安装依赖包
图三:~# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
:~# yum -y install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
:~# yum -y install freetype-devel libpng-devel gd libcurl-devel libxslt-devel
:~# wget http://mirrors.hust.edu.cn/epel/6/x86_64/Packages/l/libmcrypt-2.5.8-9.el6.x86_64.rpm
:~# wget http://mirrors.hust.edu.cn/epel/6/x86_64/Packages/l/libmcrypt-devel-2.5.8-9.el6.x86_64.rpm
:~# yum -y install libmcrypt-devel mhash mcrypt
:~# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
:~# tar xf libiconv-1.14.tar.gz -C /usr/src/
:~# cd /usr/src/libiconv-1.14/
:~# ./configure--prefix=/usr/local/libiconv && make && make install
注:编译过程中如出现以下图三所示错误,执行以下步骤
:~# cd libiconv-1.14/srclib/
:~# sed -i -e '/gets is a security/d' ./stdio.in.h
2、获取PHP安装包
官网下载:http://php.net/downloads
3、执行安装
图四 图五:~# tar xf php-7.2.0.tar.gz -C /usr/src
:~# cd /usr/src/php-7.2.0
:~# ./configure --prefix=/usr/local/php --with-pdo-mysql=/usr/local/mysql --with-iconv-dir=/usr/local/libiconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-fpm --enable-mbstring --with-gd --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-short-tags --enable-static --with-xsl --with-fpm-user=nginx --with-fpm-group=nginx --enable-ftp
:~# ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/
:~# touch ext/phar/phar.phar
:~# make
注:执行后结果如下图四:~# make install
注:执行后结果如下图五
4、配置PHP配置文件
:~# cp /usr/src/php-7.2.0/php.ini-production /usr/local/php/lib/php.ini
:~# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
:~# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
5、启动PHP
:~# /usr/local/php/sbin/php-fpm
:~# ps -ef | grep php-fpm
配置NGINX支持PHP访问
1、配置nginx配置文件
在server中添加以下内容
location ~ .*\.(php|php5)?$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME html/$fastcgi_script_name;
include fastcgi.conf;
}
2、重新启动nginx
:~# /usr/local/nginx/sbin/nginx -t
:~# /usr/local/nginx/sbin/nginx -s reload
测试LNMP环境是否生效
1、测试PHP
1.1、创建一个index.php文件,写入以下内容
:~# vim /usr/local/nginx/html/index.php
<?php phpinfo(); ?>
1.2、测试结果如下图六说明PHP解析成功
图六2、测试PHP连接MySQL
2.1、创建一个index_mysql.php文件,写入以下内容
vim /usr/local/nginx/html/index_mysql.php
<?php
$servername = "localhost";
$username = "root";
$password = "666666";
try {
$conn = new PDO("mysql:host=$servername;", $username, $password);
echo "连接成功";
}
catch(PDOException $e){
echo $e->getMessage();
}
?>
2.2、测试结果如下图七说明数据库连接成功
图七