Vagrant中Centos7源码搭建lnmp环境

【一】Nginx安装

1.清理之前安装的,并更新yum,安装wget

[root@CentOS7 /]# yum remove httpd
[root@CentOS7 /]# yum remove mysql
No Packages marked for removal
[root@CentOS7 /]# yum remove php
[root@CentOS7 /]# yum update
[root@CentOS7 /]# yum install wegt

2.查看当前系统环境

[root@CentOS7 /]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@CentOS7 /]# uname -r
3.10.0-957.12.2.el7.x86_64
[root@CentOS7 /]# uname -m
x86_64

3.安装Nginx所需要的pcre库,来支持nginx具备的URI重写功能rewrite模块

//采用yum安装方式来安装pcre
[root@CentOS7 /]# yum install pcre pcre-devel -y
//查看安装后结果
[root@CentOS7 /]# rpm -qa pcre pcre-devel
pcre-7.8-7.el6.x86_64
pcre-devel-7.8-7.el6.x86_64
//检查Nginx基础依赖包pcre-devel openssl-devel
[root@CentOS7 /]# rpm -qa pcre openssl-devel openssl
openssl-1.0.2k-16.el7_6.1.x86_64
pcre-8.32-17.el7.x86_64

但是这里没有openssl devel包,所以接下来要安装。这是HTTPS服务的时候要用到的模块。

4.安装openssl-devel来使Nginx提供HTTPS服务

[root@CentOS7 /]# yum install -y openssl openssl-devel
[root@CentOS7 /]# rpm -qa openssl openssl-devel
openssl-1.0.2k-16.el7_6.1.x86_64
openssl-devel-1.0.2k-16.el7_6.1.x86_64

5.安装Nginx服务

//创建一个应用目录
[root@CentOS7 /]# mkdir /application
//创建一个工具目录
[root@CentOS7 /]# mkdir -p /home/vagrant/tools
[root@CentOS7 /]# cd /home/vagrant/tools/
//下载nginx 
[root@CentOS7 tools]# wget -q http://nginx.org/download/nginx-1.15.10.tar.gz
//解压//-x   解开压缩文件 -f   目标文件名
[root@CentOS7 tools]# tar xf nginx-1.15.10.tar.gz
[root@CentOS7 tools]# ls
nginx-1.15.10  nginx-1.15.10.tar.gz
//创建nginx用户//-M:不创建家目录 -s,nologin就是登陆不了
[root@CentOS7 tools]# useradd nginx -s /sbin/nologin -M
//编译执行
[root@CentOS7 tools]# cd nginx-1.15.10/
[root@CentOS7 nginx-1.15.10]# ./configure \
> --user=nginx \
> --group=nginx \
> --prefix=/application/nginx-1.15.10/ \
> --with-http_stub_status_module \
> --with-http_ssl_module
[root@CentOS7 nginx-1.15.10]#make && make install
//注意**建立软连接方便访问,也方便升级
[root@CentOS7 nginx-1.15.10]# ln -s /application/nginx-1.15.10  /application/nginx

6.启动并检查结果

[root@CentOS7 /]# cd /application/
[root@CentOS7 application]# /application/nginx/sbin/nginx  -t
nginx: the configuration file /application/nginx-1.15.10//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.15.10//conf/nginx.conf test is successful
//启动
[root@CentOS7 application]# /application/nginx/sbin/nginx
[root@CentOS7 /]# ps -ef | grep nginx
root      4990     1  0 04:20 ?        00:00:00 nginx: master process /application/nginx/sbin/nginx
nginx     4991  4990  0 04:20 ?        00:00:00 nginx: worker process
root      4993  4976  0 04:23 pts/0    00:00:00 grep --color=auto nginx
[root@CentOS7 /]# curl http://127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

7.设置网络

配置文件修改然后重启reload一下。

Vagrant.configure("2") do |config|
  config.vm.box = "centos7"  #box名称
  config.vm.hostname = "CentOS7"  #系统下的主机名
  config.vm.synced_folder "D:/Vagrant-work", "/work" #共享目录
  config.vm.network :"forwarded_port", guest: 80, host: 8080 #端口映射
  config.vm.network :"private_network", ip: "192.168.10.129"#虚拟机设置IP 
  config.vbguest.auto_update = false #防止重新安装更新
  config.vbguest.no_remote = true    #不从远程web端下载
end

然后重新登陆虚拟机然后开启nginx,在本地浏览器访问ip。即可看到nginx成功页面。

 

==>Nginx1.15.10安装完成

 

【2】mysql安装

二进制安装,只需要解压就可以,不用make install

