首页 > 代码库 > linux下 php+nginx+mysql安装配置
linux下 php+nginx+mysql安装配置
我主要是用来安装php,以及nginx和php的交互。原文:http://www.cnblogs.com/lost-1987/articles/2642979.html
一 安装插件
可以选择YUM安装或者源码编译安装 gcc gcc-c++ zlib pcre pcre-devel libevent libevent-devel libxml2 libxml2-devel libmcrypt libmcrypt-devel curl-devel libpng-devel libtool-ltdl-devel gd-devel openssl openssl-devel ncurses-devel cmake mysql-devel
二 安装mysql
tar -zxvf mysql-5.5.25.tar.gz 将mysql包解压 然后放入你想要mysql的安装位置 如本例中的/usr/local/webserver/mysql cmake命令需要这个路径 cmake -DCMAKE_INSTALL_PREFIX=/usr/local/webserver/mysql -DMYSQL_DATADIR=/user/local/webserver/mysql/data -DSYSCONFDIR=/etc -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DMYSQL_UNIX_ADDR=/tmp/mysqld.sock -DMYSQL_TCP_PORT=3306 -DWITH_DEBUG=0 -DENABLED_LOCAL_INFILE=1 回车执行,执行完成后继续执行 make && make install #设置Mysql #在support-files目录中有五个配置信息文件(这里很重要,一定要根据自己的内存复制对应的cnf文件,否则mysql始终起不来): #my-small.cnf (内存<=64M) #my-medium.cnf (内存 128M) #my-large.cnf (内存 512M) #my-huge.cnf (内存 1G-2G) #my-innodb-heavy-4G.cnf (内存 4GB) cd /usr/local/webserver/mysql cp ./support-files/my-huge.cnf /etc/my.cnf vi /etc/my.cnf #在 [mysqld] 段增加 datadir = /usr/local/webserver/mysql/data wait-timeout = 30 max_connections = 512 default-storage-engine = MyISAM #在 [mysqld] 段修改 max_allowed_packet = 16M //添加mysql运行的用户和用户组 groupadd mysql useradd -g mysql mysql -s /bin/false -d /home/mysql //没有shell,不可本机登陆(安全起见) cd /usr/local/webserver/mysql chown -R root . chown -R mysql data chgrp -R mysql . //生成新的mysql授权表 //进入mysql安装目录下的脚本目录 cd /usr/local/webserver/mysql/scripts //利用mysql_install_db脚本生成新的mysql授权表 ./mysql_install_db --basedir=/usr/local/webserver/mysql --datadir=/usr/local/webserver/mysql/data --user=mysql //mysql server在系统中的服务项设置 //复制服务文件并修改 cd /usr/local/webserver/mysql/support-files cp mysql.server mysqld //修改mysqld basedir=/usr/local/webserver/mysql datadir=/usr/local/webserver/mysql/data mv mysqld /etc/init.d/mysqld chmod 755 /etc/init.d/mysqld 进行上述操作后 我们可以用 service mysqld start 来启动mysql服务了 //设置软连接使mysql, mysqldump, mysqladmin这三个bin命令能在shell中直接运行 sudo ln -s /usr/local/webserver/mysql/bin/mysql /usr/bin sudo ln -s /usr/local/webserver/mysql/bin/mysqldump /usr/bin sudo ln -s /usr/local/webserver/mysql/bin/mysqladmin /usr/bin rm -rf /etc/mysql/my.cnf 因为已经把此文件复制到/etc/my.cnf 如果不删除的话,mysql启动不起来。 /etc/init.d/mysqld start //设置root密码 mysqladmin -u root password "admin" //mysql数据库中文乱码解决 vi /etc/my.cnf //然后在[mysqld]配置选项下添加 character-set-server=utf8 //然后进入mysql cd /usr/local/webserver/mysql/bin mysql -u root -p 提示输入密码 mysql> show variables like ‘%character%‘;
三 安装Nginx
#tar zxvf nginx-0.8.24.tar.gz #cd nginx-0.8.24 #./configure --prefix=/usr/local/nginx //此处在本环节只需指定一个路径 #make && make install #/usr/local/nginx/sbin/nginx //启Nginx 编写服务脚本(服务脚本请勿复制 请在linux下写入 不然回车换行符会引起异常) vi /etc/init.d/nginx 把下列内容写入文件并保存 #!/bin/bash # nginx Startup script for the Nginx HTTP Server # this script create it by gcec at 2009.10.22. # it is v.0.0.1 version. # if you find any errors on this scripts,please contact gcec cyz. # and send mail to support at gcec dot cc. # # chkconfig: - 85 15 # description: Nginx is a high-performance web and proxy server. # It has a lot of features, but it‘s not for everyone. # processname: nginx # pidfile: /var/run/nginx.pid # config: /usr/local/nginx/conf/nginx.conf nginxd=/usr/local/nginx/sbin/nginx #这里设置为你安装nginx的执行文件位置 nginx_config=/usr/local/nginx/conf/nginx.conf #这里设置为你nginx的配置文件位置 nginx_pid=/var/run/nginx.pid RETVAL=0 prog="nginx" # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ ${NETWORKING} = "no" ] && exit 0 [ -x $nginxd ] || exit 0 # Start nginx daemons functions. start() { if [ -e $nginx_pid ];then echo "nginx already running...." exit 1 fi echo -n $"Starting $prog: " daemon $nginxd -c ${nginx_config} RETVAL=$? echo [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx return $RETVAL } # Stop nginx daemons functions. stop() { echo -n $"Stopping $prog: " killproc $nginxd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid } # reload nginx service functions. reload() { echo -n $"Reloading $prog: " #kill -HUP `cat ${nginx_pid}` killproc $nginxd -HUP RETVAL=$? echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) stop start ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|reload|status|help}" exit 1 esac exit $RETVAL 保存之后 赋予文件权限 chmod 755 /etc/init.d/nginx 我们就可以通过service nginx start 来启动服务了
四 安装php
create user and group for fpm(fastcgi process manager) groupadd fpm useradd --shell /sbin/nologin -g fpm fpm download, configure and install php5.3.3 wget http://www.php.net/distributions/php-5.3.3.tar.gz tar zxvf php-5.3.3.tar.gz cd php-5.3.3
[直接复制]
./configure --prefix=/usr/local/php --enable-fpm --with-fpm-user=fpm --with-fpm-group=fpm --with-config-file-path=/usr/local/php/lib --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --without-pdo-sqlite --without-sqlite3 --without-sqlite --with-curl --enable-mbstring --with-mhash --with-mcrypt --with-openssl --with-gd --enable-sockets --with-gettext --with-zlib --enable-zip --enable-soap --with-xmlrpc --with-freetype-dir=/usr/local/freetype --enable-gd-native-ttf --disable-fileinfo
中途错误需要yum install几个依赖包 [手敲版] ./configure --prefix=/usr/local/php --enable-fpm --with-fpm-user=fpm --with-fpm-group=fpm --with-config-file-path=/usr/local/php/lib #这里是配置放php.ini的存放位置 --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --without-pdo-sqlite --without-sqlite3 --without-sqlite --with-mysql-sock=/tmp/mysql.sock --with-curl --enable-mbstring --with-mhash --with-mcrypt --with-openssl --with-gd --enable-sockets --with-gettext --with-zlib --enable-zip --enable-soap --with-xmlrpc --with-freetype-dir=/usr/local/freetype --enable-gd-native-ttf --with-jpeg-dir=/usr/lib64 #64位系统lib64 32位系统lib32 make && make install make出现错误virtual memory exhausted: Cannot allocate memory,在configure上加上–disable-fileinfo如果出现mysql_config not found的错误 解决办法: vi /etc/profile 在最后加入一行 export PATH="$PATH:/usr/local/mysql/bin" 这个是你的mysql安装到的目录
五 配置php
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf 复制源码包里的php.ini-development到/usr/local/php/lib vi php-fpm.conf 找到"listen=" 修改为 listen = /dev/shm/php-fpm.sock (要求php版本5.3以上 该方式为使用sock文件监听) cp /backup/php-5.3.3/php.ini-development /usr/local/php/lib/php.ini 启动php /usr/local/php/sbin/php-fpm 如果设置路径正确,php.ini文件也存在,还无法加载php.ini的话 修改启动命令 /usr/local/php/sbin/php-fpm -c /etc/php.ini 编写服务脚本(服务脚本请勿复制 请在linux下写入 不然回车换行符会引起异常) touch /etc/init.d/phpfpm vim /etc/init.d/phpfpm 内容如下:
#!/bin/bash
start() {
/usr/local/php/sbin/php-fpm
/bin/echo ‘starting php listener ---[ok]‘
}
stop() {
/usr/bin/pkill php-fpm
/bin/echo ‘stopping php listener ---[ok]‘
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo ‘usage:start/stop/restart‘
exit 0
;;
esac
保存退出
然后 就能通过 service phpfpm start/stop/restart 来启动监听
六 配置Nginx
cat /usr/local/php/etc/php-fpm.conf 查看端口 为 127.0.0.1:9000 修改nginx配置文件 /usr/local/nginx/conf/nginx.conf # location / { //一定要注掉这部分,否则会不解析PHP文件,而会下载 了 # root html; # index index.html index.htm; #}
location ~ \.php { root www; #这是你网站的根目录 fastcgi_pass 127.0.0.1:9000; #这里指定了fastcgi进程侦听的端口,nginx就是通过这里与php交互的 #fastcgi_pass unix:/dev/shm/php-fpm.sock;
fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; #因为SCRIPT_FILENAME在配置中是写死的并没有随着$doucument_root变化而变化,我们可以修改SCRIPT_FILENAME配置如下 #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } 重启nginx服务 /usr/local/nginx/sbin/nginx -s reload 在/usr/local/nginx下创建www目录 mkdir www 新建一个index.php文件 cd www vim index.php 写入<?php echo phpinfo();?> 访问服务器 如果起作用就说明配置成功
七 设置php nginx mysql 自启动
我想在centos里不启用图形界面
那么选择系统运行级别为2或者3的 推荐3
在配置之前 我们先检查下 /etc/init.d中有没有我们mysql,php,nginx的服务脚本 如果没有的话 先配置再做下列操作
如以上 mysqld , nginx, phpfpm 这3个脚本都编写好 并且放入/etc/init.d下的话 我们来配置一下自启动
我想通过一个服务来启动这3个服务那么再写一个脚本就可以了
注意:system类型的服务都可以用service来启动,用chkconfig来add 和del
但是有些自己配置的服务在用chkconfig来配置到时候会提示: “service XX does not support chkconfig”
这一般都是script不符合格式造成的,解决如下,
在script开始加入两行内容即可:
# chkconfig: - 85 15
# description: this is a World Wide Web server.
mv /etc/init.d/mysqld /etc/init.d/webapp-mysqld mv /etc/init.d/nginx /etc/init.d/webapp-nginx mv /etc/init.d/phpfpm /etc/init.d/webapp-phpfpm touch /etc/init.d/webapp vim /etc/init.d/webapp 写入以下脚本
#!/bin/bash
# chkconfig: - 85 15
# description: this is a World Wide Web server.
ACTION=$1
if [ "$ACTION" = "" ] || [ "$ACTION" = "start" ];then
#start php listeners
/sbin/service webapp-phpfpm start
#start nginx service
/sbin/service webapp-nginx start
#start mysql service
/sbin/service webapp-mysqld start
echo "web applications[mysql,nginx,php] is running now !"
elif [ "$ACTION" = "stop" ];then
/sbin/service webapp-phpfpm stop
/sbin/service webapp-nginx stop
/sbin/service webapp-mysqld stop
echo ‘web application stopped‘
else
echo "use start or stop or none after your service command"
fi
添加系统服务开机启动
chkconfig --add webapp(注意在/etc/init.d下的服务脚本必须加入#chkconfig 和 #description的内容才能够在这里支持chkconfig命令,以上已经提到过)
chkconfig --level 3 webapp on
这样我们的lamp的架构就配置成功了
说明:
linux下 php+nginx+mysql安装配置