码迷,mamicode.com
首页 > 其他好文 > 详细

LNMP架构搭建详细部署

时间:2018-08-27 12:40:01      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:free   mirrors   防火   rem   sed   smt   ase   col   filter   

LNMP简介
LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。

Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统。代表版本有:debian、centos、ubuntu、fedora、gentoo等。

Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。

Mysql是一个小型关系型数据库管理系统。

PHP是一种在服务器端执行的嵌入HTML文档的脚本语言。

这四种软件均为免费、高效、扩展性强的网站服务系统。

IP 系统 需要安装的程序
192.168.47.12 centos7 NGINX MYSQL PHP
环境准备
关闭防火墙以SELINUX
[root@yanyinglai3 ~]# systemctl stop firewalld
[root@yanyinglai3 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@yanyinglai3 ~]# sed -ri ‘s/(SELINUX=).*/\1disabled/g‘ /etc/selinux/config
[root@yanyinglai3 ~]# setenforce 0

1.安装nginx
创建系统用户
[root@yanyinglai3 ~]#  groupadd -r nginx
[root@yanyinglai3 ~]#  useradd -r -M -s /sbin/nologin -g nginx nginx

配置yum源
[root@yanyinglai3 ~]# cd /etc/yum.repos.d/
[root@yanyinglai3 yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@yanyinglai3 yum.repos.d]# sed -i ‘s/\$releasever/7/g‘ /etc/yum.repos.d/CentOS7-Base-163.repo
[root@yanyinglai3 yum.repos.d]#  sed -i ‘s/^enabled=.*/enabled=1/g‘ /etc/yum.repos.d/CentOS7-Base-163.repo
[root@yanyinglai3 yum.repos.d]# yum -y install epel-release

安装依赖环境
 [root@yanyinglai3 ~]#  yum -y install pcre-devel openssl openssl-devel gd-devel  

[root@yanyinglai3 ~]# yum -y groups install ‘Development Tools‘

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

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

[root@yanyinglai3 src]# ls
debug  kernels  nginx-1.14.0.tar.gz

编译安装
[root@yanyinglai3 src]# tar xf nginx-1.14.0.tar.gz
[root@yanyinglai3 src]# cd nginx-1.14.0
[root@yanyinglai3 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@yanyinglai3 nginx-1.14.0]# make -j $(grep ‘processor‘ /proc/cpuinfo | wc -l) && make install

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

启动nginx
[root@yanyinglai3 ~]# nginx
[root@yanyinglai3 ~]# 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@yanyinglai3 ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel

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

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

解压软件至/usr/local/
[root@yanyinglai3 src]# ls
debug    mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz  nginx-1.14.0.tar.gz
kernels  nginx-1.14.0

[root@yanyinglai3 src]#  tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@yanyinglai3 src]# ls  /usr/local/
bin  games    lib    libexec                              nginx  share
etc  include  lib64  mysql-5.7.22-linux-glibc2.12-x86_64  sbin   src

[root@yanyinglai3 src]# cd  /usr/local/
[root@yanyinglai3 local]#  ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.22-linux-glibc2.12-x86_64/"

[root@yanyinglai3 local]# ll
总用量 0
drwxr-xr-x.  2 root root   6 11月  5 2016 bin
drwxr-xr-x.  2 root root   6 11月  5 2016 etc
drwxr-xr-x.  2 root root   6 11月  5 2016 games
drwxr-xr-x.  2 root root   6 11月  5 2016 include
drwxr-xr-x.  2 root root   6 11月  5 2016 lib
drwxr-xr-x.  2 root root   6 11月  5 2016 lib64
drwxr-xr-x.  2 root root   6 11月  5 2016 libexec
lrwxrwxrwx.  1 root root  36 8月  25 19:06 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/
drwxr-xr-x.  9 root root 129 8月  25 19:04 mysql-5.7.22-linux-glibc2.12-x86_64
drwxr-xr-x. 11 root root 151 8月  25 18:35 nginx
drwxr-xr-x.  2 root root   6 11月  5 2016 sbin
drwxr-xr-x.  5 root root  49 7月  30 17:17 share
drwxr-xr-x.  2 root root   6 11月  5 2016 src

修改目录/usr/locaal/mysql的属主属组
[root@yanyinglai3 ~]#  chown -R mysql.mysql /usr/local/mysql
[root@yanyinglai3 ~]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 8月  25 19:06 /usr/local/mysql -> mysql-5.7.22-linux-glibc2.12-x86_64/

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

建立数据存放目录
[root@yanyinglai3 ~]# cd /usr/local/mysql
[root@yanyinglai3 mysql]# mkdir /opt/data
[root@yanyinglai3 mysql]# chown -R mysql.mysql /opt/data/
[root@yanyinglai3 mysql]#  ll /opt/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 8月  25 19:12 data

