lnmp部署

lnmp

1.实验环境

主机名 IP地址 角色 系统版本
nginx-server 192.168.100.114 nginx服务 red-hat7
msyql-server 192.168.100.60 mysql php服务 red-hat7
  • 关闭防火墙和selinux
[root@mysql-server ~]# systemctl stop firewalld
[root@mysql-server ~]# systemctl disable firewalld
[root@mysql-server ~]# setenforce 0

2.nginx端安装nginx服务

安装过程请看上篇
https://blog.csdn.net/weixin_43120459/article/details/83153510

3.安装mysql

  • 1.下载mysql,并解压
[root@mysql-server ~]# cd /usr/src/
[root@mysql-server src]# ls
debug  kernels  mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
[root@mysql-server src]# tar xf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
  • 2.安装依赖包
[root@mysql-server src]# yum -y install ncurses-devel openssl-devel openssl  cmake mariadb-devel
  • 3.创建mysql用户和组
[root@mysql-server src]# groupadd -r -g 306 mysql
[root@mysql-server src]# useradd -r -M -s /sbin/nologin -g 306 mysql
  • 4.做软连接和添加至环境变量
[root@mysql-server src]# cd /usr/local/
[root@mysql-server local]# ln -s mysql-5.7.23-linux-glibc2.12-x86_64/ mysql
[root@mysql-server local]# chown -R mysql.mysql /usr/local/mysql
[root@mysql-server local]# ll
total 0
drwxr-xr-x. 2 root root   6 Mar 10  2016 bin
drwxr-xr-x. 2 root root   6 Mar 10  2016 etc
drwxr-xr-x. 2 root root   6 Mar 10  2016 games
drwxr-xr-x. 2 root root   6 Mar 10  2016 include
drwxr-xr-x. 2 root root   6 Mar 10  2016 lib
drwxr-xr-x. 2 root root   6 Mar 10  2016 lib64
drwxr-xr-x. 2 root root   6 Mar 10  2016 libexec
lrwxrwxrwx  1 root root  36 Oct 18 10:21 mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/
drwxr-xr-x  9 root root 129 Oct 18 10:18 mysql-5.7.23-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root   6 Mar 10  2016 sbin
drwxr-xr-x. 5 root root  49 Aug 24 11:29 share
drwxr-xr-x. 2 root root   6 Mar 10  2016 src
[root@mysql-server local]# echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql
[root@mysql-server local]# . /etc/profile.d/mysql
[root@mysql-server local]# which mysql
/usr/local/mysql/bin/mysql
  • 5.建立数据存放目录
[root@mysql-server local]# mkdir /opt/data -p
[root@mysql-server local]# chown -R mysql.mysql /opt/data/
  • 6.初始化数据库
[root@mysql-server local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
[root@mysql-server local]# echo "/>:IFFrFk6hr" > /root/a
  • 7.配置mysql
[root@mysql-server local]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@mysql-server local]# ldconfig -v
  • 8.生成配置文件
[root@mysql-server local]# 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
  • 9.配置服务启动脚本
[root@mysql-server local]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@mysql-server local]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@mysql-server local]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
  • 10.启动Mysql
[root@mysql-server local]# service mysqld start
Starting MySQL.Logging to '/opt/data/mysql-server.err'.
 SUCCESS! 
[root@mysql-server local]# ss -anlt
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                         :::22                                      :::*                  
LISTEN     0      100                        ::1:25                                      :::*                  
LISTEN     0      80                          :::3306                                    :::*
  • 11.登陆mysql修改密码
[root@mysql-server local]# mysqladmin -uroot -p"/>:IFFrFk6hr" password 123.com

4.安装php

  • 1.安装依赖包
[root@php ~]# 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-develreadline readline-devel libxslt libxslt-devel mhash mhash-deve
  • 2.下载php
[root@msyql-server ~]# cd /usr/src/
[root@msyql-server ~]#  wget http://cn.php.net/distributions/php-7.2.8.tar.xz
[root@msyql-server ~]# tar xf php-7.2.10.tar.xz
  • 3.编译安装
[root@msyql-server ~]# cd php-7.2.10/
[root@msyql-server ~]# ./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@mysql-server php-7.2.10]# make && make install
  • 4.安装完成后配置
[root@mysql-server php-7.2.10]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@mysql-server php-7.2.10]# source /etc/profile.d/php7.sh
[root@mysql-server php-7.2.10]# which php
/usr/local/php7/bin/php
[root@mysql-server php-7.2.10]# php -v
PHP 7.2.10 (cli) (built: Oct 18 2018 11:00:16) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
  • 5.配置php-fam
[root@mysql-server php-7.2.10]# cp php.ini-production /etc/php.ini
[root@mysql-server php-7.2.10]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@mysql-server php-7.2.10]# chmod +x /etc/init.d/php-fpm
[root@mysql-server php-7.2.10]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf 
[root@mysql-server php-7.2.10]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
  • 6.编辑php-fpm的配置文件(/usr/local/php7/etc/php-fpm.conf)
    配置fpm相关选项为你所需要的值
[root@mysql-server php-7.2.10]# tail /usr/local/php7/etc/php-fpm.conf
; 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
  • 7.启动php服务
[root@mysql-server php-7.2.10]# service php-fpm start
Starting php-fpm  done
[root@mysql-server php-7.2.10]# ss -anlt
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                         :::22                                      :::*                  
LISTEN     0      100                        ::1:25                                      :::*                  
LISTEN     0      80                          :::3306                                    :::*

5.配置虚拟主机

  • 1.修改nginx的主配置文件
[root@nginx-server ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  1;

error_log  logs/error.log;    //去掉此行的注释
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

启用以php结尾的网站模板
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            root           /www;
            fastcgi_pass   192.168.100.60:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
  //修改server下面的内容
      #gzip  on;

    server {
        listen       80;
        server_name  www.xiefei.com;

        charset koi8-r;

        access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
  //启用以下几行
  events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
  • 2.在mysql端创建nginx用户和组
[root@mysql-server ]# groupadd -r nginx
[root@mysql-server ]# useradd -r -M -s /sbin/nologin -g nginx  nginx
  1. 编辑php配置文件
[root@mysql-server ~]# tail /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


listen = 192.168.100.60:9000
user = nginx
group = nginx
  • 4.创建存放网站名称,写入php网页信息
[root@mysql-server ]# mkdir /www
[root@mysql-server ]# chown -R nginx.nginx /www
[root@mysql-server ]# cd /www/
[root@mysql-server www]# cat test.php 

*5.重启Nginx端的nginx,mysql的php

[root@nginx-server html]# service nginx restart
[root@mysql-server ~]# service php-fpm restart 
Gracefully shutting down php-fpm . done
Starting php-fpm  done
  • 6.测试
    lnmp部署_第1张图片

你可能感兴趣的:(lnmp部署)