linux运维架构之路-Lnmp架构部署

Posted Demon

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux运维架构之路-Lnmp架构部署相关的知识,希望对你有一定的参考价值。

一、Lnmp架构原理

二、 Lnmp架构软件安装

1、Nginx安装脚本

#!/bin/bash
useradd -s /sbin/nologin -M www
mkdir -p /server/tools/
cd /server/tools/
wget http://nginx.org/download/nginx-1.10.3.tar.gz
tar xf nginx-1.10.3.tar.gz
yum install pcre-devel openssl-devel -y
cd /server/tools/nginx-1.10.3
./configure --prefix=/application/nginx-1.10.3 --user=www --group=www  --with-http_stub_status_module  --with-http_ssl_module
make && make install
ln -s /application/nginx-1.10.3/ /application/nginx
ln -s /application/nginx/sbin/nginx  /usr/local/sbin/
nginx
lsof -i:80

2、MySQL安装脚本

#!/bin/bash
useradd -s /sbin/nologin -M mysql
cd /server/tools
tar xf mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz
mkdir -p /application/
mv /server/tools/mysql-5.6.34-*-x86_64 /application/mysql-5.6.34
ln -s /application/mysql-5.6.34/ /application/mysql
chown -R mysql.mysql /application/mysql/
/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data --user=mysql
\\cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
sed -i \'s#/usr/local/mysql#/application/mysql#g\' /application/mysql/bin/mysqld_safe /etc/init.d/mysqld
\\cp /application/mysql/support-files/my-default.cnf /etc/my.cnf
/etc/init.d/mysqld start
lsof -i:3306
echo \'export PATH=/application/mysql/bin:$PATH\' >>/etc/profile
source /etc/profile
chkconfig --add mysqld 
chkconfig mysqld on
/application/mysql/bin/mysqladmin -u root password \'123456\'

二进制安装MySQL详细步骤:
①下载解压二进制软件包

mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz

②创建MySQL管理用户

useradd -s /sbin/nologin  -M mysql

③将mysql解压后的程序包搬家到程序目录下,并进行重命名

mkdir -p /application/
mv /server/tools/mysql-5.6.34-*-x86_64 /application/mysql-5.6.34

④为mysql服务程序创建软链接

ln -s /application/mysql-5.6.34/ /application/mysql

⑤授权MySQL用户管理/application/mysql

chown -R mysql.mysql /application/mysql/

⑥初始化数据库

/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data --user=mysql

⑦复制启动脚本并授权

cp /application/mysql/support-files/mysql.server  /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld

⑧修改启动脚本 和 mysql命令中的路径

sed -i \'s#/usr/local/mysql#/application/mysql#g\' /application/mysql/bin/mysqld_safe /etc/init.d/mysqld

⑨复制默认配置文件

\\cp /application/mysql/support-files/my-default.cnf /etc/my.cnf 
/etc/init.d/mysqld start

⑩登录数据库命令简化方法

echo \'export PATH=/application/mysql/bin:$PATH\' >>/etc/profile
source /etc/profile
which mysql

MySQL基础命令使用:

命令

说明

show databases;

查询显示所有的数据库信息

create database oldboy;

创建新的数据库

drop database oldboy;

删除存在的数据库

use mysql;

表示选择使用一个数据库,相当于cd进入一个数据库

show tables;

查看数据库中表信息

select database();

表示查看当前所在数据库,类似于pwd命令的功能

select user();

查看当前登录数据库的用户,类似于whoami命令

select * from user\\G;

查看user表中所有信息,并且纵行显示

select user,host from user;

查看user表中指定信息,并且横行显示

select user,host from mysql.user;

查看可以登录mysql数据库的目录,以及都可以从哪里进行管理mysql数据库

drop user \'user\'@\'host\';

删除用户

flush privileges;

刷新权限

grant all on .* to user@\'host\' identified by \'oldboy123\';

创建用户

grant all on .* to Old_Boy@\'localhost\' identified by \'oldboy123\';

创建用户(用户名包含大写字母)

mysqladmin -u root password \'123456\';

为用户设置密码

mysqladmin -uroot -p123456 password 654321

修改密码,在知道旧密码的前提下

delete from mysql.user where user="root" and host="Mysql";

遇主机名大写,特殊字符使用drop无法删除

 3、PHP安装脚本

#!/bin/bash
yum install -y zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel
yum -y install libmcrypt-devel mhash mcrypt
cd /server/tools/
tar zxf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local/libiconv
make && make install
cd ..
cd /server/tools/
tar xf php-5.5.32.tar.gz
cd php-5.5.32
./configure \\
--prefix=/application/php-5.5.32 \\
--with-mysql=/application/mysql/ \\
--with-pdo-mysql=mysqlnd \\
--with-iconv-dir=/usr/local/libiconv \\
--with-freetype-dir \\
--with-jpeg-dir \\
--with-png-dir \\
--with-zlib \\
--with-libxml-dir=/usr \\
--enable-xml \\
--disable-rpath \\
--enable-bcmath \\
--enable-shmop \\
--enable-sysvsem \\
--enable-inline-optimization \\
--with-curl \\
--enable-mbregex \\
--enable-fpm \\
--enable-mbstring \\
--with-mcrypt \\
--with-gd \\
--enable-gd-native-ttf \\
--with-openssl \\
--with-mhash \\
--enable-pcntl \\
--enable-sockets \\
--with-xmlrpc \\
--enable-soap \\
--enable-short-tags \\
--enable-static \\
--with-xsl \\
--with-fpm-user=www \\
--with-fpm-group=www \\
--enable-ftp \\
--enable-opcache=no
ln -s /application/mysql/lib/libmysqlclient.so.18  /usr/lib64/
touch ext/phar/phar.phar
make && make install
ln -s /application/php-5.5.32/ /application/php
cp /server/tools/php-5.5.32/php.ini-production /application/php/lib/php.ini
cd /application/php/etc/
cp php-fpm.conf.default php-fpm.conf
/application/php/sbin/php-fpm
lsof -i:9000
echo "/application/php/sbin/php-fpm" >>/etc/rc.local

