redhat下php环境搭建(linux apache mysql php)

在开始之前简单阐述下lamp架构:

Linux+Apache+Mysql+Perl/PHP/Python一组常用来搭建动态网站或者服务器的开源软件,本身都是各自独立的程序,但是因为常被放在一起使用,拥有了越来越高的兼容度,共同组成了一个强大的Web应用程序平台。

从网站的流量上来说,70%以上的访问流量是LAMP来提供的,LAMP是最强大的网站解决方案.想深入了解可自行Google之~

开始:

我个人比较喜欢redhat系统,像ubuntu企业版、debian、BSD之类的没怎么用。建议大家只研究好一种版本的系统即可,否则学的多了学不精!其次我喜欢yum安装软件,比起繁琐的源代码编译过程简单过了,可以略过复杂的依赖关系,源代码方式大家可自行Google研究之~

[root@localhost ~]# uname -a
Linux localhost.localdomain 2.6.18-194.el5 #1 SMP Tue Mar 16 21:52:39 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux

为测试时不出现必要的麻烦,先暂时关闭防火墙及Selinux,等熟悉后大家再开启。
[root@localhost ~]# service iptables stop         //关闭iptables防火墙
[root@localhost ~]# setenforce 0                      //临时关闭selinux

安装php环境:
[root@localhost ~]# yum install -y httpd php php-mysql mysql-server

httpd //apache服务器  php//php主程序  php-mysql//连接mysql数据库组件  mysql-server//mysql数据库主服务

重启apache、mysql服务,初始化mysql数据库、并且将其加为开机自启动:

[root@localhost ~]# service httpd restart && chkconfig httpd on && service mysqld restart && chkconfig mysqld on
停止 httpd:                                               [失败]
启动 httpd:                                               [确定]
停止 MySQL:                                               [失败]
初始化 MySQL 数据库: Installing MySQL system tables…
OK
Filling help tables…
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password ‘new-password’
/usr/bin/mysqladmin -u root -h localhost.localdomain password ‘new-password’

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!

The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
[确定]
启动 MySQL:                                               [确定]
[root@localhost ~]#

我们可以看到启动httpd  mysql服务成功,现在测试php环境是否成功:

[root@localhost ~]# cd /var/www/html/        //apache网站根目录
[root@localhost html]# vi phpinfo.php     //创建php环境测试页
代码:

<? phpinfo(); ?>

保存退出,访问测试:


看到上面信息表示php环境可以运行了。

下面修改下mysql的密码,默认mysql的用户名为root密码为空,这样是不安全的,修改root密码:

[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.77 Source distribution

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the buffer.

mysql>

mysql> use mysql;        //切换数据库
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> update user set password=PASSWORD(‘123′) where user=’root’;   //更改root密码为123
Query OK, 3 rows affected (0.07 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql>

下面用个实例看一下,安装discuz论坛:

将discuz安装程序目录upload放到/var/www/html/下
修改upload目录中相关目录权限:
Discuz需要以下这些目录及文件需要可读写权限
./config.inc.php
./templates
./attachments
./forumdata
./forumdata/cache
./forumdata/templates
./forumdata/threadcaches
./forumdata/logs
./uc_client/data/cache/

在浏览器中输入:http://172.17.1.11/upload/install/根据提示进行安装,在此就不再截图了,注意下配置mysql数据库的时候注意填写刚才修改的密码。其他根据情况填写直至安装完成后跳转到论坛首页。

下面看下apache的住配置文件httpd.conf:

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

简单的说下配置含义

ServerRoot “/etc/httpd”          //apache程序主目录
PidFile run/httpd.pid                 //pid文件路径

Timeout 120       //超时

KeepAlive Off   //保持一直连接

MaxKeepAliveRequests 100

KeepAliveTimeout 15

<IfModule prefork.c>
StartServers       8
MinSpareServers    5
MaxSpareServers   20
ServerLimit      256
MaxClients       256
MaxRequestsPerChild  4000
</IfModule>

以上都是性能优化,可以参考下apache文档:

https://www.rootop.org/ApacheManual/Apache2.2_zh_CN/

Listen 80        //监听端口  web服务器都是工作在80端口

Include conf.d/*.conf            //加载conf.d下的所有.conf配置文件

DocumentRoot “/var/www/html”       //网站的根目录

#NameVirtualHost *:80          //以下是虚拟主机的配置。我们大部分用的是基于域名的虚拟主机
#
# NOTE: NameVirtualHost cannot be used without a port specifier
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.
#

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
#<VirtualHost *:80>
#    ServerAdmin webmaster@dummy-host.example.com
#    DocumentRoot /www/docs/dummy-host.example.com
#    ServerName dummy-host.example.com
#    ErrorLog logs/dummy-host.example.com-error_log
#    CustomLog logs/dummy-host.example.com-access_log common
#</VirtualHost>

大家可自行搜索本博客下的虚拟主机配置文章。

php的配置文件位于/etc/php.ini

大部分配置都有相关的解释:

extension_dir = “/usr/lib64/php/modules”     //php模块路径

display_errors = Off     //打开错误调试,开发比较实用
; Whether to allow HTTP file uploads.
file_uploads = On           //允许上传

#extension=msql.so       //注意下这个部分,在我们编译进php需要的模块时候,需要在此加载,比如比较常用的json.so模块。

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
;upload_tmp_dir =

; Maximum allowed size for uploaded files.
upload_max_filesize = 2M          //上传大小

简单说这些,大家可Google之~

mysql的住配置文件:

[root@localhost ~]# vi /etc/my.cnf           //主配置文件

默认的配置文件比较简单

[mysqld]
datadir=/var/lib/mysql             //mysql程序目录
socket=/var/lib/mysql/mysql.sock
user=mysql
# Default to using old password format for compatibility with mysql 3.x
# clients (those using the mysqlclient10 compatibility package).
old_passwords=1

# Disabling symbolic-links is recommended to prevent assorted security risks;
# to do so, uncomment this line:
# symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

在mysq安装好后会有几个根据数据库运行大小有几个不同的配置文件,主要是性能方面配置的不同:

[root@localhost ~]# cd /usr/share/mysql/
[root@localhost mysql]# ll

-rw-r–r– 1 root root   4972 2010-01-30 my-huge.cnf
-rw-r–r– 1 root root  20970 2010-01-30 my-innodb-heavy-4G.cnf
-rw-r–r– 1 root root   4948 2010-01-30 my-large.cnf
-rw-r–r– 1 root root   4955 2010-01-30 my-medium.cnf
-rw-r–r– 1 root root   2526 2010-01-30 my-small.cnf

先简单写到这儿,等想到了再补充。

更改php上传限制(phpmyadmin Import上传限制)

当对sql脚本通过phpmyadmin导入时,会有个提示,不能上传大于2M的文件,要不会报错。更改此限制方法:

编辑php.ini配置文件

;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;

; Whether to allow HTTP file uploads.
; http://www.php.net/manual/en/ini.core.php#ini.file-uploads
file_uploads = On

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
; http://www.php.net/manual/en/ini.core.php#ini.upload-tmp-dir
;upload_tmp_dir =

; Maximum allowed size for uploaded files.
; http://www.php.net/manual/en/ini.core.php#ini.upload-max-filesize

upload_max_filesize = 5M
memory_limit =   10M  //要大于upload_max_filesize
post_max_size =   10M   //要大于upload_max_filesize

phpmyadmin帮助文档:
The first things to check (or ask your host provider to check) are the values of upload_max_filesize, memory_limit and post_max_size in the php.ini configuration file. All of these three settings limit the maximum size of data that can be submitted and handled by PHP. One user also said that post_max_size and memory_limit need to be larger than upload_max_filesize.

重启apache生效!

Linux环境下配置apache支持WordPress伪静态

修改 httpd.conf 配置文件(红色部分):

# Each directory to which Apache has access can be configured with respect
# to which services and features are allowed and/or disabled in that
# directory (and its subdirectories).
#
# First, we configure the “default” to be a very restrictive set of
# features.
#
<Directory />
    Options FollowSymLinks
    AllowOverride All
</Directory>

#
# Note that from this point forward you must specifically allow
# particular features to be enabled – so if something’s not working as
# you might expect, make sure that you have specifically enabled it
# below.
#

#
# This should be changed to whatever you set DocumentRoot to.
#
<Directory “/var/www/html”>

#
# Possible values for the Options directive are “None”, “All”,
# or any combination of:
#   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that “MultiViews” must be named *explicitly* — “Options All”
# doesn’t give it to you.
#
# The Options directive is both complicated and important.  Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#
    Options Indexes FollowSymLinks

#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be “All”, “None”, or any combination of the keywords:
#   Options FileInfo AuthConfig Limit
#
    AllowOverride All

LoadModule rewrite_module modules/mod_rewrite.so

然后在wordpress主目录下建立.htaccess 权限设置为777便于 永久链接设置 伪静态规则能写入.htaccess文件

然后在后台管理中,找到设置-永久链接,有几项可以自己选。

自定义的话可以这样:/pages/%post_id%/.html  根据提交的文章id生成静态页。便于收录。

 

[notice] child pid xxx exit signal File size limit exceeded (25)

    从google上找到的资料解释,说是apache的日志文件大于2GB,不能处理。

This error indicates that Apache is trying to handle a file which exceeds the maximum permitted filesize. I needed to fix this quickly this evening on an Apache box, but I have proper rotation on all access and error logs, so couldn’t work out where the sink was.. Eventually it turned out to be the rewrite log.

Don’t let this happen to you. Be sure to rotate all apache’s logs properly, otherwise you essentially leave your Apache installation open to DOS attacks. It might take a lot of requests to create 2GB of Apache rewrite logs (and in this case it did!), but on a heavily SEO’d site with a high Rewrite Log Level value in Apache, it *will happen sooner or later.

最好是对apache的访问及错误日志进行日志分割,每天一新日志。

apache日志分割:https://www.rootop.org/?p=373

PS:find的简单两个用法:

find / -size +1000000k     find / -name test.sh

以大小搜索文件                    以文件名搜索文件