用yum的方式快速安装lnmp环境(MySQL5-5&5-7,php5-x&7-x)
文章发布较早,内容可能过时,阅读注意甄别。
- mysql-5.5
- php-5.x
# 1,准备。
安装 LNMP 之前要安装 EPEL,以便安装源以外的软件,如 Nginx,phpMyAdmin 等
yum install epel-release -y
1
# 2,安装 nginx
yum install nginx -y
systemctl start nginx
systemctl enable nginx
1
2
3
2
3
# 3,安装 php
yum install -y php php-devel php-fpm php-mysql php-common php-devel php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt libmcrypt-devel
systemctl start php-fpm
systemctl enable php-fpm
1
2
3
2
3
# 4,安装 mysql
yum -y install mariadb mariadb-server
systemctl enable mariadb
systemctl restart mariadb
1
2
3
2
3
初始化数据库:
mysql_secure_installation << EOF
Y
123456
123456
Y
Y
Y
Y
EOF
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
密码是123456
。
然后验证一下:
[root@localhost ~]$mysql -u root -p123456 -e 'show databases'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 5,添加 nginx 配置
[root@jenkins ~]# cat /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name 10.0.40.245;
root /usr/share/nginx/html;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
root /usr/share/nginx/html;
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
重启 nginx 即可
# 6,测试 php 是否正常运行
[root@jenkins ~]# cat /usr/share/nginx/html/index.php
<?php
// Show all information, defaults to INFO_ALL
phpinfo();
// Show just the module information.
// phpinfo(8) yields identical results.
phpinfo(INFO_MODULES);
?>
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
web 访问 index.php
10.0.40.245/index.php
- mysql-5.7
- php-7.x
# 1,安装 nginx。
安装 nginx 最新源
yum localinstall http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum repolist enabled | grep "nginx*"
1
2
2
安装 nginx
yum -y install nginx
1
启动 nginx
service nginx start
1
设置 nginx 服务器开机自启动
systemctl enable nginx.service
1
检查开机自动是否设置成功
systemctl list-dependencies | grep nginx
1
浏览器中输入本机 ip, 检测是否安装成功
http://127.0.0.1 (opens new window)
# 2,安装 mysql5.7
安装 mysql 源
yum -y localinstall http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
yum repolist enabled | grep "mysql.*-community.*"
1
2
2
安装 mysql
yum -y install mysql-community-server mysql-community-devel
1
启动 mysql
service mysqld start
1
检查 mysql 启动是否正常
service mysqld status 或者 ps -ef | grep mysql
1
设置 mysqld 服务开机自启动
systemctl enable mysqld.service
1
检查 mysqld 开机自启动是否设置成功
systemctl list-dependencies | grep mysqld
1
mysql5.7 以后的争强了安全机制, 所以使用 yum 安装, 启动会系统会自动生成一个随机的密码, 修改 mysql 密码
查看 mysql 的随机密码
grep 'temporary password' /var/log/mysqld.log
1
使用查询得到的随机密码在终端登录
mysql -u root -p 更改密码(mysql文档规定,密码必须包括大小写字母数字加特殊符号>8位)
ALTER USER 'root'@'localhost' IDENTIFIED BY 'Yourpassword';
1
2
2
退出 mysql 客户端, 用刚才修改的密码登录确保密码修改成功
exit;
mysql -u root -p
1
2
2
# 3,安装 php7.1
安装 php 源
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
1
2
2
检查源是否安装成功
yum repolist enabled | grep "webtatic*"
1
安装 php 扩展源
yum -y install php71w php71w-fpm
yum -y install php71w-mbstring php71w-common php71w-gd php71w-mcrypt
yum -y install php71w-mysql php71w-xml php71w-cli php71w-devel
yum -y install php71w-pecl-memcached php71w-pecl-redis php71w-opcache
1
2
3
4
2
3
4
验证 php7.1.x 和扩展是否安装成功 验证 php 是否安装成功
php -v
1
验证对应的扩展是否安装成功
php -m
1
设置 php-fpm 并检测 php-fpm 的运行状态 启动 php-fpm
service php-fpm start
1
检查启动是否成功
service php-fpm status
1
设置开机自启动
systemctl enable php-fpm.service
1
检查开机自启动是否设置成功
systemctl list-dependencies | grep php-fpm
ps -ef | grep php-fpm
1
2
2
# 4,配置 nginx。
$ cat /etc/nginx/conf.d/default.conf
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
root /usr/share/nginx/html;
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
上次更新: 2024/07/04, 22:40:37