上次在虚拟机中装了 CentOS6,这次给他配个LAMP吧。
什么是LAMP?
LAMP是Linux,Apache,MySQL,PHP的缩写。这个教程主要是讲在centos安装有php和mysql支持的apache web服务器。
1、安装MySQL
Centos6的默认源中就已经有相应的包,直接安装即可。
yum install mysql mysql-server
设置MySQL开机启动,并启动服务。
chkconfig --levels 235 mysqld on /etc/init.d/mysqld start
设置MySQL root帐号密码:
mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
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):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n] <-- ENTER
New password: <-- 你的MySQL root密码
Re-enter new password: <-- 你的MySQL root密码
Password updated successfully!
Reloading privilege tables..
... Success!
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] <-- ENTER
... 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] <-- ENTER
... Success!
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] <-- ENTER
- 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] <-- ENTER
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
2、安装Apache
yum install httpd
设置系统启动时自启动Apache,并启动Apache
chkconfig --levels 235 httpd on /etc/init.d/httpd start
这里安装完毕后,你直接在你的浏览器键入http://IPAddress,你就应该会看到Apache的测试页面。
3、安装PHP
yum install php
重启Apache让PHP生效
/etc/init.d/httpd restart
为了让PHP支持MySQL,我们可以安装php-mysql软件包。你也可以安装其它的php模块。可以使用以下命令搜索可用的php模块:
yum search php
选择一些你需要的模块进行安装,并重启Apache
yum install php-mysql php-common php-mbstring php-gd php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc /etc/init.d/httpd restart
4、安装phpMyAdmin
phpMyAdmin是一个可视化管理你的MySQL数据库的软件。
因为官方的CentOS 5.5软件库没有phpMyAdmin,所以我们首先需要安装RPMforge的软件库(可以到https://rpmrepo.org/RPMforge/Using获取最新版的软件库)。
x86 64系统安装如下:
rpm -Uhv http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm
i386系统安装如下:
rpm -Uhv http://pkgs.repoforge.org/rpmforge-release/rpmforge-release-0.5.2-2.el5.rf.i386.rpm
执行如下命令安装phpMyAdmin
yum install phpmyadmin
接着我们配置phpMyAdmin,为了使用phpMyAdmin不仅仅只允许localhost连接,我们修改一下apache的配置。
vi /etc/httpd/conf.d/phpmyadmin.conf
填入如下内容:
# # Web application to manage MySQL # #<Directory "/usr/share/phpmyadmin"> # Order Deny,Allow # Deny from all # Allow from 127.0.0.1 #</Directory> Alias /phpmyadmin /usr/share/phpmyadmin Alias /phpMyAdmin /usr/share/phpmyadmin Alias /mysqladmin /usr/share/phpmyadmin
重启apache:
/etc/init.d/httpd restart
然后你就可以通过http://IPAddress/phpmyadmin/进入phpMyAdmin了
可是,我装完上面这些东西后,用宿主机去访问虚拟机的情况下,却无法访问。原因,我搜了一下,最终导致的是,没有配置过的iptables。
CentOS安装好,默认只打开了22端口,我们使用Apache最终是需要打开80端口,如果是需要用加密SSL的话,还需要打开443端口。
用命令打开这两个端口:
iptables -F iptables -A INPUT -p tcp --dport 22 -j ACCEPT /*允许包从22端口进入*/ iptables -A OUTPUT -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT /*允许从22端口进入的包返回*/ iptables -A OUTPUT -p udp --dport 53 -j ACCEPT iptables -A INPUT -p udp --sport 53 -j ACCEPT iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT /*允许本机访问本机*/ iptables -A OUTPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT iptables -A INPUT -p tcp -s 0/0 --dport 80 -j ACCEPT /*允许所有IP访问80端口*/ iptables -A OUTPUT -p tcp --sport 80 -m state --state ESTABLISHED -j ACCEPT iptables-save > /etc/sysconfig/iptables /*保存配置*/
然后,我在宿主机就能访问虚拟机的Apache了。
看来iptables很强大呀。
欢迎进入Linux世界,哈哈
好像我进入很久了,不过是桌面用户而已。
我还是喜欢用lnmp,霍霍,当然现在都是手动配置一下了,唉,没办法,一键包还是需要针对自己的vps设置一番才能工作好
后面需要学会配置nignx前端,apache后端。
看是看不懂了,哈哈~~
尝试一遍就懂了。