1.创建MySQL用户的账号

利用root进行操作。

//useradd  -s禁止用户登陆, -g设置mysql用户属于mysql组 -M不创建家目录
[root@CentOS7 /]# groupadd mysql
[root@CentOS7 /]# useradd -s /sbin/nologin -g mysql -M mysql
[root@CentOS7 /]# tail -1 /etc/passwd
mysql:x:1002:1002::/home/mysql:/sbin/nologin

2.获取mysql二进制安装包

下载网址:https://dev.mysql.com/downloads/mysql/5.6.html#downloads

3.解压移动到指定的安装目录

//tar 解压x解开压缩文件 f目标文件名
[root@CentOS7 tools]# tar xf mysql-5.6.44-linux-glibc2.12-x86_64.tar.gz
//mv 移动并重命名
[root@CentOS7 tools]# mv mysql-5.6.44-linux-glibc2.12-x86_64 /application/mysql-5.6.44
//创建软连接
[root@CentOS7 tools]# ln -s /application/mysql-5.6.44/ /application/mysql
[root@CentOS7 tools]# ls -al /application/
total 0
drwxr-xr-x.  4 root root  73 May 21 05:52 .
dr-xr-xr-x. 20 root root 286 May 21 03:09 ..
lrwxrwxrwx.  1 root root  26 May 21 05:52 mysql -> /application/mysql-5.6.44/
drwxr-xr-x. 13 root root 191 May 21 05:50 mysql-5.6.44
lrwxrwxrwx.  1 root root  26 May 21 03:19 nginx -> /application/nginx-1.15.10
drwxr-xr-x. 11 root root 151 May 21 03:19 nginx-1.15.10

4.初始化mysql配置文件

[root@CentOS7 application]# /bin/cp mysql/support-files/my-default.cnf  /etc/my.cnf

5.初始化mysql数据库文件

[root@CentOS7 mysql]# /application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data --user=mysql

如有有报错

报错1:FATAL ERROR: please install the following Perl modules before executing

需要安装一个依赖包,执行安装 yum -y install autoconf

报错2:error while loading shared libraries: libaio.so.1: cannot open shared

缺少libaio库文件,执行安装 yum install libaio* -y

如果执行初始化成功是可以在/application/mysql/data/里面看到mysql必要的基本文件。

6.配置并启动mysql

//拷贝mysql的启动脚本到mysql的命令路径
[root@CentOS7 mysql]# cp support-files/mysql.server  /etc/init.d/mysqld
//赋予可执行权限
[root@CentOS7 mysql]# chmod +x /etc/init.d/mysqld

启动MySQL报错:

[root@CentOS7 mysql]# /etc/init.d/mysqld start
/etc/init.d/mysqld: line 244: my_print_defaults: command not found
/etc/init.d/mysqld: line 264: cd: /usr/local/mysql: No such file or directory
Starting MySQL ERROR! Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)

这是因为没有修改mysql的配置文件。

进行修改

 //大概是46 47行 写上对应文件位置
 46 basedir=/application/mysql/
 47 datadir=/application/mysql/data/

成功启动MySQL

[root@CentOS7 mysql]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS!
[root@CentOS7 data]# cat CentOS7.err
2019-05-21 06:50:46 6122 [Note] Plugin 'FEDERATED' is disabled.
2019-05-21 06:50:46 6122 [Note] InnoDB: Using atomics to ref count buffer pool pages
2019-05-21 06:50:46 6122 [Note] InnoDB: The InnoDB memory heap is disabled
2019-05-21 06:50:46 6122 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2019-05-21 06:50:46 6122 [Note] InnoDB: Memory barrier is not used
2019-05-21 06:50:46 6122 [Note] InnoDB: Compressed tables use zlib 1.2.11
2019-05-21 06:50:46 6122 [Note] InnoDB: Using Linux native AIO
2019-05-21 06:50:46 6122 [Note] InnoDB: Using CPU crc32 instructions
2019-05-21 06:50:46 6122 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2019-05-21 06:50:46 6122 [Note] InnoDB: Completed initialization of buffer pool
2019-05-21 06:50:46 6122 [Note] InnoDB: Highest supported file format is Barracuda.
2019-05-21 06:50:46 6122 [Note] InnoDB: 128 rollback segment(s) are active.
2019-05-21 06:50:46 6122 [Note] InnoDB: Waiting for purge to start
2019-05-21 06:50:47 6122 [Note] InnoDB: 5.6.44 started; log sequence number 1626007
2019-05-21 06:50:47 6122 [Note] Server hostname (bind-address): '*'; port: 3306
2019-05-21 06:50:47 6122 [Note] IPv6 is available.
2019-05-21 06:50:47 6122 [Note]   - '::' resolves to '::';
2019-05-21 06:50:47 6122 [Note] Server socket created on IP: '::'.
2019-05-21 06:50:47 6122 [Note] Event Scheduler: Loaded 0 events
2019-05-21 06:50:47 6122 [Note] /application/mysql/bin/mysqld: ready for connections.
Version: '5.6.44'  socket: '/tmp/mysql.sock'  port: 3306  MySQL Community Server (GPL)

