nginx,mysql,php环境搭建
1、安装Linux Nginx
# tar -zxvf nginx-0.8.5.tar.gz
# cd nginx-0.8.5
# ./configure --prefix=/usr/local/nginx –with-http_stub_status_module –with-http_ssl_module –with-http_gzip_static_module –with-http_ssl_module(安装路径及
所需依赖包)--prefix(安装路径)
# make && make install
启动nginx# /usr/local/nginx/sbin/nginx
停止nginx# kill -QUIT `cat /usr/local/nginx/logs/nginx.pid`
重启nginxkill -HUP `cat /usr/local/nginx/logs/nginx.pid`
添加到自启动# echo "/usr/local/nginx/sbin/nginx">>/etc/rc.local
或者用 /usr/local/nginx/sbin/nginx 启动 nginx
/usr/local/nginx/sbin/nginx -s quit 退出nginx
/usr/local/nginx/sbin/nginx -s reload 重新载入
还需要安装 apt-get install spawn-fcgi
每次运行的时候执行:告诉php需要相应的端口号 跟之前做的java一样
spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f
/usr/bin/php-cgi(必须滴)
2、spawn-fcgi,用来启动PHP的FastCGI模式
(linux下载包用 wget方式 或者可以直接安装用apt-get install spawn-fcgi方式)
wget http://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.gz
tar -zxf spawn-fcgi-1.6.3.tar.gz
cd spawn-fcgi-1.6.3
./configure –bindir=/usr/bin –libdir=/usr/lib –prefix=/etc
make&&make install
3、安装mysql
# tar -zxvf mysql-5.0.67.tar.gz
# cd mysql-5.0.67
# groupadd mysql
# useradd -g mysql -s /sbin/nologin -M mysql
# ./configure --prefix=/usr/local/mysql --with-charset=gbk --with-extra-charset=all --enable-hread-safe-client
--enable-local-infile --with-low-memory
# make && make install
# cp support-files/my-medium.cnf /etc/my.cnf
# chown -R mysql.mysql /usr/local/mysql/
# /usr/local/mysql/bin/mysql_install_db --user=mysql
# chown -R root.root /usr/local/mysql/
# chown -R mysql.mysql /usr/local/mysql/var/
启动数据库服务,并添加到自启动
# /usr/local/mysql/bin/mysqld_safe --user=mysql &
#cp support-files/mysql.server /etc/rc.d/init.d/mysqld
#chmod 755 /etc/rc.d/init.d/mysqld
加入自动启动服务队列:
#chkconfig --add mysqld
#chkconfig --level 345 mysqld on添加root密码
# /usr/local/mysql/bin/mysqladmin -u root password "123456"
测试一下:# /usr/local/mysql/bin/mysql -u root -p输入密码:123456,看能不能进入到数据库
配置库文件搜索路径
# echo "/usr/local/mysql/lib/mysql">>/etc/ld.so.conf
# ldconfig
# ldconfig -v
添加/usr/local/mysql/bin到环境变量PATH中
#echo "export PATH=$PATH:/usr/local/mysql/bin">>/etc/profile
#source /etc/profile
4、安装PHP
这里产生的是可执行文件,和apache的不一样,和apache结合的时候产生的是动态库
# tar -jxvf php-5.2.6.tar.bz2
# gzip -cd php-5.2.6-fpm-0.5.9.diff.gz |patch -d php-5.2.6 -p1
# cd php-5.2.6
# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --enable-fastcgi --enable-fpm
--with-config-file-path=/usr/local/php/etc --enable-force-cgi-redirect
# make && make install
# cp php.ini-recommended /usr/local/php/etc/php.ini
# vi /usr/local/php/php-fpm.conf
(1)<value name="listen_address">127.0.0.1:9000</value>修改为<value name="listen_address">IP:9000</value>
//本机就用默认的127.0.0.1
(2)下面这两行去掉注释并修改
<value name="sendmail_path">/usr/sbin/sendmail -t -i</value>
<value name="display_errors">1</value>
(3)<value name="user">nobody</value> //去注释
(4)<value name="group">nobody</value> //去注释
(5)<value name="allowed_clients">127.0.0.1</value> //允许连接的PC,本机就用127.0.0.1
启动php-fpm# /usr/local/php/sbin/php-fpm start添加到自启动# echo "/usr/local/php/sbin/php-fpm start">>/etc/rc.local
5、修改Linux Nginx的配置文件,支持PHP
# vim /usr/local/nginx/conf/nginx.conf
user nobody;
worker_processes 8;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 1024;
events
{use epoll;
worker_connections 1024;}
http{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
server {
listen 80;
server_name www.bbb.com;
root /www/uuweb;
index index.html index.htm index.php;
location ~ .*\.(php|php5)?$ { 或者 location / {
不知道怎么回事我用前一个css不起作用,只能用后面一个。
root /www/uuweb;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/uuweb$fastcgi_script_name;
include fastcgi_params;}
#location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
#{expires 30d;}
#location ~ .*\.(js|css)?$
#{expires 1h;}
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /var/logs/access.log access;}}
注:server部分为PHP虚拟主机127.0.0.1:9000为fastcgi的PC,我这里用的本机/www/uuweb$fastcgi_script_name; 为PHP网页保存的目录测试配置文件:
# /usr/local/nginx/sbin/nginx -t
----------------------------
编译pcre库以及nginx
cd /data/download
tar zxvf pcre-8.00.tar.gz -C /usr/local/src/
cd /usr/local/src/pcre-8.00/
./configure
make && make install
cd /data/download
tar zxvf nginx-0.7.64.tar.gz -C /usr/local/src/
cd /usr/local/src/nginx-0.7.64/
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
建立nginx的log文件夹
mkdir -p /data/logs
chmod +w /data/logs
chown -R nginx:nginx /data/logs
创建nginx的配置文件
rm -f /usr/local/nginx/conf/nginx.conf
vi /usr/local/nginx/conf/nginx.conf
配置文件内容:
user nginx nginx;
worker_processes 1;
error_log /data/logs/nginx_error.log crit;
pid /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
http
{
include mime.types;
default_type application/octet-stream;
#charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
server
{
listen 80;
server_name www.web.cn *.web.cn;
index index.php;
root /data/htdocs/webroot;
autoindex on;
if (!-f $request_filename){
rewrite ^/(.+)$ /index.php?$1& last;
}
location ~* ^.+\.(jpg|jpeg|gif|ico)$ {
access_log off;
gzip off;
expires 30d;
}
location ~* ^.+\.(css|js)$ {
access_log off;
expires 1d;
}
location ~* ^.+\.(pdf|gz|bz2|exe|rar|zip|7z)$ {
gzip off;
}
location ~ .*\.(php)?$
{
fastcgi_pass unix:/tmp/php-cgi.sock;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fcgi.conf;
}
log_format minhlog '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log /data/logs/webroot_access.log;
}
}
上面是假设域名为web.cn,目录就是前面安装php时建立的目录。在能看懂配置内容的时候可以自己修改里面具体的写法,并不复杂的,只有几句而已。
配置fcgi.conf文件:
vi /usr/local/nginx/conf/fcgi.conf
文件内容:
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param REDIRECT_STATUS 200;
配置完成,启动服务:
ulimit -SHn 65535
/usr/local/nginx/sbin/nginx
添加nginx,mysql,php的启动项到启动文件:
vi /etc/rc.local
/data/mysql/3306/mysql start
/usr/local/php/sbin/php-fpm start
/usr/local/nginx/sbin/nginx
这样就能开机自动启动各个服务了。
平滑重启nginx的命令是:
kill -HUP `cat /usr/local/nginx/nginx.pid`
至此web服务已经可以正常运行。对于日志切割的部分,有需要的可以自己参考张宴大牛的博客,一般小站日志也没什么好看的,我自己是直接关闭了的, 当然如果被攻击的话可以再临时打开,具体自己掌握了。也可以自己找一些nginx配置和优化的资料来看,相对来说一点也不复杂,只是要花些时间。
五、后续工作
后续的修改和设置需要看自己的个性化需要,不能一概而论了,这里只给出一点思路。大体分为两块,一是系统的安全与优化,二是网站功能的扩展。第一方 面,包括修改root密码或登录方式、更改ssh端口、配置防ping,清理系统不需要的服务、用户组、用户、精确限制用户的权限等。以及对nginx、 mysql等的安全设置。第二方面,如果需要安装ftp,可以考虑使用centos自带的vsftpd,安装其他扩展时尽量自己编译为好。
使用中注意整理常用的参数,记好各个程序的安装路径、配置文件路径、各类log文件的路径等,常用的系统命令不用说自然要熟记了。尤其强调一定要注意备份,不仅包括网站数据,也包括各类配置文件。
http://guitarsdw2.blog.163.com/blog/static/101437600201021345136141/
===========================================
在配置nginx提示如下错误时:
[emerg]: getpwnam(“www”) failed
解决方案:
在nginx.conf中 把user www www改为user nobody nobody既可
这里的www是 nginx.conf 中配置的.
user www; (原来)
user nobody nobody;
======================================
ginx启动报错,信息如下:
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
.....
使用ps ef|grep nginx,并未发现有nginx进程,有可能被其他进程占用,这时可以采用如下方式处理:
1. 查看80端口占用
netstat -ntpl
2. 杀掉占用80端口的进程
kill -9 $pid
发现httpd 仍然占用
ps -aux | grep httpd
再把找到的请求kill掉.