暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

linode vps 搭建LAMP实录-Centos 5.5

原创 Maksim 2020-05-14
1945

本来想在linode vps上面搭建LEMP(LNMP)配置Web服务环境的,但是Drupal 和Magento都不兼容支持Nginx这个轻量级Web服务器,虽然wordpress是支持Apache 和Nginx的。最后还是选择了在linode vps上部署CentOS 5.5,并安装应用最广泛的LAMP环境。

搭建lamp服务器之前先将centos 5.5 默认的iptables 防火墙开启并设置好。

安装Apache服务器配置虚拟主机
在linode vps 的Centos 5 profile中通过SSH 依次执行如下命令:

yum update
yum install httpd
cp /etc/httpd/conf/httpd.conf ~/httpd.conf.backup
vi /etc/httpd/conf.d/virtualhost.conf
以上linux指令依次是更新centos yum 源、安装apache 服务、备份apache的默认配置文件/etc/httpd/conf/httpd.conf到用户(root)目录下、在/etc/httpd/conf.d/目录下vi新建虚拟主机配置文件xxx.conf。比如新建virtualhost.conf文件配置如下(将ip和域名改成自己的):

NameVirtualHost 12.34.56.78:80

ServerAdmin lamp@farlee.info
ServerName farlee.info
ServerAlias www.farlee.info
DocumentRoot /srv/www/farlee.info/public_html/
<Directory “/srv/www/farlee.info/public_html/”>
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all

ErrorLog /srv/www/farlee.info/logs/error.log
CustomLog /srv/www/farlee.info/logs/access.log combined

注意第一个虚拟主机配置一般设置为当接收到未知server name的请求时(如恶意域名绑定到服务器-空主机头)的处理方法,所有ServerName未知的请求apache默认都会自动301重定向到第一个虚拟主机(可以设置正常的虚拟主机或设置返回403,500等状态码)。在httpd.conf 或virtualhost.conf中设置第一个虚拟主机都一样。
同样还可以新增其他虚拟主机,或新增监听其他端口:

listen 81
NameVirtualHost 12.34.56.78:81


更多请看apache.org的虚拟主机配置实例。另外由于apache访问日志大小会日益增大,尤其是对于大流量站点,因此一般CustomLog的设置还要优化,如使用apache自带的rotatelogs:

CustomLog “|/usr/local/apache/bin/rotatelogs /srv/www/farlee.info/access_log 10M” combined
其中rotatelogs bin目录会有不同,可以使用find / -name rotatelogs 搜索,ErrorLog也一样设置,或者不按大小,按时间生成日志,将10M改成时间,如每1天则改成86400。配置完毕保存后,新建这些虚拟主机网站目录:

mkdir -p /srv/www/farlee.info/public_html
mkdir -p /srv/www/farlee.info/logs
最好依次启动apache服务器、设置开启重启后自动启动apache:

/etc/init.d/httpd start
/sbin/chkconfig --levels 235 httpd on
/etc/init.d/httpd reload
开启httpd后若出现这种错误提示

Starting httpd: [warn] VirtualHost xxxx overlaps with VirtualHost xxxx, the first has precedence, perhaps you need a NameVirtualHost directive
[warn]NameVirtualHost 173.255.209.168:0 has no VirtualHosts
原因在于NameVirtualHost 12.34.56.78:80 的ip 后面没有指定80端口,同时将 不能用域名要改成ip,这里用域名会有问题。请看更多apache 虚拟主机配置实例。
若要查看需要的apache模块有没有安装,如mod_rewrite模块,有记录则表示已经加载了改模块:

grep ^LoadModule /etc/httpd/conf/httpd.conf
安装配置Mysql数据库
依次运行yum安装、设置roboot自动启动、开启mysql数据库:

yum install mysql-server
/sbin/chkconfig --levels 235 mysqld on
/etc/init.d/mysqld start
配置Mysql数据库
运行mysql_secure_installation配置mysql安全设置并设置root用户密码。然后就可以以root身份进入mysql创建用户和数据库了:

mysql -u root -p
CREATE DATABASE testdb;
CREATE USER ‘testuser’@localhost IDENTIFIED BY ‘testpassw’;
GRANT ALL PRIVILEGES ON testdb.* TO ‘testuser’@localhost;
exit 或 quit
PHP安装配置
yum安装后配置php配置文件:

yum install php php-pear php-mysql
vi /etc/php.ini
主要设置php.ini如下选项:

error_reporting = E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR
display_errors = Off
log_errors = On
error_log = /var/log/php.log
max_execution_time = 300
memory_limit = 64M
register_globals = Off
安装配置php.ini 完毕使用php -v命令查看php版本,发现Centos 5.5 默认自带安装的是php版本太低了,是php 5.1.6。Drupal 默认需要5.1.4+,Magento 需要5.2.13+,所以需要将centos 5.5 的php 5.1.6升级到新版本(5.2.6或5.3.5),详细查看这篇文章《Centos 5 升级默认php 5.1.6版本》。
安装php 模块
安装phpmyadmin需要php启用mcrypt和mbstring模块,但是Centos 5默认php 版本没有将这些模块编译进去,需要重新安装和编译。网上介绍的很多方法是下载这些php模块,然后重新phpize编译安装php这种方法。不过也有更简单一些的方法:使用上面介绍的php版本升级中remi的rpm源,直接yum 安装即可,不用编译。

yum install Libmcrypt
yum install mhash
yum install mcrypt
yum install php-mcrypt
yum install php-mbstring
安装完毕上传phpinfo()函数查看确认是否已经启用了相关php模块。

安装配置phpmyadmin
为方便数据库界面管理,还是安装phpmyadmin:

yum install phpmyadmin
phpmyadmin的默认安装目录是/usr/share/phpmyadmin,同时会在apache的配置文件目录中自动创建虚拟主机配置文件/etc/httpd/conf.d/phpMyAdmin.conf(区分大小写)。在这个配置文件中有设置:

Alias /phpMyAdmin /usr/share/phpMyAdmin
Alias /phpmyadmin /usr/share/phpMyAdmin
也就是我们可以通过绑定到apache服务器上的任何域名访问phpmyadmin,且不分大小写。
我们也可以加#注释掉上面的apache alias规则,然后在任何一个配置文件中(推荐virtualhost.conf)为phpmyadmin配置一个虚拟主机(或在81等新端口上配置),只能通过该虚拟主机(端口)访问phpmyadmin。如/srv/www/phpmyadmin/public_html配置为它的虚拟主机目录,为该目录创建软链接到phpmyadmin安装目录:

cd /srv/www/phpmyadmin/public_html
ln -s /usr/share/phpMyAdmin
修改phpmyadmin的配置设置vi /etc/phpMyAdmin/config.inc.php:

cfg[Servers][cfg['Servers'][i][‘auth_type’] = ‘cookie’;
$cfg[‘blowfish_secret’] = ‘随便填’;
若出现以下这些403没有权限的错误提示:

Forbidden
You don’t have permission to access /phpMyAdmin on this server.

[error] [client 12.34.56.78] client denied by server configuration: /usr/share/phpMyAdmin
[error] [client 12.34.56.78] Directory index forbidden by Options directive: /srv/www/phpmyadmin/public_html/
则要修改/etc/httpd/conf.d/phpMyAdmin.conf 文件中的deny allow规则,改为Allow from all,为安全考虑Allow from 你的ip。Directory 的Options 去掉indexes。

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论