nginx php, mysql ,memcached源码安装总结。

本文详细介绍了Nginx与PHP服务器的安装、配置与优化过程,包括安装依赖库、配置Nginx与PHP服务器、实现集群部署、配置MySQL数据库以及防火墙规则等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Nginx 部分:

nginx 的安装

nginx依赖以下一些软件库,在安装之前请确保安装了这些软件库,它们包括:gccopensslzlibpcre(可通过rpm -q命令查询是否已安装),其中前三个库可通过系统盘进行安装,

安装准备: nginx依赖于pcre,要先安装pcregzip module需要zlib库,需要安装zlib

tar -zxvf zlib-1.2.8.tar.gz

./configure 

make && make install

yum install pcre pcre-devel

wget http://nginx.org/download/nginx-1.4.2.tar.gz

tar zxvf nginx-1.4.2.tar.gz 

cd nginx-1.4.2

./configure --prefix=/usr/local/nginx

 --add-module=/usr/local/src/ngx_http_consistent_hash-master/

 

 --prefix=/usr/local/ngnix/ --add-module=/usr/local/src/ngx_http_consistent_hash-master/

 

make && make install



Nginx启动:

nginx -t -c /usr/nginx/conf/nginx.conf

或者

/usr/nginx/sbin/nginx -t

 

更改配置文件后启动

/usr/nginx/sbin/nginx -s reload


3.网页测试时由于有防火墙,所以要80端口通过防火墙

iptables -I INPUT -p tcp --dport 80 -j ACCEPT 

iptables -D INPUT -p tcp --dport 80 -j ACCEPT(

取消设置)

/etc/rc.d/init.d/iptables save

/etc/init.d/iptables restart 

cat /etc/sysconfig/iptables 可以查看 防火墙 iptables 配置文件内容

/etc/init.d/iptables status 

chkconfig –level 35 iptables off

cat /etc/sysconfig/iptables 可以查看 防火墙 iptables 配置文件内容

/etc/init.d/iptables status 

chkconfig –level 35 iptables off

4.nginx 配置:

第一份:


#user  nobody;

worker_processes  2;

 

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

 

#pid        logs/nginx.pid;

 

 

events {

    worker_connections  1024;

}

 

 

http {

    include       mime.types;

    default_type  application/octet-stream;

 

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';

 

    #access_log  logs/access.log  main;

 

    sendfile        on;

    #tcp_nopush     on;

 

    #keepalive_timeout  0;

    keepalive_timeout  65;

 

    #gzip  on;

   upstream mcserver

   {

 

     consistent_hash $request_uri;

     server 127.0.0.1:11211;

     server 127.0.0.1:11212;

     server 127.0.0.1:11213;

   }

   upstream  imageserver

   {

      server 10.18.93.120:81 weight=2;

      server 10.18.93.120:82;

   } 

 

   server{

   

   listen 84;

   root html;

   access_log logs/83.log main;

   }

   server{

   listen 81;

   server_name localhost;

   root html;

   access_log logs/81.log main;

 

   }

 

   server{

   listen 82;

   server_name localhost;

   root html;

   access_log logs/82.log main;

   

   }

 

   server{

   listen 2022;

   server_name z.com;

   location = /index.htm {

   root html;

   index index.htm;

   access_log   logs/zom.log  main;   

   }

}

   server {

        listen       80;

        server_name  localhost;

 

        gzip on;

        gzip_buffers 32 4k;

        gzip_comp_level 6;

        gzip_min_length 200;

        gzip_types text/css text/xml application/x-javascript;

 

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

       location /ecshop

       {

          root html;

          index index.html;

          rewrite "goods-(\d{0,7})\.html" ecshop/goods.php?id=$1;

       }

 

       location ~ \.(jpg|png|gif)$

       {

 

           proxy_set_header X-Forwarded-For $remote_addr;

           proxy_pass http://imageserver;

       

       }

       location ~ \.php$

      { 

      # proxy_pass http://10.18.93.120:8080;

 

      # proxy_set_header X-Forwarded-For $remote_addr;

        fastcgi_pass   127.0.0.1:9000;

        fastcgi_index  index.php;

        fastcgi_param  SCRIPT_FILENAME  /usr/local/ngnix/html/$fastcgi_script_name;

       # fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        include        fastcgi_params;

        }

       location / {

        

           set $memcached_key $request_uri;

           memcached_pass  mcserver;

           error_page 404 /callback.php;

           # root   html/;

           # index  index.php;

        }

 

        #error_page  404              /404.html;

 

        # redirect server error pages to the static page /50x.html

        #

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

 

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80

        #

        #location ~ \.php$ {

        #    proxy_pass   http://127.0.0.1;

        #}

 

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

        #

        #location ~ \.php$ {

        #    root           html;

        #    fastcgi_pass   127.0.0.1:9000;

        #    fastcgi_index  index.php;

        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

        #    include        fastcgi_params;

        #}

 

        # deny access to .htaccess files, if Apache's document root

        # concurs with nginx's one

        #

        #location ~ /\.ht {

        #    deny  all;

        #}

    }

 

 

    # another virtual host using mix of IP-, name-, and port-based configuration

    #

    #server {

    #    listen       8000;

    #    listen       somename:8080;

    #    server_name  somename  alias  another.alias;

 

    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }

    #}

 

 

    # HTTPS server

    #

    #server {

    #    listen       443;

    #    server_name  localhost;

 

    #    ssl                  on;

    #    ssl_certificate      cert.pem;

    #    ssl_certificate_key  cert.key;

 

    #    ssl_session_timeout  5m;

 

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;

    #    ssl_prefer_server_ciphers   on;

 

    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }

    #}

 

}

