之前有一篇博客写的是LAMP的环境搭建,今天来详细介绍一下另外一个模式LNMP=Linux+Nginx+MySQL+PHP。和LAMP不同的是,LNMP中的N指的是Nginx(类似于Apache的一种WEB服务软件),并且PHP是作为一个独立服务存在的,这个服务叫做php-fpm,Nginx直接处理静态请求,动态请求会转发给php-fpm。
目前LNMP环境的应用非常多。在静态页面的处理上,Nginx比Apache更强;但在动态页面的处理上,Nginx并不占优势。
一、目录
- 准备工作
- 安装Nginx服务
- 安装Mysql服务
- 搭建PHP运行环境
二、准备工作
1、服务器信息
操作系统 |
服务器IP |
CPU |
内存 |
CentOS 7.4 |
192.168.0.117 |
1C |
2G |
软件版本:
Nginx:1.13.4
MYSQL:5.7.22
PHP:7.2.9
2、下载软件包
#Nginx软件包
https://mirrors.yangxingzhen.com/nginx/nginx-1.13.4.tar.gz
#MYSQL软件包
https://mirrors.yangxingzhen.com/mysql/mysql-5.7.22.tar.gz
#MYSQL依赖包
https://mirrors.yangxingzhen.com/mysql/boost_1_59_0.tar.gz
#PHP软件包
https://mirrors.yangxingzhen.com/php/php-7.2.9.tar.gz
#PHP依赖包
https://mirrors.yangxingzhen.com/libiconv/libiconv-1.15.tar.gz
3、将防火墙与selinux关闭
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config
三、安装Nginx服务
1、下载软件包和安装编译工具及依赖包
[root@localhost ~]# yum -y install wget gcc gcc-c++ make pcre-devel pcre perl openssl openssl-devel
[root@localhost ~]# wget -c https://mirrors.yangxingzhen.com/nginx/nginx-1.13.4.tar.gz
2、解压软件包
[root@localhost ~]# tar zxf nginx-1.13.4.tar.gz
3、创建用户和隐藏版本号
[root@localhost nginx-1.13.4]# sed -i 's/1.13.4/ /;s/nginx///nginx/' src/core/nginx.h
[root@localhost nginx-1.13.4]# useradd -s /sbin/nologin www
4、预编译
[root@localhost ~]# cd nginx-1.13.4
[root@localhost nginx-1.13.4]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
5、编译及安装
[root@localhost nginx-1.13.4]# make && make install
6、创建软连接
[root@localhost nginx-1.13.4]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin
7、启动Nginx服务
[root@localhost nginx-1.13.4]# nginx -t
[root@localhost nginx-1.13.4]# nginx
8、访问测试
#浏览器访问服务器(192.168.0.117),如下图
四、安装MySQL服务
#MYSQL安装
1、下载boost库(MYSQL5.7及以上需要,必须安装)
[root@localhost ~]# yum -y install wget
[root@localhost ~]# wget -c https://mirrors.yangxingzhen.com/mysql/boost_1_59_0.tar.gz
[root@localhost ~]# tar zxf boost_1_59_0.tar.gz -C /usr/local/
2、下载依赖包
[root@localhost ~]# yum -y install ncurses ncurses-devel perl perl-devel gcc gcc-c++ cmake make openssl-devel openssl
3、建立MYSQL用户
[root@localhost ~]# useradd -s /sbin/nologin mysql
4、下载MYSQL-5.7.22源码包
[root@localhost ~]# wget -c https://mirrors.yangxingzhen.com/mysql/mysql-5.7.22.tar.gz
[root@localhost ~]# tar zxf mysql-5.7.22.tar.gz
[root@localhost ~]# cd mysql-5.7.22
[root@localhost mysql-5.7.22]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql /
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock /
-DMYSQL_DATADIR=/data/mysql /
-DSYSCONFDIR=/etc /
-DEXTRA_CHARSETS=all /
-DDEFAULT_CHARSET=utf8 /
-DDEFAULT_COLLATION=utf8_general_ci /
-DWITH_MYISAM_STORAGE_ENGINE=1 /
-DWITH_INNOBASE_STORAGE_ENGINE=1 /
-DWITH_MEMORY_STORAGE_ENGINE=1 /
-DWITH_PARTITION_STORAGE_ENGINE=1 /
-DDOWNLOAD_BOOST=1 /
-DWITH_BOOST=/usr/local/boost_1_59_0 /
-DENABLED_LOCAL_INFILE=1 /
-DMYSQL_TCP_PORT=3306 /
-DWITH_READLINE=1 /
-DMYSQL_USER=mysql /
-DWITH_SSL=yes
[root@localhost mysql-5.7.22]# make && make install
5、创建MYSQL数据存储目录
[root@localhost mysql-5.7.22]# mkdir -p /data/mysql
[root@localhost mysql-5.7.22]# chown -R mysql.mysql /data/mysql
6、编辑MYSQL主配置文件
[root@localhost mysql-5.7.22]# vim /etc/my.cnf
[client]
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /data/mysql
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
log-error = /tmp/mysqld.log
server-id = 1
log-bin =mysql
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
7、初始化数据库(生成空密码,生成随机密码--initialize)
[root@localhost mysql-5.7.22]# cd /usr/local/mysql/bin
[root@localhost bin]# ./mysqld --initialize-insecure --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql
8、配置成系统服务
[root@localhost bin]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost bin]# chmod o+x /etc/init.d/mysqld
[root@localhost bin]# chown -R mysql.mysql /usr/local/mysql
[root@localhost bin]# service mysqld start
[root@localhost bin]# chkconfig --add mysqld
[root@localhost bin]# chkconfig mysqld on
[root@localhost bin]# ln -s /usr/local/mysql/bin/* /usr/bin
[root@localhost bin]# mysql
#数据库安装配置成功,退出则输入"quit"
五、安装PHP运行环境
#安装libiconv
1、下载libiconv软件包
[root@localhost ~]# wget -c https://mirrors.yangxingzhen.com/libiconv/libiconv-1.15.tar.gz
2、解压libiconv软件包
[root@localhost ~]# tar zxf libiconv-1.15.tar.gz
[root@localhost ~]# cd libiconv-1.15
3、预编译
[root@localhost libiconv-1.15]# ./configure --prefix=/usr/local/libiconv
4、编译及安装
[root@localhost libiconv-1.15]# make && make install
#安装 PHP-7.2.9
1、安装编工具及插件(下载阿里云yum源)
[root@localhost ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
[root@localhost ~]# yum -y install gcc gcc-c++ libvpx libvpx-devel libjpeg-turbo libjpeg-turbo-devel libzip libpng libpng-devel freetype-devel libxml2 libxml2-devel gd gd-devel libxslt-devel libcurl-devel libmcrypt-devel mhash mcrypt openssl openssl-devel
2、解压PHP软件包
[root@localhost ~]# tar zxf php-7.2.9.tar.gz
[root@localhost ~]# cd php-7.2.9
3、预编译
[root@localhost php-7.2.9]# ln -s /usr/lib64/libldap* /usr/lib
[root@localhost php-7.2.9]# ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64
[root@localhost php-7.2.9.]# ./configure --prefix=/usr/local/php /
--with-config-file-path=/usr/local/php/etc /
--with-mysql=/usr/local/mysql /
--with-mysqli=/usr/local/mysql/bin/mysql_config /
--with-pdo-mysql=/usr/local/mysql /
--enable-opcache /
--with-iconv-dir=/usr/local/libiconv /
--with-freetype-dir /
--with-jpeg-dir /
--with-png-dir /
--with-zlib /
--with-libxml-dir /
--enable-xml /
--disable-rpath /
--enable-bcmath /
--enable-shmop /
--enable-sysvsem /
--enable-inline-optimization /
--with-curl /
--enable-mbregex /
--enable-fpm /
--enable-mbstring /
--with-mcrypt /
--with-gd /
--enable-gd-native-ttf /
--with-openssl /
--with-mhash /
--enable-pcntl /
--enable-sockets /
--with-xmlrpc /
--enable-soap /
--enable-short-tags /
--enable-static /
--with-fpm-user=www /
--with-fpm-group=www /
--enable-ftp /
--enable-zip /
--with-gettext
4、编译及安装
[root@localhost php-7.2.9.]# make && make install
4、配置PHP
[root@localhost php-7.2.9]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@localhost php-7.2.9]# cp php.ini-production /usr/local/php/etc/php.ini
[root@localhost php-7.2.9]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
[root@localhost php-7.2.9]# ln -s /usr/local/php/sbin/php-fpm /usr/bin
5、编辑PHP测试页面
[root@localhost php-7.2.9]# cd /usr/local/nginx/html
[root@localhost html]# vim index.php
<?php
phpinfo();
?>
6、配置Nginx
#编辑nginx.conf配置文件,内容如下
[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf
user www www;
worker_processes 1;
worker_cpu_affinity 00000001;
worker_rlimit_nofile 10240;
error_log /usr/local/nginx/logs/error.log;
pid /usr/local/nginx/logs/nginx.pid;
events {
use epoll;
worker_connections 10240;
multi_accept on;
}
http {
include mime.types;
default_type application/octet-stream;
access_log /usr/local/nginx/logs/access.log;
sendfile on;
tcp_nopush on;
keepalive_timeout 120;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
large_client_header_buffers 4 4k;
client_header_buffer_size 4k;
open_file_cache_valid 30s;
open_file_cache_min_uses 1;
include vhosts/*;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.php index.html index.htm;
}
location ~ /.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
#保存退出
7、重启Nginx服务,启动php-fpm服务
[root@localhost html]# nginx -s reload
[root@localhost html]# /usr/local/php/sbin/php-fpm
8、浏览器访问服务器:http://192.168.0.117
9、测试数据库
[root@localhosthtml]# mysql
mysql> create database test;
mysql> grant all on test.* to test@'%' identified by '123456';
mysql> flush privileges;
[root@localhost html]# vim info_mysql.php
<?php
$link=mysqli_connect('192.168.0.117','test','123456');
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
?>
10、访问测试
浏览器访问http://192.168.0.117/info_mysql.php
至此,Liunx环境LNMP环境搭建完毕。