基于centos7的LNMP搭建

名词解释:
LNMP
Linux+Nginx+MySql+PHP
LAMP
LInux+Apache+MySql+PHP

Nginx的正确读法应该是Engine X
我们使用CentOS自带的YUM来安装

 

centos自带的yum源来安装nginxmysqlphp,超级方便,省去编译的麻烦,省去自己配置的麻烦,还能节省非常多的时间。

先把YUM源切换成国内的镜像源
先备份一下原来的源镜像文件

#cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

下载新的CentOS-Base.repo

#wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

如果没有安装wget的话可以使用#yum install wget安装完成之后,在执行CentOS-Base.repo的安装

对/etc/yum.repos.d/CentOS-Media.repo源文件配置文件,改为不生效

enable=0

YUM缓存生成

#yum clean all

#yum makecache

#yum update

安装Nginx 

YUM源中没有Nginx,我们需要增加一个nginx的源nginx.repo 

[root@localhost conf]# vim /etc/yum.repos.d/nginx.repo

源文件的内容

[nginx]

name=nginx repo

baseurl=http://nginx.org/packages/centos/$releasever/$basearch/

gpgcheck=0

enabled=1

查看Nginx是否配置成功

[root@localhost conf]# yum list nginx

安装成功后,就可以直接安装nginx

[root@localhost conf]# yum -y installnginx

安装的就是Nginx官网的最新版本

[root@localhost conf]# nginx #启动Nginx

 

可以使用curl命令查看是否安装成功

#curl 127.0.0.1

如果安装成功的话,就会看到输出一个HTML的一个反馈

开机启动设置

[root@localhost conf]# systemctl enable nginx

Created symlink from/etc/systemd/system/multi-user.target.wants/nginx.service to/usr/lib/systemd/system/nginx.service.

[root@localhost conf]# systemctl daemon-reload

安装MySql(5.7)

官网:http://dev.mysql.com/downloads/repo/yum/
[root@localhost conf]# rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm 

查看5.7版本是否已经启用

[root@localhost conf]# yum repolist all | grep mysql
mysql-connectors-community/x86_64 MySQL Connectors Community         启用:    24
mysql-connectors-community-source MySQL Connectors Community - Sourc 禁用
mysql-tools-community/x86_64      MySQL Tools Community              启用:    38
mysql-tools-community-source      MySQL Tools Community - Source     禁用
mysql-tools-preview/x86_64        MySQL Tools Preview                禁用
mysql-tools-preview-source        MySQL Tools Preview - Source       禁用
mysql55-community/x86_64          MySQL 5.5 Community Server         禁用
mysql55-community-source          MySQL 5.5 Community Server - Sourc 禁用
mysql56-community/x86_64          MySQL 5.6 Community Server         禁用
mysql56-community-source          MySQL 5.6 Community Server - Sourc 禁用
mysql57-community/x86_64          MySQL 5.7 Community Server         启用:   146
mysql57-community-source          MySQL 5.7 Community Server - Sourc 禁用
mysql80-community/x86_64          MySQL 8.0 Community Server         禁用
mysql80-community-source          MySQL 8.0 Community Server - Sourc 禁用

如果没有启用的话,我们可以修改源文件

#/etc/yum.repos.d/mysql-community.repo

[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 

把enabled改为1就可以了,其他的版本改为0

[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 
[mysql-tools-community]
name=MySQL Tools Community
baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
# Enable to use MySQL 5.5
[mysql55-community]
name=MySQL 5.5 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 
# Enable to use MySQL 5.6
[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql-tools-preview]
name=MySQL Tools Preview
baseurl=http://repo.mysql.com/yum/mysql-tools-preview/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

修改完成之后查看可用的版本

#yum repolist enabled | grep mysql 

mysql-connectors-community/x86_64 MySQL Connectors Community                  24
mysql-tools-community/x86_64      MySQL Tools Community                       38
mysql57-community/x86_64          MySQL 5.7 Community Server                 146

如果看到5.7版本启用了之后就可以安装MySql了

[root@localhost conf]# yum -y install mysql-community-server

安装完成之后,就可以启动mysql了

#service mysqld start或者

[root@localhost conf]# /bin/systemctl start mysqld.service

查看MySql的启动状态

[root@localhost conf]# service mysqld status 
Redirecting to /bin/systemctl status mysqld.service
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 日 2018-01-21 22:13:58 CST; 12s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 96454 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 96377 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 96458 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─96458 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid...
1月 21 22:13:52 localhost.localdomain systemd[1]: Starting MySQL Server...
1月 21 22:13:58 localhost.localdomain systemd[1]: Started MySQL Server.

开机启动设置

[root@localhost conf]# systemctl enable mysqld
[root@localhost conf]# systemctl daemon-reload

MySql安装完成之后会在LOG文件(/var/log/mysqld.log)中生成一个root的默认密码

[root@localhost conf]# grep 'temporary password' /var/log/mysqld.log
2018-01-21T14:13:55.398172Z 1 [Note] A temporary password is generated for root@localhost: lFJ_5z/ovg#-
[root@localhost conf]# mysql -uroot -p

登录MySql并修改root密码

[root@localhost conf]# mysql -uroot -p

Enter password:       //密码就是lFJ_5z/ovg#-
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.21
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456qQ@';
Query OK, 0 rows affected (0.00 sec)

 

安装PHP7

[root@localhost conf]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

[root@localhost conf]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

安装PHP7

[root@localhost conf]# yum install php70w.x86_64 php70w-cli.x86_64php70w-common.x86_64 php70w-gd.x86_64 php70w-ldap.x86_64 php70w-mbstring.x86_64php70w-mcrypt.x86_64 php70w-mysql.x86_64 php70w-pdo.x86_64

安装php-fpm

[root@localhostconf]# yum install php70w-fpm php70w-opcache

启动php-fpm

[root@localhostconf]# systemctl start php-fpm

开机启动设置

[root@localhostconf]# systemctl enable php-fpm

Created symlinkfrom /etc/systemd/system/multi-user.target.wants/php-fpm.service to/usr/lib/systemd/system/php-fpm.service.

[root@localhost conf]# systemctl daemon-reload

修改根目录

vim /etc/nginx/conf.d/default.conf

 

 

location ~ \.php$ {
        root           /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

重启Nginx使修改生效

[root@localhost system]# killall -9nginx

[root@localhost system]# nginx

[root@localhost system]# ps aux|grepnginx

经过NMP这三部的安装后,至此LNMP的搭建就完成了!

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值