编译PHP详细步骤:

①安装PHP依赖

yum install -y zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel

② libiconv软件安装---字符集转换库

cd /server/tools/
tar zxf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local/libiconv
make && make install

无需安装libiconv依赖包编译参数

./configure \\
--prefix=/application/php-5.5.32 \\
--enable-mysqlnd \\
--with-mysql=mysqlnd \\
--with-mysqli=mysqlnd \\
--with-pdo-mysql=mysqlnd \\
--with-freetype-dir \\
--with-jpeg-dir \\
--with-png-dir \\
--with-zlib \\
--with-libxml-dir=/usr \\
--enable-xml \\
--disable-rpath \\
--enable-bcmath \\
--enable-shmop \\
--enable-sysvsem \\
--enable-inline-optimization \\
--with-curl \\
--enable-mbregex \\
--enable-fpm \\
--enable-mbstring \\
--with-mcrypt \\
--with-gd \\
--with-gettext \\
--enable-gd-native-ttf \\
--with-openssl \\
--with-mhash \\
--enable-pcntl \\
--enable-sockets \\
--with-xmlrpc \\
--enable-zip \\
--enable-soap \\
--enable-short-tags \\
--enable-static \\
--with-xsl \\
--with-fpm-user=www \\
--with-fpm-group=www \\
--enable-opcache=no  \\
--enable-ftp

③epel源依赖包

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
yum -y install libmcrypt-devel mhash mcrypt

④下载解压PHP软件:MySQL和web服务器分离编译参数

cd /server/tools/
tar xf php-5.5.32.tar.gz
cd php-5.5.32
./configure \\
--prefix=/application/php-5.5.32 \\
--with-mysql=mysqlnd \\
--with-pdo-mysql=mysqlnd \\
--with-iconv-dir=/usr/local/libiconv \\
--with-freetype-dir \\
--with-jpeg-dir \\
--with-png-dir \\
--with-zlib \\
--with-libxml-dir=/usr \\
--enable-xml \\
--disable-rpath \\
--enable-bcmath \\
--enable-shmop \\
--enable-sysvsem \\
--enable-inline-optimization \\
--with-curl \\
--enable-mbregex \\
--enable-fpm \\
--enable-mbstring \\
--with-mcrypt \\
--with-gd \\
--enable-gd-native-ttf \\
--with-openssl \\
--with-mhash \\
--enable-pcntl \\
--enable-sockets \\
--with-xmlrpc \\
--enable-soap \\
--enable-short-tags \\
--enable-static \\
--with-xsl \\
--with-fpm-user=www \\
--with-fpm-group=www \\
--enable-ftp \\
--enable-opcache=no

⑤防止PHP安装报错

ln -s /application/mysql/lib/libmysqlclient.so.18  /usr/lib64/
touch ext/phar/phar.phar

⑥配置安装

make && make install

⑦创建PHP软链接

ln -s /application/php-5.5.32/ /application/php

⑧ 配置PHP主配置文件

cp /server/tools/php-5.5.32/php.ini-production /application/php/lib/php.ini
cp /application/php/etc/php-fpm.conf.default /application/php/etc/php-fpm.conf

⑨lnmp架构部署时,如果mysql和web服务器不在一台主机上,PHP编译参数需要调整

--with-mysql=/application/mysql/  更改为 --with-mysql=mysqlnd

⑩启动PHP服务

/application/php/sbin/php-fpm
echo "/application/php/sbin/php-fpm" >>/etc/rc.local

三、测试Lnmp架构互通

1、测试Nginx到PHP

server {
        listen       80;
        server_name  www.etiantian.org;
        location / {
            root   html/www;
            index index.php index.html index.htm;
        }
        location ~ .*\\.(php|php5)?$ {
            root   html/bbs;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi.conf;
        }
    access_log logs/bbs_access.log main;
}
测试PHP首页文件
echo "<?php phpinfo(); ?>" >/application/nginx/html/blog/test_info.php        
location ~* .*\\.(php|php5)?$ {
            root html/blog;
            fastcgi_pass  127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi.conf;
}

2、测试PHP到MySQL

vim test_mysql.php
<?php
//$link_id=mysql_connect(\'主机名\',\'用户\',\'密码\');
//mysql -u用户 -p密码 -h 主机
$link_id=mysql_connect(\'localhost\',\'wordpress\',\'oldboy123\') or mysql_error();
if($link_id){
echo "mysql successful by oldboy !\\n";
}else{
echo mysql_error();
}
?>
注:curl blog.etiantian.org/test_mysql.php       测试php与MySQL的连通性

以上是关于linux运维架构之路-Lnmp架构部署的主要内容,如果未能解决你的问题,请参考以下文章

linux运维架构之路-Kafka集群部署

linux运维架构之路-k8s部署redis

Centos 7搭建LNMP架构及部署Discuz论坛

linux运维架构之路-Kubernetes1.13离线集群部署双向认证

linux运维架构之路-redis集群

lnmp架构实现动态php