环境准备

'关闭防火墙和selinux'
[root@arongya ~]# systemctl stop firewalld
[root@arongya ~]# systemctl disable firewalld
[root@arongya ~]# setenforce 0
[root@arongya ~]# sed -ri 's/(SELINUX=).*/\1disabled/g' /etc/selinux/config

配置nginx

'创建系统组和用户'
[root@arongya ~]# groupadd -r nginx
[root@arongya ~]# useradd -M -s /sbin/nologin -g nginx nginx

'安装依赖包'
[root@arongya ~]# yum -y install pcre-devel openssl openssl-devel gd-devel

[root@arongya ~]# yum -y groups mark install 'Development Tools'

'检查安装Development Tools 是否安装成功'
[root@arongya ~]# yum grouplist

'创建日志存放目录以及更改存放日志目录属组和属主'
[root@arongya ~]# mkdir -p /var/log/nginx
[root@arongya ~]# chown -R nginx.nginx /var/log/nginx

'下载nginx'
[root@arongya ~]# cd /usr/src/
[root@arongya src]# yum install wget
[root@arongya src]# wget http://nginx.org/download/nginx-1.14.0.tar.gz
[root@arongya src]# ls
debug  kernels  nginx-1.14.0.tar.gz

'解压'
[root@arongya src]# tar xf nginx-1.14.0.tar.gz 

