nginx+mysql+php

时间:2023-12-30 12:12:44

根据生产环境安装操作系统(centos 6.0 64位系统),安装完成后,使用Xshell通过ssh协议连接服务器。
ssh 用户名@IP+回车+输入密码后登录系统。
#mkdir -p /home/tools //创建软件存储目录
#cd /home/tools //进入软件存储目录
将软件包上传到/home/tools

安装nginx:
#/usr/sbin/groupadd www //创建php用户
#/usr/sbin/useradd -g www www //创建用户组,并且把php用户加入到用户组
#tar -zxvf pcre-8.10.tar.gz //解压pcre,安装pcre是为了让nginx支持HTTP Rewrite模块
#cd pcre-8.10 //进入pcre的解压目录
#./configure //编译PCRE
#make && make install //安装pcre
#cd .. //进入软件存储目录
#wget http://zlib.net/zlib-1.2.7.tar.gz //下载zlib
#yum -y install wget //安装wget
#wget http://zlib.net/zlib-1.2.7.tar.gz //下载zlib
#tar -zxvf zlib-1.2.7.tar.gz //解压zlib
#cd zlib-1.2.7 //进入zlib解压目录
#./configure //编译zlib
#make && make install //安装zlib
#yum -y install openssl //安装openssl,支持https
#rpm -ivh rpmforge-release-0.5.2-2.el5.rf.x86_64.rpm //安装第三方软件源
#cd .. //进入软件安装目录
#tar -zxvf nginx-1.2.6.tar.gz //解压nginx。
#cd nginx-1.2.6 //进入nginx的解压目录
#./configure --user=www --group=www --with-http_stub_status_module --with-http_gzip_static_module --prefix=/usr/local/nginx //编译nginx
#make && make install //安装nginx
#/usr/local/nginx/sbin/nginx //启动nginx
#ps -ef |grep nginx //查询nginx是否启动
有如下显示为nginx已经安装完成并成功启动。
root 8573 1 0 20:32 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 8574 8573 0 20:32 ? 00:00:00 nginx: worker process

创建Nginx日志目录

#mkdir -p /home/logs //创建Nginx日志目录
#chmod +w /home/logs //设置nginx日志目录可写
#chown -R www:www /home/logs //将php用户对nginx日志目录可写。

每创建一个域名,则在该目录下创建一个以域名命令的文件夹用于保存日志

修改Nginx配置文件
#vim /usr/local/nginx/conf/nginx.conf //编辑nginx的配置文件,在编辑前需要备份之前的配置文件
user nobody nobody; //指定nginx运行用户
worker_processes 8; //配置nginx的进程

error_log /home/logs/nginx-log/error.log crit;//nginx运行日志
pid /usr/local/nginx/logs/nginx.pid; //nginx进程存储目录

worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}

http {
include mime.types;
default_type application/octet-stream;//如果想在浏览器中访问json的内容,将此处修改为:test/html
charset utf-8;
#log_format access '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent $http_x_forwarded_for';
log_format access '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" host';

server_names_hash_bucket_size 128;
client_header_buffer_size 4k;
large_client_header_buffers 4 4k;

sendfile on;
tcp_nopush on;
tcp_nodelay on;

server_tokens off;
keepalive_timeout 60;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
#gzip_types text/plain application/x-javascript text/css application/xml;
gzip_types text/plain application/x-javascript text/css;
gzip_vary on;
server {
listen 80 default;
return 200;
}

server {
listen 80;
server_name www.zhezi.com;
root /home/app/www.zhezi.com/v1.6.2.2313/;
location / {
index index.php index.html index.htm;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

location /v1.6/ {
alias /home/app/www.zhezi.com/v1.6.2.2313/;
index index.php;
}
location ~ ^/v1.6/(.*\.php)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/app/www.zhezi.com/v1.6.2.2313/$1;
include fastcgi_params;
}

location /yd/ {
alias /home/app/www.zhezi.com/yd/;
#autoindex on;
}

access_log /home/logs/www.zhezi.com/access.log access;
error_log /home/logs/www.zhezi.com/error.log info;
}
}

编写每天定时切割Nginx日志的脚本
#cd /home
#mkdir crontab
#cd crontab
#touch cut_nginx_log.sh
#vim /home/crontab/cut_nginx_log.sh
#!/bin/bash
# This script run at 00:10

# The Nginx logs path
logs_path="/home/logs/www.zhezi.com/"

mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /home/nginx/logs/nginx.pid`
#chmod +x /home/crontab/cut_nginx_log.sh
#chown www:www /home/crontab/cut_nginx_log.sh

设置crontab,每天凌晨00:10切割nginx访问日志
#crontab -e
00 10 * * * /bin/bash /home/crontab/cut_nginx_log.sh
保存计划任务

配置开机自动启动Nginx + PHP
#vim /etc/rc.d/rc.local
ulimit -SHn 65535
/usr/local/php/sbin/php-fpm start
/usr/local/nginx/sbin/nginx

检查nginx配置文件是否正确
#/usr/local/nginx/sbin/nginx -t //检查nginx配置文件是否正确

#/usr/local/nginx/sbin/nginx -s reload //平滑重启

mysql 安装:
#/usr/sbin/groupadd mysql //创建mysql用户
#/usr/sbin/useradd -g mysql mysql //创建mysql用户组,并且把mysql用户加入到这个组中
#yum -y install mysql-devel //安装mysql客户端
#wget http://www.mysql.com/get/Downloads/MySQL-5.0/mysql-5.0.96.tar.gz/from/http://cdn.mysql.com/ //下载mysql
#yum -y install libtermcap-devel //安装libtermcap-devel
#tar -zxvf mysql-5.0.96.tar.gz //解压mysql
#cd mysql-5.0.96 //进入mysql的解压目录
#./configure --prefix=/usr/local/mysql '--with-extra-charsets=all' '--enable-assembler' --with-charset=utf8 //编译mysql

//210.14.154.7编译参数如下
./configure --prefix=/usr/local/mysql --with-mysqld-user=mysql --without-debug --with-big-tables --with-charset=utf8 --with-collation=utf8_general_ci --with-extra-charsets=all --enable-static --enable-thread-safe-client --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --enable-assembler --without-ndb-debug

//117.79.226.114编译参数
./configure --prefix=/usr/local/mysql --localstatedir=/usr/local/mysql/var --with-comment=Source --with-server-suffix=-hx10 --with-mysqld-user=mysql --without-debug --with-big-tables --with-charset=utf8 --with-collation=utf8_general_ci --with-extra-charsets=all --with-pthread --enable-static --enable-thread-safe-client --with-client-ldflags=-all-static --with-mysqld-ldflags=-all-static --enable-assembler --without-ndb-debug

#make && make install //安装mysql
#cd support-files/ //进入mysql可执行程序的目录
#ls //查看文件
#cp mysql.server /etc/init.d/ //复制mysql可执行程序到指定目录
#chmod +x /etc/init.d/mysql.server //设置mysql的程序可执行权限
#ls //查看文件
#cp my-huge.cnf /etc/my.cnf //复制mysql的配置文件到指定目录并且修改名字
#y //同意覆盖mysql的配置文件
#cd ../scripts/ //进入到生成数据库文件目录
#ls //查看目录文件
#./mysql_install_db //生成数据库文件
#/etc/init.d/mysql.server stop //停止mysql
#vim /etc/my.cnf
1、在[client]这个模块下增加:default-character-set=utf8 //设置mysql的编码
2、在[mysqld]这个模块下增加:
default-character-set=utf8 //设置mysql的编码
init_connect='SET NAMES utf8' //设置mysql的编码
#cd /usr/local/mysql //进入到mysql的安装目录
#cp -r var/ /home/var //将mysql的数据存储目录文件复制到磁盘空间较大的目录
#mv var/ var.bak //修改mysql的数据存储目录
#ln -s /home/var var //将mysql的数据存储目录软链接的到磁盘空间较大的目录
#chown -R mysql:mysql /home/var/ //设置mysql的数据存储目录的权限
#chown -R mysql:mysql /usr/local/mysql //设置mysql的权限
#/etc/init.d/mysql.server start //启动mysql
进入mysql,查看mysql编码:
#mysql -p
输入密码,显示如下内容即为正常。
mysql>show variables like 'character%'; //查看mysql的编码
+--------------------------+----------------------------------------+
| Variable_name | Value |
+--------------------------+----------------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql/share/mysql/charsets/ |
+--------------------------+----------------------------------------+
8 rows in set (0.00 sec)
mysql>

ps:如果未修改编码,在倒入数据之前(source)需要执行set names utf8;也可以解决mysql导入中文数据乱码问题。

php安装:
#yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel glibc glibc-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers //安装php说需要的库
#yum -y install libxml2-devel //安装libxml2-devel
#yum -y install libcurl-devel //安装libcurl-devel
#yum -y install libjpeg-devel //安装libjpeg-devel
#yum -y install libpng-devel //安装libpng-devel
#yum -y install freetype-devel //安装freetype-devel
#wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz //下载libmcrypt
#tar zxf libmcrypt-2.5.7.tar.gz //解压libmcrypt
#cd libmcrypt-2.5.7 //进入libmcrypt的解压目录
#ls //查看文件
#./configure //编译libmcrypt
#make && make install //安装libmcrypt
#cd /home/tools/ //进入软件存储目录
#tar zxvf php-5.2.14.tar.gz //解压php
#gzip -cd php-5.2.14-fpm-0.5.14.diff.gz | patch -d php-5.2.14 -p1 //让php支持php-fpm
#cd php-5.2.14 //进入php解压目录
#./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql/ --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-bcmath --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-iconv-dir --with-mhash --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --with-mcrypt --disable-rpath --enable-discard-path --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --with-gd --enable-gd-native-ttf --with-mhash --enable-pcntl --enable-sockets --enable-zip --enable-soap --disable-cli //编辑php
(编译的时候会报错:
1.如果提示:"configure: error: libjpeg.(a|so) not found"错误
#whereis libjpeg
#cp -frp /usr/lib64/libjpeg.* /usr/lib/
2.注意过程中提示:"Configure: error: libpng.(also) not found."错误,解决办法和上面的一样.
#whereis libjpeg
#cp -frp /usr/lib64/libpng* /usr/lib/
3、注意过程中提示:configure: error: Please reinstall libmhash - I cannot find mhash.h
#yum -y install mhash-devel //安装mhash-devel
#make && make install //安装php
#cp php.ini-dist /usr/local/php/etc/php.ini //复制php的配置文件到指定目录并且改名

安装memcache
#tar zxvf memcache-2.2.5.tgz //解压memcache
#cd memcache-2.2.5 //进入memcache解压目录
#/usr/local/php/bin/phpize //让php支持memcache
#./configure --with-php-config=/usr/local/php/bin/php-config //编译memcache
#make && make install //安装memcache

安装libevent
#yum -y install libevent libevent-devel //安装libevent和libevent-devel

安装memcached
#cd .. //进入软件下载目录
#tar zxvf memcached-1.4.4.tar.gz //解压memcached
#cd memcached-1.4.4 //进入memcached的解压目录
#./configure --prefix=/usr/local/memcached //编译memcached
#make && make install //安装memcached
#cd .. //进入软件下载目录

安装eaccelerator
#wget http://acelnmp.googlecode.com/files/eaccelerator-0.9.6.1.tar.bz2 //下载eaccelerator
#tar jxvf eaccelerator-0.9.6.1.tar.bz2 //解压eaccelerator
#cd eaccelerator-0.9.6.1/ //进入eaccelerator解压目录
#/usr/local/php/bin/phpize //让php支持eaccelerator
#./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config //编译eaccelerator
#make && make install //安装eaccelerator
#cd .. //进入软件下载目录

安装PDO_MYSQL
#tar zxvf PDO_MYSQL-1.0.2.tgz //解压PDO_MYSQL
#cd PDO_MYSQL-1.0.2 //进入PDO_MYSQL的解压目录
#/usr/local/php/bin/phpize //让php支持PDO_MYSQL
#./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql/ //编译PDO_MYSQL
#make && make install //安装PDO_MYSQL

修改php.ini文件
#vim /usr/local/php/etc/php.ini
手工修改:查找/usr/local/php/etc/php.ini中的extension_dir = "./"
修改为extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
并在此行后增加以下几行,然后保存:
extension = "memcache.so" //让php支持缓存
extension = "pdo_mysql.so" //让php可以连接mysql

配置eAccelerator加速PHP
#mkdir -p /home/logs/eaccelerator_cache
#vim /usr/local/php/etc/php.ini //增加php的加速模块
在文件最底部添加:
[eaccelerator]
extension="eaccelerator.so"
eaccelerator.shm_size="64"
eaccelerator.cache_dir="/home/logs/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"

#vim /usr/local/php/etc/php-fpm.conf //修改php-fpm配置文件
修改参数:
user:www
group:www
max_children:128
StartServers:20
MinSpareServers:5
MaxSpareServers:35
rlimit_files:65535
max_requests:1024

启动php-fpm
#/usr/local/php/sbin/php-fpm start
报错:
Starting php_fpm eAccelerator: Could not allocate 67108864 bytes, the maximum size the kernel allows is 33554432 bytes. Lower the amount of memory request or increase the limit in /proc/sys/kernel/shmmax.
PHP Warning: [eAccelerator] Can not create shared memory area in Unknown on line 0
PHP Fatal error: Unable to start eAccelerator module in Unknown on line 0
failed
#说明
php.ini中设置的eAccelerator,eaccelerator.shm_size=”64″,解释:eaccelerator可使用的共享内存大小(单位为MB),即64M
在Linux下,单个进程的最大内存使用量受/proc/sys/kernel/shmmax中设置的数字限制(单位为字节) /proc/sys/kernel/shmmax = 33554432 (32M)
临时更改该值:
echo 128000000 > /proc/sys/kernel/shmmax
或者
vi /etc/sysctl.conf
kernel.shmmax = 128000000

syscatl -p

报错:
Starting php_fpm PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/ eaccelerator.so' - /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/ eaccelerator.so: cannot open shared object file: No such file or directory in Unknown on line 0
是因为php.ini文件中
extension=" eaccelerator.so"
eaccelerator.cache_dir="/home/logs /eaccelerator_cache"
这两个地方有空格

Starting php_fpm /usr/local/php/bin/php-cgi: error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: No such file or directory
failed

#cd /usr/local/mysql/lib/
#cp libmysqlclient.so.18 /usr/local/lib