阿里云ECS服务器Linux环境下配置php服务器(一)--基础配置篇

时间:2024-04-14 21:40:53

首先你得买个阿里云ecs服务器,这个直接买 比较简单就不做过多叙述了
在我说之前 有以下几点需要说明
1. 按照以下步骤操作完,可能ip直接访问(默认80端口)会提示无法访问,请检查网络设置和防火墙,我的解决办法

参考:阿里云esc云服务器IP不能访问的解决办法

2.直接使用以下方案操作 windows可能还要安装ssh mac直接可以操作 建议在windows下使用xshell 和xftp 两个都安装一下 百度搜一下就可以了,直接下载安装
命令基本差不多

会提示输入用户名密码 就是你的阿里云ecs服务器的账号密码 不懂的后面有,可以往下翻

这里是使用xftp的地方(红圈标识),windows使用xftp的好处可以将文件直接下下来,在本地修改完了,再上传到服务器 ,在我看来windows下用命令行操作 始终用起来不如linux和unix下爽 当然mac和linux下也是可以把文件通过命令方式下下来 改好了 再传上去
不喜欢敲命令的,可以这么操作

 

 

好,废话不多说,进入正题

买服务器后,登录你的账号,查找购买的服务器的公网ip: 
这里写图片描述

然后打开你的终端(windows系统请cmd打开控制台),输入:

ssh root@公网IP

注意,root其实是你的登录名,这个在你购买服务器的时候已经填写过了,公网ip就是上面说的ip。 
然后输入密码,就登录进来了,密码也在你购买的时候设置过。 
这里写图片描述

好了,登录成功后,下一步就开始安装软件了,我们需要安装的软件有apache,php和MySQL

ps:如果你购买的是北京的服务器,有个安全组需要设置,我全部用的默认设置,暂时还没发现会有什么影响。

首先关闭SELINUX(SELINUX是一个安全子系统,它能控制程序只能访问特定文件。如果不关闭,你可能访问文件受限):

vi /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
:wq!#保存退出
shutdown -r now#重启系统

然后安装apache

yum install httpd #根据提示,输入Y安装即可成功安装
/etc/init.d/httpd start#启动Apache
备注:Apache启动之后会提示错误:
正在启动 httpd:httpd: Could not reliably determine the server\'s fully qualif domain name, using ::1 for ServerName
解决办法:
vi /etc/httpd/conf/httpd.conf #编辑
找到 #ServerName www.example.com:80
修改为 ServerName www.jbaobao.net:80 #这里设置为你自己的域名,如果没有域名,可以设置为localhost
:wq! #保存退出
chkconfig httpd on #设为开机启动
/etc/init.d/httpd restart #重启Apache

 

安装mysql

yum install mysql mysql-server #询问是否要安装,输入Y即可自动安装,直到安装完成
/etc/init.d/mysqld start #启动MySQL
chkconfig mysqld on #设为开机启动
cp /usr/share/mysql/my-medium.cnf /etc/my.cnf #拷贝配置文件(注意:如果/etc目录下面默认有一个my.cnf,直接覆盖即可)

 

然后为mysql的root账号设置密码(默认的是空)

mysql_secure_installation回车

这里需要注意的是,如果你是新安装的mysql,会弹出如下提示:
In order to log into MySQL to secure it, we\'ll need the current password for the root user.  If you\'ve just installed MySQL, 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): 
大概意思就是如果你是新安装的话,你的默认密码是空,直接按enter键就可以了

然后设置新的密码,输入两次。

再然后,会有若干个提示:
By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL 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!
大概意思是,mysql会默认创建一个匿名用户,方便你测试什么的,现在问你要不要删掉它,果断删掉


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] y
 ... Success!
大概意思是,root用户默认只能访问localhost,以防止有人猜你的密码。。。问你是否禁止root登陆,也选yes,虽然基本上不会有人来猜吧。。。


By default, MySQL 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
大概意思是,mysql默认创建了一个名为test的数据库,这个库任何人都可以访问,问你是不是要把它删掉,也删掉。


Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y

大概意思是,上面所有的修改是否马上生效,选y

总之就是一路Yes。。。。
最后出现:Thanks for using MySQL!

MySql密码设置完成,重新启动 MySQL:
/etc/init.d/mysqld restart #重启

最后一步,安装php

yum install php #根据提示输入Y直到安装完成
#安装PHP组件,使 PHP5 支持 MySQL
yum install php-mysql php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt #这里选择以上安装包进行安装,根据提示输入Y回车
/etc/init.d/mysqld restart #重启MySql
/etc/init.d/httpd restart #重启Apche

OK,到这里,所有的软件都安装完了,现在测试一下。 
默认的,你会有一个www的文件夹,里面有个html的文件夹,你的默认访问路径就是这里。

cd /var/www/html
vi index.php #编辑输入下面内容
<?php
echo "hello my server!";
?>
:wq! #保存退出

 

然后在浏览器里键入你的公网ip,顺利的话,你就开启自己第一个页面了!~


本文部分内容转自:http://blog.****.net/l00149133/article/details/50434004#comments