前言
笔者也是第一次想着搭建一个博客,一开始也尝试用了宝塔面板,还是不顺手。就确定自己搭建一个服务器环境。这期间也遇到不少的坑,总算是自己解决了。所以也决定这这一整套搭建过程记录下来,分享给大家。
课前准备
在阿里云购买一台服务器,操作系统选择Centos7.7版
记住公网Ip(等会有用),然后就可以进行服务器搭建了。
安装Apache
一、安装
yum -y install httpd
二、开启apache服务
systemctl start httpd.service
三、设置apache服务开机启动
systemctl enable httpd.service
在本机浏览器中输入刚才记住公网ip地址,如果看到apache默认的页面–有Testing 123… 字样,便是成功安装了apache服务了。如图所示
访问应该是失败的,原因如下:
查了资料,说法是,CentOS7用的是Firewall-cmd,CentOS7之前用的是iptables防火墙;要想让外网能访问到apache主目录,就需要做以下的操
firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload
安装PHP
一、安装(默认版本)即可
yum -y install php
二、重启apache服务
systemctl restart httpd.service
三、测试php环境
建议安装xftp,在 /var/www/html/ 下放入index.php文件,内容如下:
xifp6
链接:https://pan.baidu.com/s/1vAdzb1py2BoYoo63vimX5w
提取码:9h2u
<?php
phpinfo();
?>
然后,在自己电脑浏览器输入 IP(公网)/index.php ,运行后会出现php的一些信息即为成功!图片如下。
安装MySQL
一、安装(这里选择的是MySQL的替代品mariadb,功能和命令都是一样的)
yum install mariadb mariadb-server mariadb-libs mariadb-devel
二、开启MySQL服务
systemctl start mariadb.service
三、设置开机启动MySQL服务
systemctl enable mariadb.service
四、数据库安全设置(也就是设置MySQL的密码)
mysql_secure_installation
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n
... Success!
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
五、登陆数据库测试
mysql -uroot -p (刚才设置的密码)
[root@yangpeng ~]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
PHP与MySQL进行关联
yum -y install php-mysql
安装常用的PHP模块
yum -y install php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel
环境测试首先重启一下Apache服务器,
systemctl restart httpd.service
在浏览器中输入:IP(公网)/index.php
就可以看到更新了数据库信息,如图所示
- 如果页面出现了PHP相关模块信息,紫色区块,则表示LAMP环境搭建成功。
- 如果已有php程序,只需要把整个php程序上传到 /var/www/html 目录下即可。