Maison >développement back-end >tutoriel php >linux下 php+nginx+mysql安装配置
我主要是用来安装php,以及nginx和php的交互。
一 安装插件
<span>可以选择YUM安装或者源码编译安装 </span><span>gcc</span><span>gcc</span>-c++<span> zlib pcre pcre-devel libevent libevent</span>-<span>devel libxml2 libxml2</span>-<span>devel libmcrypt libmcrypt</span>-<span>devel curl</span>-<span>devel libpng</span>-<span>devel libtool</span>-ltdl-<span>devel gd</span>-<span>devel openssl openssl</span>-<span>devel ncurses</span>-<span>devel cmake mysql-devel </span>
二 安装mysql
<span>tar</span> -zxvf mysql-<span>5.5</span>.<span>25</span>.<span>tar</span><span>.gz 将mysql包解压 然后放入你想要mysql的安装位置 如本例中的/usr/local/webserver/mysql cmake命令需要这个路径</span><span>cmake \ </span>-DCMAKE_INSTALL_PREFIX=/usr/local/webserver/<span>mysql \ </span>-DMYSQL_DATADIR=/user/local/webserver/mysql/<span>data \ </span>-DSYSCONFDIR=/<span>etc \ </span>-DEXTRA_CHARSETS=<span>all \ </span>-DDEFAULT_CHARSET=<span>utf8 \ </span>-DDEFAULT_COLLATION=<span>utf8_general_ci \ </span>-DWITH_INNOBASE_STORAGE_ENGINE=<span>1</span><span> \ </span>-DWITH_ARCHIVE_STORAGE_ENGINE=<span>1</span><span> \ </span>-DWITH_BLACKHOLE_STORAGE_ENGINE=<span>1</span><span> \ </span>-DWITH_FEDERATED_STORAGE_ENGINE=<span>1</span><span> \ </span>-DWITH_PARTITION_STORAGE_ENGINE=<span>1</span><span> \ </span>-DWITH_PERFSCHEMA_STORAGE_ENGINE=<span>1</span><span> \ </span>-DMYSQL_UNIX_ADDR=/tmp/<span>mysqld.sock \ </span>-DMYSQL_TCP_PORT=<span>3306</span><span> \ </span>-DWITH_DEBUG=<span>0</span><span> \ </span>-DENABLED_LOCAL_INFILE=<span>1</span><span> 回车执行,执行完成后继续执行 </span><span>make</span> && <span>make</span><span>install</span><span>#设置Mysql #在support</span>-<span>files目录中有五个配置信息文件(这里很重要,一定要根据自己的内存复制对应的cnf文件,否则mysql始终起不来): #my</span>-small.cnf (内存64M) #my-<span>medium.cnf (内存 128M) #my</span>-<span>large.cnf (内存 512M) #my</span>-huge.cnf (内存 1G-<span>2G) #my</span>-innodb-heavy-<span>4G.cnf (内存 4GB) cd </span>/usr/local/webserver/<span>mysql </span><span>cp</span> ./support-files/my-huge.cnf /etc/<span>my.cnf vi </span>/etc/<span>my.cnf #在 [mysqld] 段增加 datadir </span>= /usr/local/webserver/mysql/<span>data </span><span>wait</span>-timeout = <span>30</span><span> max_connections </span>= <span>512</span><span> default</span>-storage-engine =<span> MyISAM #在 [mysqld] 段修改 max_allowed_packet </span>=<span> 16M </span><span>//</span><span>添加mysql运行的用户和用户组</span><span>groupadd mysql useradd </span>-g mysql mysql -s /bin/<span>false</span> -d /home/mysql <span>//</span><span>没有shell,不可本机登陆(安全起见)</span><span> cd </span>/usr/local/webserver/<span>mysql </span><span>chown</span> -<span>R root . </span><span>chown</span> -<span>R mysql data </span><span>chgrp</span> -<span>R mysql . </span><span>//</span><span>生成新的mysql授权表 </span><span>//</span><span>进入mysql安装目录下的脚本目录</span> cd /usr/local/webserver/mysql/<span>scripts </span><span>//</span><span>利用mysql_install_db脚本生成新的mysql授权表</span> ./mysql_install_db --basedir=/usr/local/webserver/mysql --datadir=/usr/local/webserver/mysql/data --user=<span>mysql </span><span>//</span><span>mysql server在系统中的服务项设置 </span><span>//</span><span>复制服务文件并修改</span> cd /usr/local/webserver/mysql/support-<span>files </span><span>cp</span><span> mysql.server mysqld </span><span>//</span><span>修改mysqld</span> basedir=/usr/local/webserver/<span>mysql datadir</span>=/usr/local/webserver/mysql/<span>data </span><span>mv</span> mysqld /etc/init.d/<span>mysqld </span><span>chmod</span><span>755</span> /etc/init.d/<span>mysqld 进行上述操作后 我们可以用 service mysqld start 来启动mysql服务了 </span><span>//</span><span>设置软连接使mysql, mysqldump, mysqladmin这三个bin命令能在shell中直接运行</span><span>sudo</span><span>ln</span> -s /usr/local/webserver/mysql/bin/mysql /usr/<span>bin </span><span>sudo</span><span>ln</span> -s /usr/local/webserver/mysql/bin/mysqldump /usr/<span>bin </span><span>sudo</span><span>ln</span> -s /usr/local/webserver/mysql/bin/mysqladmin /usr/<span>bin </span><span>rm</span> -rf /etc/mysql/my.cnf 因为已经把此文件复制到/etc/<span>my.cnf 如果不删除的话,mysql启动不起来。 </span>/etc/init.d/<span>mysqld start </span><span>//</span><span>设置root密码</span> mysqladmin -u root password <span>"</span><span>admin</span><span>"</span><span>//</span><span>mysql数据库中文乱码解决</span> vi /etc/<span>my.cnf </span><span>//</span><span>然后在[mysqld]配置选项下添加</span> character-set-server=<span>utf8 </span><span>//</span><span>然后进入mysql</span> cd /usr/local/webserver/mysql/<span>bin mysql </span>-u root -<span>p 提示输入密码 mysql</span>> show variables like <span>'</span><span>%character%</span><span>'</span>;
三 安装Nginx
#<span>tar</span> zxvf nginx-<span>0.8</span>.<span>24</span>.<span>tar</span><span>.gz #cd nginx</span>-<span>0.8</span>.<span>24</span><span> #.</span>/configure --prefix=/usr/local/nginx <span>//</span><span>此处在本环节只需指定一个路径</span> #<span>make</span> && <span>make</span><span>install</span><span> #</span>/usr/local/nginx/sbin/nginx <span>//</span><span>启Nginx</span><span> 编写服务脚本(服务脚本请勿复制 请在linux下写入 不然回车换行符会引起异常) vi </span>/etc/init.d/<span>nginx 把下列内容写入文件并保存 <span>#</span></span><span>!/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</span><span><span>exit $RETVAL </span> 保存之后 赋予文件权限 chmod 755 /etc/init.d/nginx 我们就可以通过service nginx start 来启动服务了 </span>
四 安装php
create user and group <span>for</span><span> fpm(fastcgi process manager) groupadd fpm useradd </span>--shell /sbin/nologin -<span>g fpm fpm download, configure and </span><span>install</span> php5.<span>3.3</span><span>wget</span> http:<span>//</span><span>www.php.net/distributions/php-5.3.3.tar.gz</span><span>tar</span> zxvf php-<span>5.3</span>.<span>3</span>.<span>tar</span><span>.gz cd php</span>-<span>5.3</span>.<span>3 </span>
<span>[直接复制] ./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 </span> <pre class="brush:php;toolbar:false"><span>--disable-fileinfo</span>
<span>中途错误需要yum install几个依赖包 </span><span> [手敲版] .</span>/configure --prefix=/usr/local/<span>php \ </span>--enable-<span>fpm \ </span>--with-fpm-user=<span>fpm \ </span>--with-fpm-group=<span>fpm \ <span>--with-config-file-path=/usr/local/php/lib #这里是配置放php.ini的存放位置</span></span>--with-mysql=<span>mysqlnd \ </span>--with-mysqli=<span>mysqlnd \ </span>--with-pdo-mysql=<span>mysqlnd \ </span>--without-pdo-<span>sqlite \ </span>--without-<span>sqlite3 \ </span>--without-<span>sqlite \ </span>--with-mysql-sock=/tmp/<span>mysql.sock \ </span>--with-<span>curl \ </span>--enable-<span>mbstring \ </span>--with-<span>mhash \ </span>--with-<span>mcrypt \ </span>--with-<span>openssl \ </span>--with-<span>gd \ </span>--enable-<span>sockets \ </span>--with-<span>gettext \ </span>--with-<span>zlib \ </span>--enable-<span>zip</span><span> \ </span>--enable-<span>soap \ </span>--with-<span>xmlrpc \ --with-freetype-dir=/usr/local/freetype \ --enable-gd-native-ttf \ <span>--with-jpeg-dir=/usr/lib64 #64位系统lib64 32位系统lib32 </span></span><span>make</span> && <span>make</span><span>install make出现错误</span>virtual memory exhausted: Cannot allocate memory<span>,在configure上加上–disable-fileinfo</span><span></span> <pre class="brush:php;toolbar:false">如果出现mysql_config not found的错误 解决办法: vi /etc/profile 在最后加入一行 export PATH="$PATH:/usr/local/mysql/bin" 这个是你的mysql安装到的目录
五 配置php
<span>cp</span> /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf 复制源码包里的php.ini-development到/usr/local/php/lib <span> vi php-fpm.conf 找到"listen=" 修改为 listen = /dev/shm/php-fpm.sock (要求php版本5.3以上 该方式为使用sock文件监听) cp</span> /backup/php-5.3.3/php.ini-development /usr/local/php/lib/php.ini 启动php /usr/local/php/sbin/php-fpm <span>如果设置路径正确,php.ini文件也存在,还无法加载php.ini的话 修改启动命令 /usr/local/php/sbin/php-fpm -c /etc/php.ini</span>编写服务脚本(服务脚本请勿复制 请在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/<span>nginx.conf </span><span># location / { //一定要注掉这部分,否则会不解析PHP文件,而会下载 了 # root html; # index index.html index.htm; #} </span><span></span>
<span><span>location </span></span><span>~ \.php { root www; #这是你网站的根目录 fastcgi_pass 127.0.0.1:9000; #这里指定了fastcgi进程侦听的端口,nginx就是通过这里与php交互的 #</span>fastcgi_pass unix:/dev/shm/php-fpm.sock;
<span> fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME <span>$document_root</span>/</span><span><span>$fastcgi_script_name; #因为SCRIPT_FILENAME在配置中是写死的并没有随着$doucument_root变化而变化,我们可以修改SCRIPT_FILENAME配置如下 </span></span> #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;<span><span> include fastcgi_params; }</span> 重启nginx服务 /usr/local/nginx/sbin/nginx -s reload 在/usr/local/nginx下创建www目录 mkdir www 新建一个index.php文件 cd www vim index.php 写入<?php echo phpinfo();?> 访问服务器 如果起作用就说明配置成功</span>
七 设置php nginx mysql 自启动
<span>我想在centos里不启用图形界面 那么选择系统运行级别为2或者3的 推荐3 </span>在配置之前 我们先检查下 /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.
<span>mv /etc/init.d/mysqld /etc/init.d/webapp-mysqld</span><span>mv /etc/init.d/nginx /etc/init.d/webapp-nginx</span><span>mv /etc/init.d/phpfpm /etc/init.d/webapp-phpfpm</span><span>touch /etc/init.d/webapp</span><span>vim /etc/init.d/webapp</span>写入以下脚本
#!/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的架构就配置成功了
说明:
语法为:
chkconfig --list [name] 用来列表服务
chkconfig --add name 用来添加服务
chkconfig --del name 用来删除服务
chkconfig [--level levels] name 改变启动信息以及检查特定服务的启动状态。
on 和 off 分别指服务在改变运行级时的启动和停止。reset 指初始化服务信息。
对于 on 和 off 开关,系统默认只对运行级 3,4, 5有效,但是 reset 可以对所有运行级有效。
以上就介绍了linux下 php+nginx+mysql安装配置,包括了mysql安装方面的内容,希望对PHP教程有兴趣的朋友有所帮助。