7.设置开启自启动

[root@CentOS7 mysql]# chkconfig --add mysqld
[root@CentOS7 mysql]# chkconfig mysql on
[root@CentOS7 mysql]# chkconfig --list mysqld
mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off

8.配置mysql命令的全局使用路径

为了之后连接mysql方便,不用敲全部路径就可以使用,我们可以添加到path环境变量中。

//把mysql命令添加到path里面
[root@CentOS7 mysql]# echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/vagrant/.local/bin:/home/vagrant/bin
[root@CentOS7 mysql]# echo 'export PATH=/application/mysql/bin:$PATH' >> /etc/profile
[root@CentOS7 mysql]# tail -1 /etc/profile
export PATH=/application/mysql/bin:$PATH
//使用source使之生效
[root@CentOS7 mysql]# source /etc/profile
[root@CentOS7 mysql]# echo $PATH
/application/mysql/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/vagrant/.local/bin:/home/vagrant/bin

还有一种方法就是把mysql的/application/mysql/bin 添加到全局系统命令中/usr/local/sbin下。

9.登陆MySQL测试

[root@CentOS7 mysql]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.44 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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 |
| test               |
+--------------------+
4 rows in set (0.00 sec)

mysql> select user();
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

10.设置MySQL的密码

[root@CentOS7 mysql]# mysqladmin -u root password '123456'
Warning: Using a password on the command line interface can be insecure.

//这样如果要进入MySQL就要输入密码了
[root@CentOS7 mysql]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@CentOS7 mysql]# mysql -uroot -p123456

11.删除无用的mysql用户

mysql> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1       |
|      | centos7   |
| root | centos7   |
|      | localhost |
| root | localhost |
+------+-----------+
6 rows in set (0.00 sec)

mysql> drop user "root"@"::1";
Query OK, 0 rows affected (0.00 sec)

mysql> drop user "root"@"centos7";
Query OK, 0 rows affected (0.00 sec)

mysql> drop user ""@"centos7";
Query OK, 0 rows affected (0.00 sec)

mysql> drop user ""@"localhost";
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> select user,host from mysql.user;
+------+-----------+
| user | host      |
+------+-----------+
| root | 127.0.0.1 |
| root | localhost |
+------+-----------+
2 rows in set (0.00 sec)

mysql>

==>Mysql5.6.44安装完成

 

【3】php的安装

1.安装php所需要的库

[root@CentOS7 application]# yum install zlib-devel libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel  libjpeg-turbo-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel gd-devel -y	
//安装libiconv库 因为yum安装不了
[root@CentOS7 application]# cd  /home/vagrant/tools/
[root@CentOS7 tools]# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
[root@CentOS7 tools]# tar zxf libiconv-1.14.tar.gz
[root@CentOS7 tools]# cd libiconv-1.14/
[root@CentOS7 libiconv-1.14]# ./configure --prefix=/usr/local/libiconv
[root@CentOS7 libiconv-1.14]# make && make install
//配置epel第三方yum源
[root@CentOS7 application]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
//安装libmcrypt库
[root@CentOS7 application]# yum install limcrypt-devel
//安装mhash加密扩展库
[root@CentOS7 application]# yum install mhash -y

2.下载php软件包

