Linux项目之lamp分离部署

lamp搭建

环境说明:

服务器类型 ip地址 应用 操作系统
httpd 192.168.159.135 httpd centos7
mysql 192.168.159.132 mysql centos7
php 192.168.159.136 php centos7

安装httpd:

# 关闭防火墙和SELINUX
[root@longnian ~]# systemctl stop firewalld
[root@longnian ~]# systemctl disable firewalld
[root@longnian ~]# setenforce 0
setenforce: SELinux is disabled

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

# 安装开发工具包
[root@longnian ~]# yum groups mark install 'Development Tools'

# 创建apache服务的用户和组
[root@longnian ~]# groupadd -r apache
[root@longnian ~]# useradd -r -M -s /sbin/nologin -g apache apache

# 安装依赖包
[root@longnian ~]# yum -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++

# 下载和安装apr以及apr-util
[root@longnian ~]# cd /usr/src/
[root@longnian src]# wget http://mirror.bit.edu.cn/apache/apr/apr-1.6.5.tar.gz

[root@longnian src]# wget http://mirror.bit.edu.cn/apache/apr/apr-util-1.6.1.tar.gz

[root@longnian src]# ls
apr-1.6.5.tar.gz       debug    mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
apr-util-1.6.1.tar.gz  kernels
[root@longnian src]# tar xf apr-1.6.5.tar.gz 
[root@longnian src]# tar xf apr-util-1.6.1.tar.gz 
[root@longnian src]# ls
apr-1.6.5         apr-util-1.6.1.tar.gz  mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
apr-1.6.5.tar.gz  debug
apr-util-1.6.1    kernels
[root@longnian src]# cd apr-1.6.5
[root@longnian apr-1.6.5]# vim configure
    cfgfile="${ofile}T"
    trap "$RM \"$cfgfile\"; exit 1" 1 2 15
    # $RM "$cfgfile"

[root@longnian apr-1.6.5]# ./configure --prefix=/usr/local/apr

[root@longnian apr-1.6.5]# make && make install

[root@longnian apr-1.6.5]# cd /usr/src/apr-util-1.6.1
[root@longnian apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr

[root@longnian apr-util-1.6.1]# make && make install

# 编译安装httpd
[root@longnian ~]# cd /usr/src
[root@longnian ~]# wget http://mirror.bit.edu.cn/apache/httpd/httpd-2.4.43.tar.gz



[root@longnian ~]# ls
httpd-2.4.43.tar.gz
[root@longnian ~]# tar xf httpd-2.4.43.tar.gz
[root@longnian ~]# cd httpd-2.4.43
[root@longnian httpd-2.4.43]# ./configure --prefix=/usr/local/apache \
--sysconfdir=/etc/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork

[root@longnian httpd-2.4.43]# make && make install

# 安装后配置
[root@longnian ~]# echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@longnian ~]# source /etc/profile.d/httpd.sh
[root@longnian ~]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@longnian ~]# echo 'MANPATH /usr/local/apache/man' >> /etc/man.config

# 取消ServerName前面的注释
[root@longnian ~]# sed -i '/#ServerName/s/#//g' /etc/httpd24/httpd.conf 

# 启动apache
[root@longnian ~]# apachectl start
[root@longnian ~]# ss -anlt
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128             *:111                         *:*                  
LISTEN      0      128             *:20048                       *:*                  
LISTEN      0      128             *:22                          *:*                  
LISTEN      0      100     127.0.0.1:25                          *:*                  
LISTEN      0      128             *:3260                        *:*                  
LISTEN      0      64              *:59680                       *:*                  
LISTEN      0      64              *:2049                        *:*                  
LISTEN      0      128             *:43943                       *:*                  
LISTEN      0      64             :::38029                      :::*                  
LISTEN      0      128            :::54030                      :::*                  
LISTEN      0      128            :::111                        :::*                  
LISTEN      0      128            :::80                         :::*                  
LISTEN      0      128            :::20048                      :::*                  
LISTEN      0      32             :::21                         :::*                  
LISTEN      0      128            :::22                         :::*                  
LISTEN      0      100           ::1:25                         :::*                  
LISTEN      0      128            :::3260                       :::*                  
LISTEN      0      64             :::2049                       :::*

