linux运维—lnmp架构(源码编译安装nginx 、mysql 、 php)

lnmp

LNMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写。LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。L指Linux,N指Nginx,M一般指MySQL,也可以指MariaDB,P一般指PHP,也可以指Perl或Python。

  • Linux是一类Unix计算机操作系统的统称,是目前最流行的免费操作系统。
  • Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。
  • Mysql是一个小型关系型数据库管理系统。
  • PHP是一种在服务器端执行的嵌入HTML文档的脚本语言。

这四种软件均为免费开源软件,组合到一起,成为一个免费、高效、扩展性强的网站服务系统

一、 nginx源码编译
1、准备nginx源码包

可以从官网进行下载:www.nginx.org —>download
以下使用该版本进行: nginx-1.15.9.tar.gz

2、解压nginx压缩包

根据源码包压缩方式进行解压:

压缩方式解压命令
gziptar zxf nginx-1.15.9.tar.gz
bzip2tar jxf nginx-1.15.9.tar.gz
xztar Jxf nginx-1.15.9.tar.gz

查看文件的压缩方式:使用file命令

[root@toto4 lnmp]# file nginx-1.15.9.tar.gz   # 查看压缩包的属性
nginx-1.15.9.tar.gz: gzip compressed data, from Unix, last modified: Tue Feb 26 23:29:26 2019
                    #压缩方式gzip

本次使用的压缩包使用的是gzip的方式进行压缩
解压nginx:tar zxf nginx-1.15.9.tar.gz

进入nginx解压包,修改配置,便于进行最优进行编译安装

1 、进入解压包:

cd nginx-1.15.9/

2、修改配置文件。关闭debug模式进行编译安装
关闭该模式之后,编译安装的nginx目录会比较小,便于移动使用。

[root@toto4 nginx-1.15.9]# vim auto/cc/gcc  # 修改该文件

171 # debug
172 #CFLAGS="$CFLAGS -g"   # 将这两行进行注释,关闭debug模式

3 、修改配置文件。关闭显示nginx版本功能
关闭之后,别人无法查看已经安装的nginx本版

[root@toto4 nginx-1.15.9]# vim src/core/nginx.h  #修改该文件

 14 #define NGINX_VER          "nginx/"      #将该14行保留该部分
编译安装nginx:
./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --user=nginx --group=nginx

–prefix=/usr/local/lnmp/nginx #安装路径
–with-http_ssl_module # 添加模块
–user=nginx --group=nginx # 指定用户,指定用户组

过程中可能会出现报错,缺少依赖,根据提示安装依赖,重新进行编译:
不同的机器出现的情况不一致,根据实际情况解决依赖即可。

[root@toto4 nginx-1.15.9]# yum install pcre-devel openssl-devel -y
make && make install    # 编译安装
nginx 服务初始设置:

创建nginx用户,并修改配置文件指定该服务的用户以及组为nginx

[root@toto4 html]# useradd nginx  # 先在系统中创建nginx用户

[root@toto4 conf]# vim nginx/conf/nginx.conf   # 修改配置文件

  2 user  nginx nginx;   # 指定用户以及用户组为nginx
测试安装成功:

1、进入到指定好的安装目录中,设置软链接或者添加环境变量,方便使用nginx。

将nginx启动脚本链接到/usr/local/sbin/

ln -s /usr/local/lnmp/nginx/sbin/nginx /usr/local/sbin/

2 、编辑发布文件:
nginx的共享发布目录为:/usr/local/lnmp/nginx/html

vim /usr/local/lnmp/nginx/html/index.html 

hello nginx

3、开启服务

nginx  -t   # 检查nginx  检测语法 、配置是否正确
nginx -s reload   # 重新加载参数配置
nginx -s stop   # 关闭服务
nginx   # 开启服务

4 、测试服务是否正常:

[root@foundation13 Desktop]# curl 172.25.13.140
hello nginx
[root@foundation13 Desktop]# curl 172.25.13.140
hello nginx
二、源码编译mysql
1 、下载源码包进行并进行解压

可以从官网进行下载:https://www.mysql.com/downloads/

解压:tar zxf mysql-boost-5.7.17.tar.gz

2、安装cmake(相当于configure),用来编译mysql

mysql拥有自己的编译方式,需要进行安装:

yum install -y cmake-2.8.12.2-4.el6.x86_64.rpm 同时解决依赖性

