LNMP之nginx安裝
編譯安裝
1、準(zhǔn)備yum倉庫
:~#? wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
2、編譯安裝的基礎(chǔ)環(huán)境
:~#??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、下載二進(jìn)制編譯包并編譯安裝
:~#? 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、驗證版本及編譯參數(shù)
:~#? /usr/local/nginx/sbin/nginx? -V
5、啟動nginx
:~#? /usr/local/nginx/sbin/nginx
LNMP之MySQL安裝
二進(jìn)制安裝
1、獲取二進(jìn)制包
:~#? ?wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.6.46-linux-glibc2.12-x86_64.tar.gz
2、創(chuàng)建mysql用戶
:~#? groupadd mysql
:~#? useradd -s /sbin/nologin -g mysql -M mysql
3、執(zhí)行安裝
:~#? 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
注:二進(jìn)制包直接解壓即可,無需再進(jìn)行make、make? install等操作
4、初始化MySQL配置文件my.cnf
:~#??/bin/cp??/usr/local/mysql/support-files/my-default.cnf? /etc/my.cnf
5、初始化數(shù)據(jù)庫文件
:~#??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
注:初始化內(nèi)容如下圖一

6、啟動MySQL數(shù)據(jù)庫
:~#??/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
注:登錄結(jié)果如下圖二

8、設(shè)置數(shù)據(jù)庫登錄密碼
:~#??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
注:編譯過程中如出現(xiàn)以下圖三所示錯誤,執(zhí)行以下步驟
:~#? cd??libiconv-1.14/srclib/
:~#? sed -i -e '/gets is a security/d' ./stdio.in.h

2、獲取PHP安裝包
官網(wǎng)下載:http://php.net/downloads
3、執(zhí)行安裝
:~#??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
注:執(zhí)行后結(jié)果如下圖四:~#? make? install
注:執(zhí)行后結(jié)果如下圖五


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中添加以下內(nèi)容
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環(huán)境是否生效
1、測試PHP
1.1、創(chuàng)建一個index.php文件,寫入以下內(nèi)容
:~#??vim??/usr/local/nginx/html/index.php?
<?php phpinfo(); ?>?
1.2、測試結(jié)果如下圖六說明PHP解析成功

2、測試PHP連接MySQL
2.1、創(chuàng)建一個index_mysql.php文件,寫入以下內(nèi)容
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、測試結(jié)果如下圖七說明數(shù)據(jù)庫連接成功