安装mysql:

# 关闭防火墙和SELINUX
[root@longnian ~]# systemctl stop firewalld
[root@longnian ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
[root@longnian ~]# setenforce 0
setenforce: SELinux is disabled

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

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


# 下载二进制格式的mysql软件包
[root@longnian ~]# cd /usr/src/
[root@longnian src]# wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

# 解压软件至/usr/local/
[root@longnian src]# ls
debug  kernels  mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
[root@longnian src]# tar xf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@longnian ~]# ls /usr/local/
bin  games    lib    libexec                              sbin   src
etc  include  lib64  mysql-5.7.24-linux-glibc2.12-x86_64  share
[root@longnian ~]# cd /usr/local/
[root@longnian local]# ln -sv mysql-5.7.24-linux-glibc2.12-x86_64/ mysql
‘mysql’ -> ‘mysql-5.7.24-linux-glibc2.12-x86_64/’
[root@longnian local]# ll
总用量 0
drwxr-xr-x. 2 root root   6 8月  12 2015 bin
drwxr-xr-x. 2 root root   6 8月  12 2015 etc
drwxr-xr-x. 2 root root   6 8月  12 2015 games
drwxr-xr-x. 2 root root   6 8月  12 2015 include
drwxr-xr-x. 2 root root   6 8月  12 2015 lib
drwxr-xr-x. 2 root root   6 8月  12 2015 lib64
drwxr-xr-x. 2 root root   6 8月  12 2015 libexec
lrwxrwxrwx  1 root root  36 6月  17 05:47 mysql -> mysql-5.7.24-linux-glibc2.12-x86_64/
drwxr-xr-x  9 root root 120 6月  17 03:44 mysql-5.7.24-linux-glibc2.12-x86_64
drwxr-xr-x. 2 root root   6 8月  12 2015 sbin
drwxr-xr-x. 5 root root  46 4月  26 19:28 share
drwxr-xr-x. 2 root root   6 8月  12 2015 src


# 修改目录/usr/local/mysql的属主属组
[root@longnian ~]# chown -R mysql.mysql /usr/local/mysql
[root@longnian local]# ll /usr/local/mysql -d
lrwxrwxrwx 1 mysql mysql 36 6月  17 05:47 /usr/local/mysql -> mysql-5.7.24-linux-glibc2.12-x86_64/



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



# 建立数据存放目录
[root@longnian mysql]# mkdir /opt/data
[root@longnian mysql]# chown -R mysql.mysql /opt/data/
[root@longnian local]# ll /opt/
总用量 0
drwxr-xr-x 2 mysql mysql 6 6月  17 05:49 data



# 初始化数据库
[root@longnian ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2020-06-16T21:50:40.761225Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-06-16T21:50:40.958277Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-06-16T21:50:41.021566Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-06-16T21:50:41.084252Z 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: 6c01283b-b01b-11ea-a2e8-000c294a7e86.
2020-06-16T21:50:41.085737Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-06-16T21:50:41.099008Z 1 [Note] A temporary password is generated for root@localhost: 6E5po9tuj_qI
# 请注意,这个命令的最后会生成一个临时密码,此处密码是6E5po9tuj_qI


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

# 生成配置文件
[root@longnian ~]# cat > /etc/my.cnf < 

# 设置新密码
mysql> set password = password('longnian123.');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye

安装php:

# 关闭防火墙和SELINUX
[root@longnian ~]# systemctl stop firewalld
[root@longnian ~]# systemctl disable firewalld
[root@longnian ~]# getenforce 
Disabled

# 配置yum源
[root@longnian ~]# wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
[root@longnian ~]# rpm -Uvh remi-release-7.rpm
[root@longnian ~]# yum makecache --enablerepo=remi-php74

# 安装依赖包
[root@longnian ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel  pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php72-php-mysqlnd

# 下载php
[root@longnian ~]# cd /usr/src/
[root@longnian src]# wget http://cn.php.net/distributions/php-7.2.8.tar.xz
下载过程略....


# 编译安装php
[root@longnian src]# tar xf php-7.2.8.tar.xz
[root@longnian src]# cd php-7.2.8
[root@longnian php-7.2.8]# ./configure --prefix=/usr/local/php7  \
--with-config-file-path=/etc \
--enable-fpm \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif  \
--enable-ftp \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-freetype-dir \
--with-gettext \
--enable-json \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix
[root@longnian php-7.2.8]# make -j $(cat /proc/cpuinfo |grep processor|wc -l)

[root@longnian php-7.2.8]# make install

# 安装后配置
[root@longnian ~]# echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@longnian ~]# source /etc/profile.d/php7.sh
[root@longnian php-7.2.8]# which php
/usr/local/php7/bin/php

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

[root@longnian ~]# 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



# 启动php-fpm
[root@longnian ~]# service php-fpm start
Starting php-fpm  done

# 默认情况下,fpm监听在127.0.0.1的9000端口,也可以使用如下命令验证其是否已经监听在相应的套接字
[root@longnian ~]# ss -anlt
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128     127.0.0.1:9000                        *:*                  
LISTEN      0      64              *:39946                       *:*                  
LISTEN      0      128             *:111                         *:*                  
LISTEN      0      128             *:20048                       *:*                  
LISTEN      0      128             *:22                          *:*                  
LISTEN      0      128             *:34360                       *:*                  
LISTEN      0      100     127.0.0.1:25                          *:*                  
LISTEN      0      128             *:3260                        *:*                  
LISTEN      0      64              *:2049                        *:*                  
LISTEN      0      64             :::43533                      :::*                  
LISTEN      0      128            :::111                        :::*                  
LISTEN      0      128            :::20048                      :::*                  
LISTEN      0      128            :::60469                      :::*                  
LISTEN      0      128            :::22                         :::*                  
LISTEN      0      100           ::1:25                         :::*                  
LISTEN      0      128            :::3260                       :::*                  
LISTEN      0      64             :::2049                       :::*

httpd,mysql,php安装完成后做以下操作:
httpd服务端:

# 启用代理模块
[root@longnian ~]# sed -i '/proxy_module/s/#//g' /etc/httpd24/httpd.conf
[root@longnian ~]# sed -i '/proxy_fcgi_module/s/#//g' /etc/httpd24/httpd.conf

# 编辑配置文件添加以下内容
[root@longnian ~]# vim /etc/httpd24/httpd.conf

    DocumentRoot "/usr/local/apache/htdocs/"
    ServerName www.longniand.com
    ProxyRequests Off
    ProxyPassMatch ^/(.*\.php)$ fcgi://192.168.159.136:9000/usr/local/apache/htdocs/$1
    
        Options none
        AllowOverride none
        Require all granted
    


# 搜索AddType,添加以下内容
	AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType application/x-httpd-php .php       		# 添加此行
    AddType application/x-httpd-php-source .phps        # 添加此行

[root@longnian htdocs]# sed -i '/    DirectoryIndex/s/index.html/index.php index.html/g' /etc/httpd24/httpd.conf   
# 做完上面的操作后保存退出

php服务端:

# 修改php-fpm的配置文件 
[root@longnian ~]# vim /usr/local/php7/etc/php-fpm.d/www.conf
listen = 192.168.159.136:9000		# 找到此行将自己的ip地址监听
;listen.allowed_clients = 192.168.159.135		#找到此行将httpd地址设置可访问
# 保存退出

# 创建根目录
[root@longnian ~]# cd /usr/local/apache/htdocs
[root@longnian htdocs]# vim index.php

重启httpd服务和php服务

[root@longnian ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done

[root@longnian ~]# apachectl stop
[root@longnian ~]# apachectl start

添加hosts文件

windows端:
C:\Windows\System32\drivers\etc\hosts
添加:192.168.159.135      www.longniand.com

验证
Linux项目之lamp分离部署_第1张图片

你可能感兴趣的:(Linux项目之lamp分离部署)