[root@CentOS7 tools]#  wget -O php-7.2.6.tar.gz http://cn2.php.net/get/php-7.2.6.tar.gz/from/this/mirror
[root@CentOS7 tools]# tar zxf php-7.2.6.tar.gz
[root@CentOS7 tools]# cd php-7.2.6/
[root@CentOS7 php-7.2.6]# ./configure \
> --prefix=/application/php7.2.6 \
> --with-mysql=/application/mysql \
> --with-iconv-dir=/usr/local/libiconv \
> --with-jpeg-dir \
> --with-png-dir \
> --with-zlib \
> --with-libxml-dir=/usr \
> --enable-xml \
> --disable-rpath \
> --enable-bcmath \
> --enable-sysvsem \
> --enable-inline-optimization \
> --with-curl \
> --enable-mbregex \
> --enable-fpm \
> --enable-mbstring \
> --with-gd \
> --with-openssl \
> --with-mhash \
> --enable-pcntl \
> --enable-sockets \
> --with-xmlrpc \
> --enable-zip \
> --enable-soap \
> --enable-short-tags \
> --enable-static \
> --with-xsl \
> --with-fpm-user=nginx \
> --with-fpm-group=nginx \
> --enable-ftp
也就是下面的命令编译php
 ./configure --prefix=/application/php7.2.6 --with-pdo-mysql --with-iconv-dir=/usr/local/libiconv  --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-sysvsem --enable-inline-optimization --with-curl  --enable-mbregex --enable-fpm  --enable-mbstring  --with-gd  --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-short-tags  --enable-static --with-xsl --with-fpm-user=nginx --with-fpm-group=nginx --enable-ftp

然后make && make install 执行完毕后最后两行
/home/vagrant/tools/php-7.2.6/build/shtool install -c ext/phar/phar.phar /application/php7.2.6/bin
ln -s -f phar.phar /application/php7.2.6/bin/phar

3.配置PHP引擎配置文件php.ini

//设置软连接,访问方便
[root@CentOS7 php-7.2.6]# ln -s /application/php7.2.6/ /application/php
[root@CentOS7 php-7.2.6]# ls -l /application/php
lrwxrwxrwx. 1 root root 22 May 21 09:20 /application/php -> /application/php7.2.6/
//复制php配置文件到默认文件中,并改名php.ini
[root@CentOS7 php-7.2.6]# ls php.ini*
php.ini-development  php.ini-production
[root@CentOS7 php-7.2.6]# cp php.ini-production  /application/php/lib/php.ini
[root@CentOS7 php-7.2.6]# ls -l /application/php/lib/php.ini
-rw-r--r--. 1 root root 70449 May 21 09:23 /application/php/lib/php.ini

4.配置PHP服务(FastCGI)

[root@CentOS7 application]# ls
mysql  mysql-5.6.44  nginx  nginx-1.15.10  php  php7.2.6
[root@CentOS7 application]# cd php/etc/
[root@CentOS7 etc]# ls
php-fpm.conf.default  php-fpm.d

//复制一份,先用默认的配置

[root@CentOS7 etc]# cp php-fpm.conf.default/ php-fpm.conf

5.启动PHP

//启动php-fpm
[root@CentOS7 etc]# /application/php/sbin/php-fpm
[21-May-2019 09:26:39] WARNING: Nothing matches the include pattern '/application/php7.2.6/etc/php-fpm.d/*.conf' from /application/php7.2.6/etc/php-fpm.conf at line 125.
[21-May-2019 09:26:39] ERROR: No pool defined. at least one pool section must be specified in config file
[21-May-2019 09:26:39] ERROR: failed to post process the configuration
[21-May-2019 09:26:39] ERROR: FPM initialization failed

[root@CentOS7 etc]# cd /application/php/etc/php-fpm.d/
[root@CentOS7 php-fpm.d]# ls
www.conf.default
//复制一份
[root@CentOS7 php-fpm.d]# cp www.conf.default  www.conf
//测试php-fpm是否有错误
[root@CentOS7 php-fpm.d]# /application/php/sbin/php-fpm  -t
[21-May-2019 09:28:58] NOTICE: configuration file /application/php7.2.6/etc/php-fpm.conf test is successful

//进行启动
[root@CentOS7 php-fpm.d]# /application/php/sbin/php-fpm
[root@CentOS7 php-fpm.d]# ps -ef | grep php
root     21082     1  0 09:30 ?        00:00:00 php-fpm: master process (/application/php7.2.6/etc/php-fpm.conf)
nginx    21083 21082  0 09:30 ?        00:00:00 php-fpm: pool www
nginx    21084 21082  0 09:30 ?        00:00:00 php-fpm: pool www

6.修改nginx的配置文件

//添加下面的代码
  location ~ .*\.(php)?$ {
                root html;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi.conf;
        }
   //然后重启启动nginx
   [root@CentOS7 conf]# /application/nginx/sbin/nginx -s reload

7.设置开机自启动

打开vim /etc/rc.d/rc.local文件,后面添加上这两句
/application/php/sbin/php-fpm
/application/nginx/sbin/nginx

【4】测试lnmp环境

[root@CentOS7 html]# pwd
/application/nginx/html
[root@CentOS7 html]# ls
50x.html  index.html.bak  index.php
[root@CentOS7 html]# cat index.php
<?php
phpinfo();

然后打开你的浏览器输入虚拟机ip

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值