初始化数据库
[root@yanyinglai3 mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
//这个命令的最后会生成一个临时密码,此处密码是#Ysf:iDUu37J

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

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

启动mysql
[root@yanyinglai3 ~]# service mysqld start
Starting MySQL.Logging to ‘/opt/data/yanyinglai3.err‘.
. SUCCESS!

[root@yanyinglai3 ~]# ps -ef|grep mysql
root      47944      1  0 19:31 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql     48122  47944  1 19:31 pts/0    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=yanyinglai3.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root      48154   1705  0 19:32 pts/0    00:00:00 grep --color=auto mysq

[root@yanyinglai3 ~]# 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@yanyinglai3 ~]#  mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.22
mysql>  set password = password(‘123456‘);
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye

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

编译安装php
[root@yanyinglai3 src]# ls
debug    mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz  nginx-1.14.0.tar.gz
kernels  nginx-1.14.0                                php-7.2.8.tar.xz
[root@yanyinglai3 src]# tar xf php-7.2.8.tar.xz
[root@yanyinglai3 src]# cd php-7.2.8
[root@yanyinglai3 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@yanyinglai3 php-7.2.8]#  make -j $(cat /proc/cpuinfo |grep processor|wc -l)

[root@yanyinglai3 php-7.2.8]# make install

安装后配置
[root@yanyinglai3 ~]# echo ‘export PATH=/usr/local/php7/bin:$PATH‘ > /etc/profile.d/php7.sh
[root@yanyinglai3 ~]# source /etc/profile.d/php7.sh
[root@yanyinglai3 ~]#  which php
/usr/local/php7/bin/php
[root@yanyinglai3 ~]# php -v
PHP 7.2.8 (cli) (built: Aug 25 2018 20:34:32) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

配置php-fpm
[root@yanyinglai3 php-7.2.8]# cp php.ini-production /etc/php.ini
[root@yanyinglai3 php-7.2.8]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@yanyinglai3 php-7.2.8]# chmod +x /etc/rc.d/init.d/php-fpm
[root@yanyinglai3 php-7.2.8]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@yanyinglai3 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@yanyinglai3 ~]# vi /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  //最大空闲进程数

[root@yanyinglai3 ~]#  tail /usr/local/php7/etc/php-fpm.conf
; files from a glob(3) pattern. This directive can be used everywhere in the
; file.
; Relative path can also be used. They will be prefixed by:
;  - the global prefix if it‘s been set (-p argument)
;  - /usr/local/php7 otherwise
include=/usr/local/php7/etc/php-fpm.d/*.conf
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8

启动php-fpm
[root@yanyinglai3 ~]# service php-fpm start
Starting php-fpm  done
[root@yanyinglai3 ~]#  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@yanyinglai3 ~]# ps -ef|grep php
root      69346      1  0 21:02 ?        00:00:00 php-fpm: master process (/usr/local/php7/etc/php-fpm.conf)
nobody    69347  69346  0 21:02 ?        00:00:00 php-fpm: pool www
nobody    69348  69346  0 21:02 ?        00:00:00 php-fpm: pool www
nobody    69349  69346  0 21:02 ?        00:00:00 php-fpm: pool www
nobody    69350  69346  0 21:02 ?        00:00:00 php-fpm: pool www
nobody    69351  69346  0 21:02 ?        00:00:00 php-fpm: pool www
root      69354   1705  0 21:03 pts/0    00:00:00 grep --color=auto php

4.配置nginx
编辑nginx配置文件/usr/local/nginx/conf/nginx.conf,主要修改nginx的server {}配置块中的内容,修改location块,追加index.php让nginx服务器默认支持index.php为首页:
[root@yanyinglai3 ~]#  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;
        }        

   然后配置.php请求被传送到后端的php-fpm模块,默认情况下php配置块是被注释的,此时去掉注释并修改为以下内容:
   [root@yanyinglai3 ~]#  vim /usr/local/nginx/conf/nginx.conf

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        location ~ \.php$ {
           root           html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
        }

    //搜索找到\.php$ 在{}里面添加以上内容
这里面很多都是默认的,root是配置php程序放置的根目录,主要修改的就是fastcgi_param中的/scripts为$document_root

测试PHP程序
在nginx下的html目录下创建test.php文件并打印配置php
[root@yanyinglai3 ~]# cd /usr/local/nginx/
[root@yanyinglai3 nginx]# ls
client_body_temp  fastcgi_temp  logs        sbin       uwsgi_temp
conf              html          proxy_temp  scgi_temp
[root@yanyinglai3 nginx]# cd html/
[root@yanyinglai3 html]# touch test.php
[root@yanyinglai3 html]# cat > test.php << EOF
> <?php
> phpinfo();
> ?>
> EOF

验证
打开浏览器输入 192.168.47.12/test.php进行访问,看到输出页面,说明nginx和php都配置成功了         

技术分享图片

LNMP架构搭建详细部署

标签:free   mirrors   防火   rem   sed   smt   ase   col   filter   

原文地址:http://blog.51cto.com/13910274/2164661

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!