'编译安装'
[root@arongya src]# cd nginx-1.14.0/
[root@arongya nginx-1.14.0]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-debug \
> --with-http_ssl_module \
> --with-http_realip_module \
> --with-http_image_filter_module \
> --with-http_gunzip_module \
> --with-http_gzip_static_module \
> --with-http_stub_status_module \
> --http-log-path=/var/log/nginx/access.log \
> --error-log-path=/var/log/nginx/error.log
[root@arongya nginx-1.14.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

nginx安装后配置

'配置环境变量'
[root@arongya ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@arongya ~]# . /etc/profile.d/nginx.sh

'启动nginx'
[root@arongya ~]# nginx 
[root@arongya ~]# ss -antl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128     *:80                  *:*                  
LISTEN      0      128     *:22                  *:*                  
LISTEN      0      100    127.0.0.1:25                  *:*                  
LISTEN      0      128    :::22                 :::*                  
LISTEN      0      100       ::1:25                 :::*                  

在浏览器上输入本主机的IP地址看是否能访问,例如本主机IP是:192.168.228.30,如图所示:
LNMP架构搭建_第1张图片


安装MYSQL

'安装依赖包'
[root@arongya ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel

'创建mysql的系统用户和组'
[root@arongya ~]# groupadd -r -g 306 mysql
[root@arongya ~]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql

'下载二进制格式的mysql的软件包'
[root@arongya ~]# cd /usr/src/
[root@arongya src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

'解压软件至/usr/local/'
[root@arongya src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@arongya src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@arongya src]# ls /usr/local/
bin    include  libexec                              sbin
etc    lib      mysql-5.7.22-linux-glibc2.12-x86_64  share
games  lib64    nginx                                src
[root@arongya src]# cd /usr/local/
[root@arongya local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
‘mysql’ -> ‘mysql-5.7.22-linux-glibc2.12-x86_64/’
[root@arongya local]# ll
total 0
drwxr-xr-x.  2 root root   6 Nov  5  2016 bin
drwxr-xr-x.  2 root root   6 Nov  5  2016 etc
drwxr-xr-x.  2 root root   6 Nov  5  2016 games
drwxr-xr-x.  2 root root   6 Nov  5  2016 include
drwxr-xr-x.  2 root root   6 Nov  5  2016 lib
drwxr-xr-x.  2 root root   6 Nov  5  2016 lib64
drwxr-xr-x.  2 root root   6 Nov  5  2016 libexec
lrwxrwxrwx.  1 root root  36 Aug 24 16:29 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
drwxr-xr-x.  9 root root 129 Aug 24 16:28 mysql-5.7.22-linux-glibc2.12-x86_64

'修改目录/usr/local/mysql的属主属组'
[root@arongya ~]# chown -R mysql.mysql /usr/local/mysql
[root@arongya ~]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 Aug 24 16:29 /usr/local/mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/

'添加环境变量'
[root@arongya ~]# ls /usr/local/mysql
bin      docs     lib  README  support-files
COPYING  include  man  share
[root@arongya ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@arongya ~]# . /etc/profile.d/mysql.sh 
[root@arongya ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

'建立数据存放目录'
[root@arongya ~]# mkdir /opt/data
[root@arongya ~]# chown -R mysql.mysql /opt/data/
[root@arongya ~]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Aug 24 16:33 data

'初始化数据库'
[root@arongya ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2018-08-24T08:34:51.043748Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-08-24T08:34:52.915665Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-08-24T08:34:53.347600Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-08-24T08:34:53.418106Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 92b6fee8-a778-11e8-8c4e-000c29b52e8f.
2018-08-24T08:34:53.540420Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-08-24T08:34:53.544447Z 1 [Note] A temporary password is generated for root@localhost: gz(Ist:C;6_6
'这个命令最后会生成一个临时密码,此处密码是gz(Ist:C;6_6'

'配置mysql'
[root@arongya ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
‘/usr/local/include/mysql’ -> ‘/usr/local/mysql/include/’
[root@arongya ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@arongya ~]# ldconfig -v

'生成配置文件'
[root@arongya ~]# cat > /etc/my.cnf < [mysqld]
> basedir = /usr/local/mysql
> datadir = /opt/data
> socket = /tmp/mysql.sock
> port = 3306
> pid-file = /opt/data/mysql.pid
> user = mysql
> skip-name-resolve
> EOF

'配置服务启动脚本'
[root@arongya ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@arongya ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@arongya ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld 

'启动mysql'
[root@arongya ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/arongya.err'.
. SUCCESS! 
[root@arongya ~]# ps -ef |grep mysql
root       4829      1  0 16:44 pts/1    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql      5007   4829  3 16:44 pts/1    00:00:00 /usr/localmysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=arongya.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root       5039   1760  0 16:44 pts/1    00:00:00 grep --color=auto mysql
[root@arongya ~]# ss -antl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128     *:80                  *:*                  
LISTEN      0      128     *:22                  *:*                  
LISTEN      0      100    127.0.0.1:25                  *:*                  
LISTEN      0      128    :::22                 :::*                  
LISTEN      0      100       ::1:25                 :::*                  
LISTEN      0      80     :::3306               :::*                  

'修改密码(使用临时密码登录)'
root@arongya ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22
'设置新密码'
mysql> set password = password('yaoxiaorong!');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye

安装php

'配置yum源'
[root@arongya ~]# cd /etc/yum.repos.d/
[root@arongya yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
--2018-08-24 16:48:23--  http://mirrors.163.com/.help/CentOS7-Base-163.repo
Resolving mirrors.163.com (mirrors.163.com)... 59.111.0.251
Connecting to mirrors.163.com (mirrors.163.com)|59.111.0.251|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1572 (1.5K) [application/octet-stream]
Saving to: ‘CentOS7-Base-163.repo’

100%[===================>] 1,572       --.-K/s   in 0s      

2018-08-24 16:48:23 (46.9 MB/s) - ‘CentOS7-Base-163.repo’ saved [1572/1572]

[root@arongya yum.repos.d]# sed -i 's/\$releasever/7/g' /etc/yum.repos.d/CentOS7-Base-163.repo 
[root@arongya yum.repos.d]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo 
[root@arongya yum.repos.d]# yum -y install epel-release
'安装依赖包'
[root@arongya ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel
'下载php'
[root@arongya yum.repos.d]# cd /usr/src/
[root@arongya src]# wget http://cn.php.net/distributions/php-7.2.8.tar.xz

'编译安装php'
[root@arongya php-7.2.8]# tar xf php-7.2.8.tar.xz 
[root@arongya php-7.2.8]# cd php-7.2.8/
[root@arongya php-7.2.8]# ./configure --prefix=/usr/local/php7 \
> --with-curl \
> --with-freetype-dir \
> --with-gd \
> --with-gettext \
> --with-iconv-dir \
> --with-kerberos \
> --with-libdir=lib64 \
> --with-libxml-dir=/usr \
> --with-mysqli=/usr/local/mysql/bin/mysql_config \
> --with-openssl \
> --with-pcre-regex \
> --with-pdo-mysql \
> --with-pdo-sqlite \
> --with-pear \
> --with-jpeg-dir \
> --with-png-dir \
> --with-xmlrpc \
> --with-xsl \
> --with-zlib \
> --with-config-file-path=/etc \
> --with-config-file-scan-dir=/etc/php.d \
> --with-bz2 \
> --enable-fpm \
> --enable-bcmath \
> --enable-libxml \
> --enable-inline-optimization \
> --enable-mbregex \
> --enable-mbstring \
> --enable-opcache \
> --enable-pcntl \
> --enable-shmop \
> --enable-soap \
> --enable-sockets \
> --enable-sysvsem \
> --enable-xml \
> --enable-zip
[root@arongya php-7.2.8]# make -j 2 && make install
'安装后配置'
[root@arongya ~]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@arongya ~]# source /etc/profile.d/php7.sh 
[root@arongya ~]# php -v
HP 7.2.8 (cli) (built: Aug 24 2018 17:37:56) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Te
'配置php-fpm'
[root@arongya php-7.2.8]# cp php.ini-production /etc/php.ini
[root@arongya php-7.2.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@arongya php-7.2.8]# chmod +x /etc/rc.d/init.d/php-fpm 
[root@arongya php-7.2.8]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@arongya php-7.2.8]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf

'配置php-fpm的配置文件(/usr/local/php7/etc/php-fpm.conf),配置fpm的相关选项为你所需要的值'
'配置文件的最后一行'
root@arongya php-7.2.8]# vim /usr/local/php7/etc/php-fpm.conf

pm.max_children = 50        '最多同时提供50个进程提供50个并发服务'
pm.start_servers = 5      '启动时启动5个进程'
pm.min_spare_servers =  2     '最小空闲进程数'
pm.max_spare_servers = 8      '最大空闲进程数'

'启动php-fpm'
[root@arongya php-7.2.8]# service php-fpm start
Starting php-fpm  done
'默认情况下,fpm监听在127.0.0.1的9000端口,也可以使用如下命令验证其是否已经监听在相应的套接字'
[root@arongya php-7.2.8]# ss -antl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128     *:80                  *:*                  
LISTEN      0      128     *:22                  *:*                  
LISTEN      0      100    127.0.0.1:25                  *:*                  
LISTEN      0      128    127.0.0.1:9000                *:*                  
LISTEN      0      128    :::22                 :::*                  
LISTEN      0      100       ::1:25                 :::*                  
LISTEN      0      80     :::3306               :::*                  
[root@arongya php-7.2.8]# ps -ef |grep php
root      58499      1  0 18:51 ?        00:00:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf)
nobody    58500  58499  0 18:51 ?        00:00:00 php-fpm: pool www
nobody    58501  58499  0 18:51 ?        00:00:00 php-fpm: pool www
nobody    58502  58499  0 18:51 ?        00:00:00 php-fpm: pool www
nobody    58503  58499  0 18:51 ?        00:00:00 php-fpm: pool www
nobody    58504  58499  0 18:51 ?        00:00:00 php-fpm: pool www
root      58508  26795  0 18:52 pts/0    00:00:00 grep --color=auto php

配置nginx

'编辑nginx配置文件/usr/local/nginx/conf/nginx.conf,将index.php添加在index.htm后面'
    server {
        listen       80;      '搜索listen,就会查找到以下内容'
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm  index.php;  '添加在此'

'然后配置.php请求被传送到后端的php-fpm模块,默认情况下php配置块是被注释的,去掉注释'

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {      '从以下行的注释#去掉'
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;   '将fastcgi_param中的/scripts修改成$document'
            include        fastcgi_params;
        }
最后保存并退出
'重新加载nginx'
root@arongya ~]# nginx -s reload
[root@arongya ~]# ps -ef | grep nginx
root       4621      1  0 17:12 ?        00:00:00 nginx: master process nginx
nginx     58569   4621  0 19:09 ?        00:00:00 nginx: worker process
root      58571  26795  0 19:10 pts/0    00:00:00 grep --color=auto nginx

测试PHP程序

'在/usr/local/nginx/html目录下创建yxr.php文件并打印配置php'
[root@arongya ~]# cd /usr/local/nginx/html/
[root@arongya html]# touch yxr.php
[root@arongya html]# cat > yxr.php << EOF
>    phpinfo();
> ?>
> EOF

在浏览器输入192.168.228.30/yxr.php进行访问,如图就是访问成功
LNMP架构搭建_第2张图片