首先申请一台腾讯云主机,我这里是默认推荐的配置(1核1GB 1Mbps,系统是Ubuntu Server 16.04.1 LTS 64位);

点击控制台主机右侧的登录即可登陆上云服务器;或者下载PUTTY客户端远程登录;
putty下载地址:
https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html

1.更新 sudo apt-get update
Hit:1 http://mirrors.tencentyun.com/ubuntu xenial InRelease
Hit:2 http://mirrors.tencentyun.com/ubuntu xenial-security InRelease
Hit:3 http://mirrors.tencentyun.com/ubuntu xenial-updates InRelease
Reading package lists... Done2.安装apache
命令sudo apt-get install apache2
3.安装PHP
sudo apt-get install php
Creating config file /etc/php/7.0/fpm/php.ini with new version
Setting up php7.0 (7.0.18-0ubuntu0.16.04.1) ...
Setting up php (1:7.0+35ubuntu6) ...
Processing triggers for systemd (229-4ubuntu10) ...
Processing triggers for ureadahead (0.100.0-19) ...安装完成
命令php -v可以查看当前PHP版本
ubuntu@VM-223-146-ubuntu:~$ php -v
PHP 7.0.18-0ubuntu0.16.04.1 (cli) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.18-0ubuntu0.16.04.1, Copyright (c) 1999-2017, by Zend Technologies3.安装mysql
sudo apt-get install mysql-server
安装过程中会看到
这里输入默认的mysql密码,输入之后 按回车(ok是不能点的)
确认密码,输入之后按回车。
Processing triggers for libc-bin (2.23-0ubuntu3) ...
Processing triggers for systemd (229-4ubuntu10) ...
Processing triggers for ureadahead (0.100.0-19) ..安装完成。
sudo apt-get install mysql-client
client是客户端,可以直接操作SQL语言的
登陆数据库mysql -u root -p
输入密码之后按回车即可进入mysql-client
ubuntu@VM-223-146-ubuntu:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.18-0ubuntu0.16.04.1 (Ubuntu)
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> 这里如果由于权限等原因无法进入数据库,是由于腾讯云默认的登录账户是Ubuntu,需要修改登录用户为root.下面介绍如何获得root权限。
1.登录Ubuntu账户
2.修改root密码:sudo passwd root 回车
3.设置root的密码,要输入两遍

4.sudo vi/etc/ssh/sshd_config 回车,修改配置文件
5. 找到loginGraceTime 120 ,把PermitRootLogin 后面的修改为yes
修改方法:按下I进入INSERT模式,可以修改和编辑;修改完成后按ESC返回命令行模式,按下shift+:,wq保存退出。
7.重新启动ssh服务 sudo service ssh restart 回车
8.重新启动后就可以用root来登录服务器了。

输入sql语句,一定要以;结束,回车执行,没有分号会换行 。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> 退出mysql的命令:exit无分号
mysql> exit
Bye其他:
安装vim编辑器:apt-get install vim
修改index.html文件权限:sudo chmod 777 /var/www/html/index.html
vim /var/www/html/index.html删除文件所有内容:在命令行模式下,先按gg跳至文件首行,然后按dG就可以清空整个文件内容;或者退出vim,echo >> file也可以删除整个文件的内容。
在命令行模式下按I进入INSERT模式,输入以下内容:
<html>
<head>
</head>
<body>
<h1>hello world!</h1>
</body>
</html>shift+:,wq保存;
在浏览器输入主IP,可以看到浏览器页面出现了hello world!
回到上层文件夹的命令:cd ..
修改文件夹的权限: sudo chmod -R 777 html/
进入html文件夹cd html
vim index.php 新建并打开文件 index.php
<?php
phpinfo();
?> shift+:wq, 保存退出
安装Apache的PHP驱动:sudo apt-get instal libapache2-mod-php7.0

MySQL相关:
修改MySQL的root用户密码:(新版的user表中已经取消password字段,密码存放在authentication_string字段中)

创建新的数据库表:

修改index.php文件:
<?php
class DB{
protected $db;
public function __construct(){
try {
$this->db = new PDO("mysql:host=localhost;dbname=test;charset=utf8mb4","root","root");
$this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE,PDO::FETCH_ASSOC);
$this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); //禁用prepared statements的仿真效果
date_default_timezone_set('Asia/Shanghai');
} catch (PDOException $error) {
echo 'Connection failed: ' . $error->getMessage();
die();
}
}
public function insertDB($name,$age){
$sql = $this->db->prepare('INSERT INTO userinfo (username,age)VALUES(?,?)');
$sql->bindParam(1,$name);
$sql->bindParam(2,$age);
return $sql->execute();
}
}
$db = new DB();
echo $db->insertDB('info',24);
?>保存刷新浏览器
会显示 Connection failed:no driver
这表示PHP在链接mysql的过程中因为没有安装连接mysql的PHP扩展
用命令安装MySQL的扩展:sudo apt-get install php-mysql
安装之后,重启Apache:sudo apachectl restart
如果出现AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this messag
是因为默认的apache.conf没有设置servername
只需修改配置文件在最后加上 ServerName localhost:80 即可。不是重点BUG,这里先不做修改。
重启Apache之后刷新浏览器 会显示一个1,这表示脚本运行成功,并且把数据插入了数据库。
登陆数据库:
mysql> use test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from userinfo;
+--------+----------+------+
| userID | username | age |
+--------+----------+------+
| 1 | info | 24 |
+--------+----------+------+
1 row in set (0.00 sec)可以看到数据插入成功。
本文详细介绍了如何在腾讯云Ubuntu服务器上配置LAMP环境,包括安装Apache、PHP、MySQL,修改SSH配置以允许root登录,以及解决PHP连接MySQL的驱动问题。通过此教程,你可以成功搭建起一个基础的Web服务环境。

340

被折叠的 条评论
为什么被折叠?