第二份(集群配置)


#user  nobody;

worker_processes  1;

worker_rlimit_nofile 102400;

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

 

#pid        logs/nginx.pid;

 

 

events {

    worker_connections  50000;

}

 

 

http {

    include       mime.types;

    default_type  application/octet-stream;

 

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

    #                  '$status $body_bytes_sent "$http_referer" '

    #                  '"$http_user_agent" "$http_x_forwarded_for"';

 

    #access_log  logs/access.log  main;

 

    sendfile        on;

    #tcp_nopush     on;

 

    #keepalive_timeout  0;

    keepalive_timeout  65;

 

    #gzip  on;

 

   upstream phpserver

   {

      server 10.18.93.124:9001;       

      server 10.18.93.124:9002;

      server 10.18.93.124:9003;

     server 10.18.93.124:9004;

   }

 

   upstream memcacheserver

  {

     consistent_hash $request_uri;

     server 10.18.93.123:11211;

     server 10.18.93.123:11212;

     server 10.18.93.123:11213;

     server 10.18.93.123:11214;

     server 10.18.93.123:11215;

     server 10.18.93.123:11216;

     server 10.18.93.123:11217;

     server 10.18.93.123:11218;

  }

    server {

        listen       80;

        server_name  localhost;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

 

        #error_page  404              /404.html;

 

        # redirect server error pages to the static page /50x.html

        #

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

 

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80

        #

        #location ~ \.php$ {

        #    proxy_pass   http://127.0.0.1;

        #}

 

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

        #

        location ~ \.php$ {

            root           /var/www;

             fastcgi_pass   phpserver;

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  /var/www/$fastcgi_script_name;

            include        fastcgi_params;

        }

        location /

        {

            root /var/www;

        

            set $memcached_key $request_uri;

            memcached_pass  memcacheserver;

            error_page 404  /info.php;

        }

        # deny access to .htaccess files, if Apache's document root

        # concurs with nginx's one

        #

        #location ~ /\.ht {

        #    deny  all;

        #}

    }

 

 

    # another virtual host using mix of IP-, name-, and port-based configuration

    #

    #server {

    #    listen       8000;

    #    listen       somename:8080;

    #    server_name  somename  alias  another.alias;

 

    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }

    #}

 

 

    # HTTPS server

    #

    #server {

    #    listen       443;

    #    server_name  localhost;

 

    #    ssl                  on;

    #    ssl_certificate      cert.pem;

    #    ssl_certificate_key  cert.key;

 

    #    ssl_session_timeout  5m;

 

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;

    #    ssl_prefer_server_ciphers   on;

 

    #    location / {

    #        root   html;

    #        index  index.html index.htm;

    #    }