3 、编译安装
编译
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql -DMYSQL_DATADIR=/usr/local/lnmp/mysql/data -DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DWITH_BOOST=boost/boost_1_59_0/
  • -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql \ #安装目录
  • -DMYSQL_DATADIR=/usr/local/lnmp/mysql/data \ #数据库存放目录
  • -DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock \ #Unix socket 文件路径
  • -DWITH_MYISAM_STORAGE_ENGINE=1 \ #安装 myisam 存储引擎
  • -DWITH_INNOBASE_STORAGE_ENGINE=1 \ #安装 innodb 存储引擎
  • -DDEFAULT_CHARSET=utf8 \ #使用 utf8 字符
  • -DDEFAULT_COLLATION=utf8_general_ci \ #校验字符
  • -DEXTRA_CHARSETS=all #安装所有扩展字符集
  • -DWITH_BOOST= 参数 -DWITH_BOOST=boost/boost_1_59_0/(用来解决以下报错)
##报错##
CMake Error at cmake/boost.cmake:81 (MESSAGE):
  You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=<directory>

  This CMake script will look for boost in <directory>.  If it is not there,
  it will download and unpack it (in that directory) for you.

  If you are inside a firewall, you may need to use an http proxy:

  export http_proxy=http://example.com:80

再编译过程中,可能会出现各种依赖问题导致的无法编译:根据缺少的依赖提示进行安装解决。
例如:需要安装包:yum install -y gcc gcc-c++ ncurses-devel
以及其他的一些模块或者文件。当缺少文件的时候:
yum whatprovides */file #可以查询所需要的文件所在的安装包,对应进行安装即可:

出现错误解决后重新编译之前,需要清除原来的对象文件和缓存信息

make clean
rm -fr CmakeCache.txt

该步骤必须进行,没出现一次失败,再重新编译之前都需要进行

安装:

make && make install #过程较长) ##如果make还有问题,删除原来的目录,重新解压编译

4、安装完成后进行初始化设置:

1 设置启动脚本:
启动脚本可以自己编写,也可以直接采用源码包中进行拷贝:

[root@toto4 lnmp]# cd mysql-5.7.17/
[root@toto4 mysql-5.7.17]# pwd
/root/lnmp/mysql-5.7.17    # 进入mysql源码包中
[root@toto4 mysql-5.7.17]# cp /root/lnmp/mysql-5.7.17/support-files/mysql.server /etc/init.d/mysqld    # 复制源码包中提供启动脚本到指定位置

chmod 777 /etc/init.d/mysqld # 启动脚本必须拥有执行权限才能正常启动
2 、设置配置文件
将原来d的 /etc/my.cnf 备份,重新拷贝解压后的安装包里的配置文件过去

[root@toto4 lnmp]# cd mysql-5.7.17/
[root@toto4 mysql-5.7.17]# cp /etc/my.cnf /etc/my.cnf.bak
[root@toto4 mysql-5.7.17]# cp /root/lnmp/mysql-5.7.17/support-files/my-default.cnf /etc/my.cnf
cp: overwrite ‘/etc/my.cnf’? y

修改配置:

[root@toto4 mysql-5.7.17]# vim /etc/my.cnf

basedir = /usr/local/lnmp/mysql
datadir = /usr/local/lnmp/mysql/data
socket = /usr/local/lnmp/mysql/data/mysql.sock

3 添加用户和组:

 groupadd -g 27 mysql
 useradd -u 27 -g 27 mysql (id=27是因为rpm默认安装时mysql用户的id为27,并非强制)

4 将mysql命令添加到环境变量
vim ~/.bash_profile
添加:PATH= P A T H : PATH: PATH:HOME/bin:/usr/local/lnmp/mysql/bin
生效:source ~/.bash_profile

5 初始化mysql:
mysqld --user=mysql --initialize(以mysql用户身份初始化)

初始化会生成一个临时密码,用于登录mysql(要记住此密码)
例如:

2019-06-30T15:08:38.375456Z 1 [Note] A temporary password is generated for root@localhost: :OGsUQ:HA1fk

6 数据目录所有者必须是mysql

chown mysql /usr/local/lnmp/mysql/data -R #不然mysql用户不能写

7 启动mysql:/etc/init.d/mysqld start

8 安全初始化:使用刚才的临时密码 :OGsUQ:HA1fk
mysql_secure_installation

[root@toto4 mysql-5.7.17]# mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root:   :OGsUQ:HA1fk

The existing password for the user account root has expired. Please set a new password.

New password: Westos+001

Re-enter new password: Westos+001

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: 
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : 

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : 

 ... skipping.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : 

 ... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : 

 ... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : 

 ... skipping.
