PHP项目部署到云服务器(CentOS+HTTP+PHP+MYSQL)

如何将写好的项目部署到云服务器,让外界可以通过互联网访问项目网站。以下使用的是腾讯云服务器操作,系统环境为CentOS7.9。

一、购买云服务器

打开腾讯云官网,注册好自己的平台账号。如果是新人的话去新人专区购买服务器,性价比高。

这里可以选的服务器有轻量云服务器、云服务器CVM。两者都可以指定自己的操作系统,配置自定义环境上没有区别。轻量云服务器带宽比云服务器CVM大,但是轻量云服务器有流量限制,云服务器CVM是没有流量限制的,但轻量云性价比高。以下操作轻量云服务器还是云服务器CVM可以部署。

二、开放安全组(防火墙)

有效去防止入侵者去攻击服务器,不建议全部端口开放。

这里把以下端口开放

80WEB服务
443HTTPS服务
3306MySQL
22登录服务器
ICMPPing

三、部署云服务器环境

 1,安装HTTPD服务器

[root@localhost ~]# yum install httpd -y

2,启用HTTPD服务并设置开机启动

[root@localhost ~]# systemctl start httpd

[root@localhost ~]# systemctl enable httpd 

3,测试网站是否能访问

 以上访问成功接下来做以下操作

4,安装PHP服务器

[root@localhost ~]# yum install php php-mysql -y

5,配置HTTPD

[root@localhost ~]# echo "AddType application/x-httpd-php .php" >> /etc/httpd/conf/httpd.conf 

[root@localhost ~]# vi /etc/httpd/conf/httpd.conf 

#找到

DirectoryIndex index.html

后面追加 index.php 如下

 保存退出

6,重启HTTPD服务器

[root@localhost ~]# systemctl restart httpd 

7,测试php文件可用

[root@localhost ~]# echo "<?php phpinfo(); ?>" > /var/www/html/index.php

 

注意:/var/www/html/目录是存放你的项目目录!

以上访问没有问题接下来可以做mysql配置了

8,安装mysql服务器(mariadb)【你的项目中不使用数据库可以跳了到第12个步骤就可以完成网站上线】

[root@localhost ~]# yum install mariadb mariadb-server -y

9,启用服务器并设置开机启动

[root@localhost ~]# systemctl start mariadb.service   
[root@localhost ~]# systemctl enable mariadb.service 

10,mysql数据库初始化

[root@localhost ~]# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

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                #是否设置密码,输入Y或者直接车
New password:                                 #输入root密码
Re-enter new password:                         #再输入root密码
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]                        #是否删除匿名用户,回车
 ... 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                #是否禁止root远程登录,输N否
 ... skipping.

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]         #是否删除test数据库,回车
 - 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]                 #是否重新加载权限表,回车
 ... 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!
[root@localhost ~]# 

11,测试数据库连接

[root@localhost ~]# mysql -uroot -p123456 -h127.0.0.1
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)]> 

以上设置了mysql密码 数据库地址为:localhost 用户名为:root 密码为: 123456    (项目包里的数据库连接配置写成这个)

四、部署PHP项目到服务器

12,上传文件到CentOS

这里使用的是SecureCRT演示

 ​

 以上是上传文件到CentOS操作,如果还不会,建议重开。

13,授权root用户远程访问

MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

14,创建一个数据库并导入(这里按照你的项目去创建)【这里不会使用的话可以用数据库工具可视化创建】

MariaDB [(none)]> create database room;    #创建数据名
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> use room;                        #进入这个数据库
Database changed
MariaDB [room]> source /var/www/html/room.sql                #导入数据库

导入完成,网站就部署部署完毕。

防止别人可以攻击服务器,去腾讯云上安全规则(防火墙)除了80,443端口其他都禁用。有需要上服务器的时候去开启服务。

  • 6
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值