注意:hash一致性时,需要安装

nginxhash模块

--add-module=/usr/local/src/ngx_http_consisitent_hash_master


php部分:

注意我们编译的PHP 要有如下功能:

连接mysql, gd, ttf, fpm(fascgi)方式运行


Yum -y install libpng libpng-devel

Yum -y install libxml2 libxml2-devel

Libxml2是个C语言的XML程式库,能简单方便的提供对XML文件的各种操作,并且支持XPATH查询,及部分的支持XSLT转换等功能


./configure --prefix=/usr/local/fastphp --with-config-file-path=/usr/local/fastphp/ --with-mysql=/usr/local/mysql --enable-fpm --with-gd --enable-gd-native-ttf --enable-gd-jis-conv


make && make install

安装完成后:

拷贝配置文件

 cp /usr/local/src/php-5.4.37/php.ini-development /usr/local/fastphp/lib/php.ini

php-fpm.conf中,一定要修改为

127.0.0.1:9000 修改为10.18.93.124:9000

否则会被拒绝。


做集群时:


php.ini中添加

Memcache.hash_strategy=consistent

php-fpm.conf

listen = 10.18.93.124:9001

pm = static

pm.max_children = 5


php运行:

/usr/local/php/sbin/php-fpm
/usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini -y /usr/local/php/etc/php-fpm.conf



Mysql部分:


需要安装gcc gcc-c++.

Yum -y install gcc gcc-c++

1.假设已经有mysql-5.5.10.tar.gz以及cmake-2.8.4.tar.gz两个源文件


(1)先安装cmakemysql5.5以后是通过cmake来编译的)

[root@ rhel5 local]#tar -zxv -f cmake-2.8.4.tar.gz
[root@ rhel5 local]#cd cmake-2.8.4
[root@ rhel5 cmake-2.8.4]#./configure
[root@ rhel5 cmake-2.8.4]#make
[root@ rhel5 cmake-2.8.4]#make install


(2)创建mysql的安装目录及数据库存放目录

[root@ rhel5~]#mkdir -p /usr/local/mysql                 //安装mysql 
[root@ rhel5~]#mkdir -p /usr/local/mysql/data            //存放数据库

(3)创建mysql用户及用户组

[root@ rhel5~]groupadd mysql
[root@ rhel5~]useradd -r -g mysql mysql

(4)安装mysql


yum install ncurses-devel

yum install bison

 cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all -DENABLED_LOCAL_INFILE=1

[root@ rhel5 mysql-5.5.10]#make
[root@ rhel5 mysql-5.5.10]#make install

参数说明:

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql        //安装目录

-DINSTALL_DATADIR=/usr/local/mysql/data         //数据库存放目录

-DDEFAULT_CHARSET=utf8                    //使用utf8字符

-DDEFAULT_COLLATION=utf8_general_ci            //校验字符

-DEXTRA_CHARSETS=all                        //安装所有扩展字符集

-DENABLED_LOCAL_INFILE=1                      //允许从本地导入数据



注意事项:

重新编译时,需要清除旧的对象文件和缓存信息。

# make clean

# rm -f CMakeCache.txt

# rm -rf /etc/my.cnf

2.配置

(1)设置目录权限

[root@ rhel5~]# cd /usr/local/mysql

[root@ rhel5 mysql]# chown -R root:mysql . //把当前目录中所有文件的所有者所有者设为root,所属组为mysql

[root@ rhel5 mysql]# chown -R mysql:mysql data

(2)

[root@ rhel5 mysql]# cp support-files/my-medium.cnf /etc/my.cnf //将mysql的启动服务添加到系统服务中



(3)创建系统数据库的表

[root@ rhel5 mysql]# cd /usr/local/mysql
[root@ rhel5 mysql]# scripts/mysql_install_db --user=mysql

