一、搭建Nginx

1、解压压缩包

[root@localhost abc]# tar zxvf nginx-1.12.2.tar.gz -C /opt/

2、创建用户,并且不允许登录系统

[root@localhost abc]# useradd -M -s /sbin/nologin nginx

3、安装编译工具

[root@localhost abc]# yum install gcc gcc-c++ pcre-devel zlib-devel -y

4、进行编译安装

[root@localhost opt]# cd nginx-1.12.2/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module

安装

[root@localhost nginx-1.12.2]# make && make install

5、为了方便管理,可以给nginx创建一个软连接

[root@localhost nginx-1.12.2]#  ln -s /usr/local/nginx/sbin/* /usr/local/sbin/

6、编写一个脚本,可以用 systemctl 工具控制其开启、关闭、重载

root@localhost nginx-1.12.2]# cd /lib/systemd/
[root@localhost systemd]# cd system
[root@localhost system]# vim nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target

添加权限

root@localhost system]#  chmod 754 nginx.service 

7、启动Nginx,并关闭防火墙和增强性安全功能

[root@localhost system]# systemctl start nginx.service 
[root@localhost system]# systemctl stop firewalld.service
[root@localhost system]# setenforce 0

我们在客户机上进行访问
LNMP(Nginx+MySQL+PHP)_第1张图片

二、搭建MySQL

1、解压MySQL

[root@localhost abc]# tar zxvf mysql-boost-5.7.20.tar.gz -C /opt/

2、安装工具包

yum -y install \
ncurses \
ncurses-devel \
bison \
cmake

3、创建用户

[root@localhost abc]# useradd -s /sbin/nologin mysql

4、编译

[root@localhost abc]# cd /opt/
[root@localhost opt]# cd mysql-5.7.20/
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1

5、安装

make && make install 

6、设置权限

[root@localhost mysql-5.7.20]# cd /usr/local/
[root@localhost local]# chown -R mysql.mysql mysql/

7、修改配置文件

[root@localhost local]# vim /etc/my.cnf
[client]
port = 3306
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 = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1

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

8、配置环境变量
source /etc/profile

echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
echo 'export PATH' >> /etc/profile

启动

source /etc/profile

9、初始化数据库

[root@localhost mysql]# cd /usr/local/mysql/
bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data

10、在当前路下复制到 /lib/systemd/system/下,方便systemctl管理

cp usr/lib/systemd/system/mysqld.service /lib/systemd/system/

11、检查mysql服务是否启动

[root@localhost mysql]# systemctl start mysqld.service 

在这里插入图片描述
12、为mysql设置密码

mysqladmin -u root -p  password

登录数据库

mysql -u root -p

三、安装PHP

1、安装环境包

yum -y install \
libjpeg \
libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 \
libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel

2、解压PHP

[root@02 abc]# tar jxvf php-7.1.10.tar.bz2 -C /opt/

3、编译

[root@02 /]# cd /opt/php-7.1.10/
./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \                                   ###客户端支持库
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip

4、安装

[root@02 php-7.1.10]# make && make install

5、修改配置文件

cp php.ini-development /usr/local/php/lib/php.ini   //复制到lib路径下
root@02 php-7.1.10]# vim /usr/local/php/lib/php.ini
mysqli.default_socket = /usr/local/mysql/mysql.sock  //指定路径
date.timezone = Asia/Shanghai                        //指定时区

四、配置及优化FPM模块

1、配置FPM模块

cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf   //复制一份到 php-fpm.conf
vim php-fpm.conf    //进入文件
pid = run/php-fpm.pid    //开启pid(去掉行首的注释符即可)

cd /usr/local/php/etc/php-fpm.d/   
cp www.conf.default www.conf        //复制扩展包

启动PHP

/usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini

2、检测是否成功开启

netstat -napt | grep 9000

3、创建软连接以便系统识别

ln -s /usr/local/php/bin/* /usr/local/bin/

五、让Nginx支持PHP

root@localhost php-fpm.d]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf

  location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }

2、重新启动nginx服务

[root@localhost conf]# systemctl restart nginx.service 

3、创建PHP首页

vim /usr/local/nginx/html/index.php

LNMP(Nginx+MySQL+PHP)_第2张图片