linux下搭建lnmp环境

2.安装实验需要的软件包

2.1 安装nginx

2.1.1

在这个时候我们需要安装依赖包

yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel

2.1.2

下载并且解压到(/usr/local/src目录当中去)

cd /usr/local/src

wget http://nginx.org/download/nginx-1.12.2.tar.gz

tar -zxvf nginx-1.12.2.tar.gz

2.1.3 编译安装
cd nginx-1.12.2

./configure --prefix=/usr/local/nginx

make && make install

2.1.4添加账号

groupadd nginx

useradd -M -g nginx -s /sbin/nologin nginx

cd /usr/local/nginx/conf

编辑 vim nginx.conf

user nginx nginx;

linux下搭建lnmp环境_第1张图片

2.1.5设置nginx为系统服务

vim /lib/systemd/system/nginx.service

文件内容

[Unit]

Description=nginx

After=network.target

[Service]

Type=forking

ExecStart=/usr/local/nginx/sbin/nginx

ExecReload=/usr/local/nginx/sbin/nginx -s reload

ExecStop=/usr/local/nginx/sbin/nginx -s stop

PrivateTmp=true

[Install]

WantedBy=multi-user.target

2.1.6启动nginx

systemctl restart nginx

2.1.7在虚拟机中访问127.0.0.1

出现nginx的画面就是成功了

2.1.8如果需要在windos上看见

关闭防火墙

systemctl stop firewall.service

安装mysql 5.6

[root@server1 ~]# cat /etc/redhat-release 
CentOS Linux release 7.4.1708 (Core) 
[root@server1 ~]# uname -r
3.10.0-693.el7.x86_64

一、安装MySQL前准备

1)查看系统是否存在旧版本

rpm -qa | grep mysql

可能出现一到多个结果,也可能没有

2)卸载旧版本

rpm -e --nodeps {file-name}

{file-name} 中是第一步中查询到的名字,没查到旧版本,可以跳过本步骤

二、准备yum源

访问官网,选自适合自己操作系统的yum源:MySQL :: Download MySQL Yum Repository

1)去官网下载yum源的rpm安装包到 /tmp 目录

wget -P /tmp https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

需要安装wget软件

yum install -y wget

2)安装yum源

rpm -ivh /tmp/mysql80-community-release-el7-3.noarch.rpm

3)选择要安装的版本(默认是mysql 8.0版,如果安装8.0版本跳过本步骤)

方法一:使用命令选择版本

# 关闭8.0版本
yum-config-manager --disable mysql80-community
# 开启5.6版本
yum-config-manager --enable mysql56-community

需要安装 yum-utils 软件

yum install -y yum-utils

方法二:手动编辑yum文件

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

找到您要配置的子存储库的条目,然后编辑该enabled选项。指定 enabled=0禁用子存储库,或 enabled=1启用子存储库

例如,要安装 MySQL 5.6,请确保 enabled=0在mysql80 的子存储库条目上,以及enabled=1在mysql56 的子存储库条目上:

[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.6-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服务器

1)安装命令

yum install -y mysql-community-server

2)启动服务

方法一:centos7

# 启动服务
systemctl start mysqld
# 查看服务状态
systemctl status mysqld
# 停止服务
systemctl stop mysqld
# 重启服务
systemctl restart mysqld

方法二:centos6

# 启动服务
service mysqld start
# 查看服务状态
service mysqld status
# 停止服务
service mysqld stop
# 重启服务
service mysqld restart

四、配置MySQL

1)设置root用户密码

mysql5.6版本安装后root默认密码为空,可通过命令查看

mysql> select user,host,password from mysql.user;
+------+-----------+----------+
| user | host      | password |
+------+-----------+----------+
| root | localhost |          |
| root | server1   |          |
| root | 127.0.0.1 |          |
| root | ::1       |          |
|      | localhost |          |
|      | server1   |          |
+------+-----------+----------+

方法一:使用mysqladmin为账户分配密码

shell> mysqladmin -u root password "new_password"
shell> mysqladmin -u root -h host_name password "new_password"
  • Ps: 密码周围的双引号并不是必须的,但是如果密码包含空格或特殊字符时需要使用它们。

方法二:使用语句为root账户分配密码

shell> mysql -u root
mysql> UPDATE mysql.user SET Password = PASSWORD('new_password')
    ->     WHERE User = 'root';
mysql> FLUSH PRIVILEGES;
  • Ps: FLUSH 语句使服务器重新读取授权表,没有它,服务器不会注意到密码更改,直到您重新启动它。

2)授权root用户可以从任何位置远程访问

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'new_password' WITH GRANT OPTION;

mysql> FLUSH PRIVILEGES;

3)设置mysql编码为utf-8,解决中文乱码问题

step1: 查看编码集

mysql> SHOW VARIABLES LIKE 'character_set_%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

step2: 修改配置文件/etc/my.cnf

  • 将该配置文件清空
[root@server1 ~]# >/etc/my.cnf
  • 执行如下,修改配置文件
cat >> /etc/my.cnf << EOF
[client] 
default-character-set=utf8

[mysql] 
default-character-set=utf8

[mysqld] 
character-set-server=utf8
EOF
  • 重启mysqld服务
systemctl restart mysqld

step3: 重新查看编码集

mysql> SHOW VARIABLES LIKE 'character_set_%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

为了安全起见,添加一个新的帐户:

mysql> GRANT ALL PRIVILEGES ON *.* TO 'jeiker'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

2.3安装php

2.3.1 安装依赖包

yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel 

若没有libmcrypt包的话可以去下载安装

cd /usr/local/src

tar -zxvf libmcrypt-2.5.8.tar.gz

cd libmcrypt-2.5.8

./configure --prefix=/usr/local/libmcrypt

make

make install

2.3.2配置php

cd /usr/local/src

软件包可以在windos上利用xshell上传

cd php-7.2.0

./configure --prefix=/usr/local/php --disable-fileinfo --enable-fpm --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-openssl --with-zlib --with-curl --enable-ftp --with-gd --with-xmlrpc --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-gd-native-ttf --enable-mbstring --with-mcrypt=/usr/local/libmcrypt --enable-zip --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql-sock=/var/lib/mysql/mysql.sock --without-pear  --enable-bcmath

make

make install

cp php.ini-development /etc/php.ini

groupadd www-data

useradd -M -g www-data -s /sbin/nologin www-data

cd /usr/local/php/etc

cp php-fpm.conf.default php-fpm.conf

cd php-fpm.d

cp www.conf.default www.conf

vim www.conf

user=www-data

group=www-data

 vim /usr/local/nginx/conf/nginx.conf

第一步修改的

linux下搭建lnmp环境_第2张图片

第二步修改配置文件,然后开启php

linux下搭建lnmp环境_第3张图片

 2.3.3设置php-fpm为系统服务

vim /etc/systemd/system/php-fpm.service

文件内容:

[Unit]

Description=php-fpm

After=network.target

[Service]

Type=forking

ExecStart=/usr/local/php/sbin/php-fpm

PrivateTmp=True

[Install]

WantedBy=multi-user.target

启动服务

systemctl start php-fpm.service

然后创建测试访问的页面

linux下搭建lnmp环境_第4张图片

然后在浏览器里面输入地址访问

linux下搭建lnmp环境_第5张图片

然后就ok啦嘻嘻嘻有什么问题可以私聊我

你可能感兴趣的:(Centos7,linux,linux,nginx,centos)