lnmp 架构搭建

lnmp 架构

1.安装nginx

//配置yum源
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# yum  install  -y  wget
[root@localhost yum.repos.d]# wget  http://mirrors.163.com/.help/CentOS7-Base163.repo
……下载过程略
[root@localhost yum.repos.d]# sed -i 's/⧵$releasever/7/g' /etc/yum.repos.d/CentOS7-Base163.repo
[root@localhost yum.repos.d]# sed -i 's/^enabled=.*/enabled=1/g' /etc/yum.repos.d/CentOS7-Base-163.repo
[root@localhost yum.repos.d]# yum install -y epel-release

//创建系统用户nginx
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx

//安装依赖环境
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel
[root@localhost ~]# yum -y groups mark install 'Development Tools'

//创建日志存放目录
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx

//下载nginx
[root@localhost ~]# cd /usr/src/
[root@localhost src]# wget  http://nginx.org/download/nginx-1.14.0.tar.gz

//编译安装
[root@localhost src]# tar xf nginx-1.14.0.tar.gz
[root@localhost src]# cd nginx-1.14.0
[root@localhost nginx-1.14.0]#  ./configure  --prefix=/usr/local/nginx  --user=nginx  --group=nginx  
--with-debug   --with-http_ssl_module   --with-http_realip_module   --withhttp_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@localhost nginx-1.14.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) &&make install
……过程略

//配置
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' >/etc/profile.d/nginx.sh
[root@localhost ~]#  .  /etc/profile.d/nginx.sh
[root@localhost ~]# nginx
[root@localhost ~]# 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                              :::* 

2.安装mysql

//安装依赖包
[root@localhost ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-deve
……下载过程略

//创建用户与组
[root@localhost ~]# cd /usr/src/
[root@localhost src]# groupadd -r -g 306 mysql
[root@localhost src]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql

//下载mysql二进制安装包
[root@localhost src]# wget  https://downloads.mysql.com/archives/get/file/mysql5.7.22-linux-glibc2.12-x86_64.tar.gz
……下载过程略

//解压软件至/usr/local
[root@localhost src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@localhost src]# cd /usr/local/

//创建软连接
[root@localhost local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql

//修改目录/usr/local/mysql的属主属组
[root@localhost local]# chown -R mysql.mysql /usr/local/mysql

//添加环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' >/etc/profile.d/mysql.sh
[root@localhost ~]# source /etc/profile.d/mysql.sh

//建立数据存放目录
[root@localhost ~]# mkdir /opt/data
[root@localhost ~]# chown -R mysql.mysql /opt/data/

//初始化数据库 注意这个命令后会生成临时密码 要记住 !!!!
[root@lanzhiyong ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2018-08-15T07:57:46.168380Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-08-15T07:57:50.542516Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-08-15T07:57:50.927286Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-08-15T07:57:51.071260Z 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: e8600890-a060-11e8-b1a2-000c294c50b4.
2018-08-15T07:57:51.074566Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-08-15T07:57:51.078089Z 1 [Note] A temporary password is generatedfor root@localhost: hfGdwnViq6,,
// 请注意,这个命令的最后会生成一个临时密码,此处密码是hfGdwnViq6,,

//配置mysql
//创建软连接
[root@localhost ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
[root@localhost ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@localhost ~]# ldconfig -v

//生成配置文件
[root@localhost ~]# cat > /etc/my.cnf << EOF
> [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@localhost ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# sed -ri 's#^(basedir=).*#⧵1/usr/local/mysql#g' /etc/init.d/mysqld
[root@localhost ~]# sed -ri 's#^(datadir=).*#⧵1/opt/data#g' /etc/init.d/mysqld

//启动mysql
[root@localhost ~]# service mysqld start
[root@localhost ~]# ps -ef |grep mysql
[root@localhost ~]#ss -antl

//修改密码 使用临时密码登入然后修改密码!!!!
[root@localhost ~]# mysql -uroot  -p'hfGdwnViq6,,'
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> set password = password('12345');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye

//使用新密码登录
[root@localhost ~]# mysql -uroot -p12345
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

3.安装php

//安装依赖包
[root@localhost ~]# 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@localhost ~]# cd /usr/src/
[root@localhost src]# wget  http://cn.php.net/distributions/php-7.2.8.tar.xz

//编译安装php 
[root@localhost src]#  tar xf php-7.2.8.tar.xz
[root@localhost src]#  cd php-7.2.8
[root@localhost 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@localhost php-7.2.8]# make -j $(cat /proc/cpuinfo |grep processor |wc -l)
[root@localhost php-7.2.8]# make install
……过程略

//安装后配置
[root@localhost ~]# echo 'export PATH=/usr/local/php7/bin:$PATH' >/etc/profile.d/php7.sh
[root@localhost ~]# source /etc/profile.d/php7.sh
[root@localhost ~]# cd /usr/src/php-7.2.8
[root@localhost php-7.2.8]# which php
/usr/local/php7/bin/php
[root@localhost php-7.2.8]# php -v
PHP 7.2.8 (cli) (built: Aug 16 2018 13:27:30) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

//配置php-fpm
[root@localhost php-7.2.8]# cp php.ini-production /etc/php.ini
[root@localhost php-7.2.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-7.2.8]# chmod +x /etc/rc.d/init.d/php-fpm
[root@localhost php-7.2.8]# cp /usr/local/php7/etc/php-fpm.conf.default  /usr/local/php7/etc/php-fpm.conf
[root@localhost php-7.2.8]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default  /usr/local/php7/etc/php-fpm.d/www.conf

//配置fpm的相关选项为你所需的值:
[root@localhost ~]# vim /usr/local/php7/etc/php-fpm.conf
*****在配置文件最后面添加一下内容
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8

//启动php-fpm
[root@localhost ~]# service php-fpm start
Starting php-fpm done
[root@localhost ~]# ss -antl 
State       Recv-Q Send-Q              Local Address:Port                             Peer Address:Port              
LISTEN      0      128                             *:22                                          *:*                  
LISTEN      0      100                     127.0.0.1:25                                          *:*                  
LISTEN      0      128                     127.0.0.1:9000                                        *:*                  
LISTEN      0      128                            :::80                                         :::*                  
LISTEN      0      128                            :::22                                         :::*                  
LISTEN      0      100                           ::1:25                                         :::*                  
LISTEN      0      80                             :::3306                                       :::

4.配置nginx与php测试

//nginx配置文件/usr/local/nginx/conf/nginx.conf
//只要修改配置文件中的server{ } 配置块中的内容,修改location块,追加index.php让nginx服务器默认支持index.php为首页
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
…………………………………………
…………………………………………
server {
        listen 80;
        server_name localhost;
        #charset
        koi8-r;
        #access_log
        logs/host.access.log main;
        location / {
                root html;
                index index.html index.htm//后面添加 index.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 /scripts$fastcgi_script_name;
#主要修改的就是fastcgi_param中的/scripts为$document_root
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root

//修改配置文件后重新加载nginx
[root@localhost ~]# nginx  -s  reload

//创建测试php程序,在/usr/local/nginx/html/index.php,打印一下php配置
[root@localhost ~]# cat > /usr/local/nginx/html/index.php << EOF
>  phpinfo();
> ?>
> EOF

最后打开浏览器输入 192.168.206.129/index.php 看到输出的页面,说明nginx和php都配置
成功了

lnmp 架构搭建_第1张图片