(4)设置环境变量

[root@ rhel5~]# vi /root/.bash_profile

在PATH=$PATH:$HOME/bin添加参数为:

PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/usr/local/mysql/lib

[root@ rhel5~]#source /root/.bash_profile

chmod 1757 /tmp

(5)手动启动mysql

[root@ rhel5~]# cd /usr/local/mysql

[root@ rhel5 mysql]# ./bin/mysqld_safe --user=mysql   //启动MySQL,但不能停止

./mysqld_safe --user=mysql --skip-grant-tables --skip-networking



启动日志写在此文件下:/usr/local/mysql/data/localhost.err

关闭MySQL服务

[root@ rhel5 mysql]# mysqladmin -u root -p shutdown  //这里MySQL的root用户还没有配置密码,所以为空值。需要输入密码时,直接点回车键即可。

(6)另一种简单的启动mysql的方法(mysql已经被添加到系统服务中)

[root@ rhel5~]# service mysql.server start  
[root@ rhel5~]# service mysql.server stop
[root@ rhel5~]# service mysql.server restart

 

错误:

mysqld_safe mysqld from pid file /usr/local/mysql/data/localhost.localdomain.pid ended

解决:

没有安装库:

/usr/local/mysql/scripts/mysql_install_db --user=mysql

 

如果上述命令出现:mysql.server 未识别的服务

则可能mysql还没添加到系统服务中,下面用另一种方法添加:

[root@ rhel5 mysql]# cp support-files/mysql.server  /etc/init.d/mysql //将mysql的启动服务添加到系统服务中

注意:主要是将mysql.server拷贝到/etc/init.d中,命名为mysql。在有的系统中,mysql.server/usr /local/mysql/share/mysql/mysql.server中,而本系统中,mysql.server/usr/local /mysql/support-files/mysql.server中。

然后再用#service mysql start 来启动mysql即可。


(7)修改MySQLroot用户的密码以及打开远程连接

[root@ rhel5~]# mysql -u root mysql

mysql>use mysql;
mysql>desc user;
mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "root";  //为root添加远程连接的能力。
mysql>update user set Password = password('xxxxxx') where User='root';
mysql>select Host,User,Password  from user where User='root'; 
mysql>flush privileges;
mysql>exit

重新登录:mysql -u root -p

若还不能进行远程连接,则关闭防火墙
[root@ rhel5~]# /etc/rc.d/init.d/iptables stop

ERROR 1045 (28000): Access denied for user ’root’@’localhost’ (using password: NO)

 

mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '******' WITH GRANT OPTION;
# /etc/init.d/mysql stop 
# mysqld_safe --user=mysql --skip-grant-tables --skip-networking & 
# mysql -u root mysql 
mysql> UPDATE user SET Password=PASSWORD(’newpassword’) where USER=’root’; 
mysql> FLUSH PRIVILEGES; 
mysql> quit 
# /etc/init.d/mysql restart 
# mysql -uroot -p 
Enter password: <输入新设的密码newpassword> 
mysql> 

mysqldnd即mysql native driver简写,即是由PHP源码提供的mysql驱动连接代码.它的目的是代替旧的libmysql驱动.

因为mysqlnd内置于PHP源代码,故你在编译安装php时就不需要预先安装mysql server也可以提供mysql client API (mysql_connect, pdo , mysqli), 这将减化一些工作量.

--with-mysql=/usr/local/mysql 这实际上就是使用了mysql官方自带的libmysql驱动, 这是比较老的驱动, PHP 5.3开始已经不建议使用它了, 而建议使用mysqlnd.

/var/lib/mysql/mysql.sock  是mysqlnd安装时默认的mysql.sock

/tmp/mysql.sock   是源码安装时生成的mysql.sock

Access denied for user 'root'@'10.18.93.124' (using password: NO)

Mysql服务器需要添加一个10.18.93.124的账号

 

Connect not 10.18.93.122 

没有关闭防火墙:service iptables stop

 Access denied for user 'root'@'10.18.93.124' (using password: NO)

 