All done! 
5 、测试使用:
[root@toto4 mysql-5.7.17]# mysql -uroot -pWestos+001
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.17 Source distribution

Copyright (c) 2000, 2016, 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> 
三、 php源码编译
1 、下载源码包并解压

https://www.php.net/downloads.php 可以在官网下载各种版本的安装包:

本次使用:php-5.6.35.tar.bz2
以及其依赖的其他包:
libmcrypt-2.5.8-9.el6.x86_64.rpm
libmcrypt-devel-2.5.8-9.el6.x86_64.rpm
re2c-0.13.5-1.el6.x86_64.rpm

解压:tar jxf php-5.6.35.tar.bz2

2、进入解压后的目录,开始编译

安装依赖的软件包:

yum install libmcrypt-2.5.8-9.el6.x86_64.rpm libmcrypt-devel-2.5.8-9.el6.x86_64.rpm re2c-0.13.5-1.el6.x86_64.rpm -y

编译:

./configure --prefix=/usr/local/lnmp/php --with-config-file-path=/usr/local/lnmp/php/etc --with-openssl --with-snmp --with-gd --with-zlib --with-curl --with-libxml-dir --with-png-dir --with-jpeg-dir --with-freetype-dir --with-gmp --with-gettext --with-pear --enable-mysqlnd --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-inline-optimization --enable-soap --enable-ftp --enable-sockets --enable-mbstring --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mcrypt --with-mhash

解决所有的依赖:yum whatprovides */file 查询所依赖的文件所在的安装包

 139  yum install libxml2-devel -y
  141  yum install libcurl-devel -y
 
 143  yum whatprovides */jpeglib.h
  144  yum install libjpeg-turbo-devel-1.2.90-5.el7.x86_64 -y
 
 146  yum whatprovides */png.h
  147  yum install -y libpng-devel-1.5.13-7.el7_2.x86_64
  
149  yum whatprovides */freetype-config
  150  yum install -y freetype-devel-2.4.11-12.el7.x86_64
  
152  yum whatprovides */gmp.h
  153  yum install gmp-devel-6.0.0-12.el7_1.x86_64 -y 
 
 155  yum whatprovides */net-snmp-config binary
  156  yum install -y net-snmp-devel-5.7.2-24.el7_2.1.x86_64 

安装:

make && make install 
3 、开始设置配置文件

1 第一个配置文件
[root@toto4 php-5.6.35]# cd /usr/local/lnmp/php/etc/
[root@toto4 etc]# cp php-fpm.conf.default php-fpm.conf # 复制一个配置文件

[root@toto4 etc]# vim php-fpm.conf # 修改文件内容:

149 user = nginx
150 group = nginx # 用户和组都是nginx

25 pid = run/php-fpm.pid # 取消该行的注释

2 第二个配置文件

[root@toto4 etc]# cd /root/lnmp/php-5.6.35/ # 进入该源码包中
[root@toto4 php-5.6.35]#cp php.ini-production /usr/local/lnmp/php/etc/php.ini # 复制文件到安装好的目录中作为配置文件

[root@toto4 etc]# vim php.ini # 修改配置文件
936 date.timezone = Asia/Shanghai # 取消该行的注释,并且设定时区和虚拟机时区一致

注意:查看修改虚拟机时区:
timedatectl status 查看时区状态
timedatectl set-timezone 时区 #设定当前时区

4 、 启动脚本

可以自己编写启动脚本或者在源码包中进行拷贝

[root@toto4 etc]# cd /root/lnmp/php-5.6.35/sapi/fpm/ #切换到源码包的该目录中

[root@toto4 fpm]# cp init.d.php-fpm /etc/init.d/php-fpm # 复制启动脚本到/etc/init.d

[root@toto4 fpm]# chmod +x /etc/init.d/php-fpm # 给启动脚本执行权限

5 、启动php

[root@toto4 fpm]# /etc/init.d/php-fpm start 使用启动脚本启动php
[root@toto4 fpm]# netstat -antlp # 查看端口开启情况
php:9000
nginx:80
mysql:3306

[root@toto4 fpm]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      6261/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      674/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      800/master          
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      488/php-fpm: master 
tcp        0      0 172.25.13.140:22        172.25.13.250:51076     ESTABLISHED 2083/sshd: root@pts 
tcp6       0      0 :::3306                 :::*                    LISTEN      6651/mysqld         
tcp6       0      0 :::22                   :::*                    LISTEN      674/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      800/master          

至此,所有服务全部源码编译完成,并且启动成功。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值