【手记】CentOS 7 搭建 LAMP 环境

一、配置防火墙,开启 80 端口、3306 端口

CentOS7.0 默认使用的是 firewall 作为防火墙,这里改为 iptables 防火墙。

1、关闭 firewall:

1
2
systemctl stop firewalld.service# 停止 firewall
systemctl disable firewalld.service# 禁止 firewall 开机启动

2、安装 iptables 防火墙

1
yum install iptables-services# 安装
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
vi/etc/sysconfig/iptables # 编辑防火墙配置文件
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT[0:0]
:FORWARD ACCEPT[0:0]
:OUTPUT ACCEPT[0:0] -A INPUT-m state--state ESTABLISHED,RELATED-j ACCEPT
-A INPUT-p icmp-j ACCEPT
-A INPUT-i lo-j ACCEPT
-A INPUT-m state--state NEW-m tcp-p tcp--dport22-j ACCEPT
-A INPUT-m state--state NEW-m tcp-p tcp--dport80-j ACCEPT
-A INPUT-m state--state NEW-m tcp-p tcp--dport3306-j ACCEPT
-A INPUT-j REJECT--reject-withicmp-host-prohibited
-A FORWARD-j REJECT--reject-withicmp-host-prohibited
COMMIT :wq!# 保存退出
1
2
systemctl restart iptables.service# 最后重启防火墙使配置生效
systemctl enable iptables.service# 设置防火墙开机启动

参考资料:http://www.cncentos.com/thread-4031-1-1.html

二、安裝 HTTP、PHP、MariaDB 與相關工具

1
2
root# yum install httpd mariadb-server mariadb php php-mbstring php-mysql php-gd 
root# yum install wget unzip
1
2
3
4
root# systemctl start mariadb.service 
root# systemctl start httpd.service
root# systemctl enable mariadb.service
root# systemctl enable httpd.service

参考资料:http://technote.aven-network.com/576/centos-7-%E5%BF%AB%E9%80%9F%E8%A8%AD%E5%AE%9A-lamp%EF%BC%88linux-apache-mariadb-php%EF%BC%89

三、配置 MySQL

設定 MariaDBInnodb
修改 / etc/my.cnf 檔案,在 [mysqld] 下新增

1
innodb_file_per_table=1

四、测试 PHP

建立 info.php 测试文件

1
vi /var/www/html/info.php

写入以下代码

1
<?php phpinfo();?>

:wq# 保存退出
参考资料:https://www.howtoforge.com/apache_php_mysql_on_centos_7_lamp

五、配置 MySQL

1
root# mysqladmin -u root password 'your-password'

参考资料:http://technote.aven-network.com/576/centos-7-%E5%BF%AB%E9%80%9F%E8%A8%AD%E5%AE%9A-lamp%EF%BC%88linux-apache-mariadb-php%EF%BC%89

【使用 sftp 上传】
sftp 连接成功之后常用操作命令如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
help/? 打印帮助信息。
pwd 查看远程服务器当前目录;
lpwd 查看本地系统的当前目录。
cd <dir> 将远程服务器的当前目录更改为 & lt;dir>;
lcd <dir> 将本地系统的当前目录更改为 & lt;dir>。
ls 显示远程服务器上当前目录的文件名;
ls -l 显示远程服务器上当前目录的文件详细列表;
ls <pattern> 显示远程服务器上符合指定模式 & lt;pattern> 的文件名;
ls -l <pattern> 显示远程服务器上符合指定模式 & lt;pattern> 的文件详细列表。
lls 显示本地系统上当前目录的文件名;
lls 的其他参数与 ls 命令的类似。
get <file> 下载指定文件 & lt;file>;
get <pattern> 下载符合指定模式 & lt;pattern> 的文件。
put <file> 上传指定文件 & lt;file>;
get <pattern> 上传符合指定模式 & lt;pattern> 的文件。
progress 切换是否显示文件传输进度。
mkdir <dir> 在远程服务器上创建目录;
lmkdir <dir> 在本地系统上创建目录。
exit/quit/bye 退出 sftp。
! 启动一个本地 shell。
! <commandline> 执行本地命令行。

其他命令还有:chgrp, chmod, chown, ln, lumask, rename, rm, rmdir, symlink, version。