Dai Chong's blog

一、前言

 搭建程序运行环境是程序员必不可少的技能!
 今天买了个新的云服务器,需要搭建环境,借此机会自己也熟悉了一番搭建的流程,做个日记供以后参考。
 本篇大部分命令仅适合Centos7.x的系统使用。
lnmp是指Liunx、Nginx、Mysql和Php

 目录:
 (1)安装Nginx1.4
 (2)安装php7.2
 (3)安装Mariadb10.2
 (4)总结。

二、安装Nginx

 (1)防火墙设置,允许http、https通信、开启80、443、8080等端口(已开启的忽略此步骤)。

1
2
3
4
5
6
7
8
9
10
11
12
// 允许http通信
firewall-cmd --zone=public --add-service=http --permanent
// 允许https通信
firewall-cmd --zone=public --add-service=https --permanent
// 打开80端口
firewall-cmd --zone-public –add-port=80/tcp --permanent
// 打开443端口
firewall-cmd --zone-public –add-port=443/tcp --permanent
// 打开8080端口
firewall-cmd --zone-public –add-port=8080/tcp --permanent
// 重新加载配置
firewall-cmd --reload

 (2)下载依赖
1
yum intall pcre pcre-devel yum install pcre pcre-devel yum install zlib zlib-devel yum install openssl; openssl-devel yum install openssl;openssl-devel yum install openssl openssl-devel

 (3)配置官方yum源&安装Nginx
1
2
3
4
5
6
7
8
9
10
11
12
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

yum install nginx

# 默认主配置文件:/etc/nginx/conf.d

# 启动&停止
systemctl start/stop nginx.service
# 查看服务是否启动
ps aux|grep nginx
# 设置服务开机自启
systemctl enable nginx

注意:安装成功后无法访问,请检查防火墙设置,或在服务商后台配置安全组策略等。

二、安装php

 (1)配置yum源

1
2
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

 (2)安装php7.2及常用的扩展
1
yum install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-fpm php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml

 (3)启动php-fpm
1
2
3
systemctl stop/start php-fpm
# 设置开机启动
systemctl enable php-fpm

 (4)配置nginx支持php
bash
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
vim /etc/nginx/conf.d/default.conf

location / {
root /usr/share/nginx/html; //这是里应用目录和下面的root对应,可自定义
index index.html index.htm index.php;
}

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

三、安装mariadb10.2

 (1)配置yum源

1
2
3
4
5
6
7
8
9
10
11
12
# 进入
cd /usr/local/src
# 下载rpm包
wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
# 安装rpm
rpm -ivh mysql57-community-release-el7-11.noarch.rpm
# 安装 mysql-server
yum install -y mysql-server
# 启动
systemctl start mysqld
# 设置开机自启
systemctl enable mysqld

 (2)登入

1
2
3
4
5
6
7
8
9
10
11
12
13
# 由于MySQL从5.7开始不允许首次安装后,使用空密码进行登录,系统会随机生成一个密码以供管理员首次登录使用,这个密码记录在/var/log/mysqld.log文件中,使用下面的命令可以查看此密码:
cat /var/log/mysqld.log|grep 'A temporary password'
# 登录
mysql -u root -p
# 如报错 You must reset your password using ALTER USER statement before executing this statement
修改密码
use mysql;
SET PASSWORD = PASSWORD('daichongweb');

ALTER USER USER() IDENTIFIED BY 'daichongweb';

# 如报错Your password does not satisfy the current policy requirements
set global validate_password_policy=0; //修改密码验证为长度验证

四、总结

 总体来说使用yum安装比源码编译安装简单多了。
 再多的理论不如真正的实践。
 从出现问题,到分析问题,再到解决问题,你会从中学习到很多的知识,这也是我就算花钱也要买云服务器的原因。


 评论