not rute host;

一定要关闭防火墙

Service iptables stop;


Memcahched部分

一定先安装libevent

./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent

 

./configure --prefix=/usr/local/memched

Make && make install

./bin/memcached -m 64 -p 11211 -vv -u nobody (-d 后台运行)

./bin/memcached -m 64 -p 11212 -vv -u nobody (-d 后台运行)

 

注意;需要安装在php服务器中

如果需要要用php访问memcached,需要编辑memecachedphp扩展

1,进入扩展文件夹:memecahe-2.2.7:

2/usr/local/fashphp/bin/phpize

./configure --with-php-config=/usr/local/fastphp/bin/php-config

在/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525会生成memecach.so

php.ini中加入

extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so

Apache httpd部分:

安装:

首先下载aprapr-util,pcre

./configure --prefix=/usr/local/httpd --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-pcre=/usr/local/pcre  --enable-so --enable-rewrite

 

如果缺少libxm2

Yum install libxml2//libxml2核心功能

Yum install libxml2-devel //libxml的头文件

http://downloads.php.net/pierre/

http://httpd.apache.org

http://php.net/downloads.php

http://pecl.php.net/

 

Httpd.conf设置:

Listen: 80 //监听的端口

ServerName localhost:80 //虚拟的web站点

DocumentRoot "/usr/local/httpd/htdocs"  //资源文件的根目录

ErrorLog "logs/error_log"  //错误log

 

 <IfModule dir_module>

  DirectoryIndex index.html  //默认访问的文件

  </IfModule>

 

Include conf/extra/httpd-mpm.conf //开启mpm机制

 

 

<IfModule mpm_prefork_module>

    StartServers          5

    MinSpareServers       5

    MaxSpareServers      10

    ServerLimit    8000

    MaxClients         8000

    MaxRequestsPerChild   0

</IfModule>

 

使用directory段设置根目录

<directory />

</directory>

Deny:设定禁止访问apache服务器主机

Allow from all

Allow from 127.0.0.1

Allow override 

1.none:不读取.htaccess文件

2.all:读取.htaccess文件,修改原来的访问权限

设置资源文件的权限

<directory /usr/local/httpd/htmldos>

</directory>

 

设置默认首页依次为index.html index.php index.thm

<ifmodulle dir_module>

  DirectoryIndex index.html index.php index.htm

</ifmodule>

 

 

2.

#AddHandler application/x-httpd-php .php //遇到php文件需要httpdphp模块解析

 AddType application/x-httpd-php .php  遇到php文件需要httpdphp模块解析

 

3. Listen 8080

 DocumentRoot "/usr/local/httpd/htdocs"

  <Directory "/usr/local">

Options Indexes FollowSymLinks

 AllowOverride None

     Require all granted

     #Require all denied

 </Directory>

 Include conf/extra/httpd-vhosts.conf //打开这个

/usr/local/httpd/conf/extra/httpd-vhosts.conf

 <VirtualHost *:8080>

         ServerAdmin webmaster@dummy-host.example.com

         DocumentRoot "/usr/local/ngnix/html/"

          ServerName 10.18.93.120

          ServerAlias www.dummy-host.example.com

          ErrorLog "logs/dummy-host.example.com-error_log"

          CustomLog "logs/dummy-host.example.com-access_log" common

      </VirtualHost>

注意:httpd.conf中的<Directory "/usr/local">一定要比httpd-vhosts.conf中的 DocumentRoot "/usr/local/ngnix/html/" 中目录大。否则可能会forbidn,拒绝访问。

 

 

 

 

 

 

相应的php的安装需要这样

./configure  --prefix=/usr/local/php\

--with-mysql=mysqlnd \

--enable-mysqlnd \

--with-gd \

--enable-gd-native-ttf \

--enable-gd-jis-conv

--with-apxs2=/usr/local/httpd/bin/apxs

 

 

之后再php的httpd.conf文件中会出现

 LoadModule php5_module        modules/libphp5.so

在/usr/local/httpd/modules/libphp5.so

将php作为httpd的一个模块。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值