如何在linux下搭建nginx服务器

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在linux下搭建nginx服务器相关的知识,希望对你有一定的参考价值。

1.先从nginx官网下载最新的版本 http://nginx.org/download/nginx-1.7.8.tar.gz 2.解压nginx-1.7.8.tar.gz,然后执行下面操作即可 ./configure --prefix=/usr/local/nginxmakemake install 参考技术A 这个问题不是一两句话说的清楚的,建议去Linux公社,51CTO.com ,这是Linux爱好者经常去的网站,你去一搜就会有很多的教程的,跟着操作试试

linux下nginx+php+mysql环境搭建

++++++++++++++++++++++++++++++++++++++++++++++
linux下nginx+php+mysql环境搭建
++++++++++++++++++++++++++++++++++++++++++++++
操作系统    : [CentOS6.0]
服务器     : [nginx-1.1.8]
PHP      : [php-5.2.6]
数据库     : [mysql-5.1.59]

PS                   : 经过两天的奋斗终于配置上了这套系统
++++++++++++++++++++++++++++++++++++++++++++++


wget \'http://nginx.org/download/nginx-1.1.8.tar.gz\'
wget \'http://museum.php.net/php5/php-5.2.6.tar.gz\'
wget \'http://php-fpm.org/downloads/php-5.2.6-fpm-0.5.8.diff.gz\'
wget \'http://downloads.mysql.com/archives/mysql-5.1/mysql-5.1.59.tar.gz\'
wget \'http://download.chinaunix.net/down.php?id=28491&ResourceID=6095&site=1\'
wget \'http://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz/download\'
wget \'http://sourceforge.net/projects/libpng/files/zlib/1.2.3/zlib-1.2.3.tar.gz/download\'
wget \'http://files.directadmin.com/services/custombuild/libpng-1.2.31.tar.gz\'
wget \'http://download.chinaunix.net/down.php?id=10021&ResourceID=5095&site=1\'
wget \'http://download.savannah.gnu.org/releases/freetype/freetype-2.4.0.tar.gz\'
wget \'http://ftp.gnu.org/gnu/autoconf/autoconf-2.61.tar.gz\'
wget \'http://google-desktop-for-linux-mirror.googlecode.com/files/gd-2.0.35.tar.gz\'
wget \'http://ftp.gnu.org/gnu/ncurses/ncurses-5.6.tar.gz\'
wget \'http://sourceforge.net/projects/pcre/files/pcre/8.10/pcre-8.10.tar.gz/download\'
wget \'http://www.openssl.org/source/openssl-1.0.0e.tar.gz\'

1>【安装gcc, gcc-c++】

yum install gcc  
yum install gcc-c++  

2>【开放80、3306、22端口】

#关闭防火墙  
service iptables stop  
vi /etc/sysconfig/iptables  

#添加  
# Firewall configuration written by system-config-securitylevel
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT - [0:0]
-A INPUT -j RH-Firewall-1-INPUT
-A FORWARD -j RH-Firewall-1-INPUT
-A RH-Firewall-1-INPUT -i lo -j ACCEPT
-A RH-Firewall-1-INPUT -p icmp --icmp-type any -j ACCEPT
-A RH-Firewall-1-INPUT -p 50 -j ACCEPT
-A RH-Firewall-1-INPUT -p 51 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp --dport 5353 -d 224.0.0.251 -j ACCEPT
-A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 3690 -j ACCEPT
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
COMMIT

#退出后重启防火墙  
service iptables restart  

3>【建立home/soft目录, 将源码包上传】
4>【安装libxml2】

tar -zxvf libxml2-2.6.30.tar.gz  
cd libxml2-2.6.30  
./configure --prefix=/usr/local/libxml2/  
make   
make install 

5>【安装libmcrypt】

tar -zxvf libmcrypt-2.5.8.tar.gz  
cd libmcrypt-2.5.8  
./configure --prefix=/usr/local/libmcrypt/ --enable-ltdl-install  
make  
make install  

6>【安装libxml2-devel】

yum install libxml2-devel  

7>【安装zlib】

tar -zxvf zlib-1.2.3.tar.gz  
cd zlib-1.2.3  
./configure  
make  
make install  

8>【安装libpng】

tar -zxvf libpng-1.2.31.tar.gz  
cd libpng-1.2.31  
./configure --prefix=/usr/local/libpng/  
make  
make install  

9>【安装jpegsrc.v6b】

mkdir /usr/local/jpeg6  
mkdir /usr/local/jpeg6/bin  
mkdir /usr/local/jpeg6/lib  
mkdir /usr/local/jpeg6/include  
mkdir -p /usr/local/jpeg6/man/man1  

tar -zxvf jpegsrc.v6b.tar.gz  
cd jpeg-6b  
./configure --prefix=/usr/local/jpeg6/ --enable-shared --enable-static  
make  
make install  

9.1>【安装jpegsrc报错 machine `x86_64-redhat\' not recognized】

yum install libtool-ltdl
cp /usr/share/libtool/config.sub .
cp /usr/share/libtool/config.guess .
ln -s /usr/local/bin/libtool ./libtool
mkdir -p /usr/local/man/man1

10>【安装freetype】

tar -zxvf freetype-2.4.0.tar.gz  
cd freetype-2.4.0  
./configure --prefix=/usr/local/freetype/  
make  
make install  

11>【安装autoconf】

tar -zxvf autoconf-2.61.tar.gz  
cd autoconf-2.61  
./configure  
make  
make install  

12>【安装gd】

tar -zxvf gd-2.0.35.tar.gz  
cd gd/2.0.35/
./configure --prefix=/usr/local/gd2/ --with-jpeg=/usr/local/jpeg6/ --with-freetype=/usr/local/freetype/  
make  
make install  

13>【pcre-8.10】

tar -zxvf pcre-8.10.tar.gz  
cd pcre-8.10  
./configure  
make  
make install  

14>【安装openssl】

tar -zxvf openssl-1.0.0e.tar.gz  
cd openssl-1.0.0e  
./config --prefix=/usr/local/openssl  
make  
make install  

15>【安装nginx】

groupadd www  
useradd -g www www  
tar -zxvf nginx-1.1.8.tar.gz  
cd nginx-1.1.8  
./configure --user=www --group=www --prefix=/usr/local/nginx --with-openssl=/lamp/src/openssl-1.0.0e --with-http_stub_status_module --with-http_ssl_module   
make  
make install  

16>【安装ncurses】

tar -zxvf ncurses-5.6.tar.gz  
cd ncurses-5.6  
./configure --with-shared --without-debug --without-ada --enable-overwrite  
make  
make install  

17>【安装mysql】

groupadd mysql  
useradd -g mysql mysql  
tar -zxvf mysql-5.1.59.tar.gz  
cd mysql-5.1.59  
./configure --prefix=/usr/local/mysql/ --with-extra-charsets=all  
make  
make install  
  
cp support-files/my-medium.cnf /etc/my.cnf  
/usr/local/mysql/bin/mysql_install_db --user=mysql  
chown -R root /usr/local/mysql  
chown -R mysql /usr/local/mysql/var  
chgrp -R mysql /usr/local/mysql  
  
/usr/local/mysql/bin/mysqld_safe  --user=mysql &  
cp /lamp/src/mysql-5.1.59/support-files/mysql.server /etc/rc.d/init.d/mysqld  
chown root.root /etc/rc.d/init.d/mysqld  
chmod 755 /etc/rc.d/init.d/mysqld  
chkconfig --add mysqld  
chkconfig --list mysqld  
chkconfig --levels 245 mysqld off  
  
#配置mysql  
cd /usr/local/mysql  
#简单的测试  
bin/mysqladmin version  
#查看所有mysql参数  
bin/mysqladmin Variables  
#没有密码可以直接登录本机服务器  
bin/mysql -uroot  
DELETE FROM mysql.user WHERE Host=\'localhost\' AND User=\'\';  
FLUSH PRIVILEGES;  
#设置root密码为123456  
SET PASSWORD FOR \'root\'@\'localhost\' = PASSWORD(\'123456\');  
#配置可远程连接mysql  
use mysql  
SELECT user,password,host FROM user;  
DELETE FROM user WHERE host=\'localhsot.localdomain\'  
DELETE FROM user WHERE host=\'127.0.0.1\';  
UPDATE user SET host=\'%\' WHERE user=\'root\';  
#重启mysql  
service mysqld restart  

18>【安装php】

插入 yum install libpng-devel,解决“error: png.h not found”

tar -zxvf php-5.2.6.tar.gz gzip -cd php-5.2.6-fpm-0.5.8.diff.gz | patch -d php-5.2.6 -p1 cd php-5.2.6
./buildconf --force
./configure --prefix=/usr/local/php/ --with-config-file-path=/usr/local/php/etc/ --with-mysql=/usr/local/mysql/ --with-libxml-dir=/usr/local/libxml2/ --with-jpeg-dir=/usr/local/jpeg6/ --with-freetype-dir=/usr/local/freetype/ --with-gd=/usr/local/gd2/ --with-mcrypt=/usr/local/libmcrypt/ --with-mysqli=/usr/local/mysql/bin/mysql_config   --with-libxml-dir=/usr/local/libxml2  --with-gettext=/usr/local/gettext  --with-jpeg-dir=/usr/local/jpeg  --with-png-dir=/usr/local  --with-zlib-dir=/usr/local/zlib --with-curl=/usr/local/curl  --enable-soap --with-xsl=/usr/local/libxslt --enable-fastcgi  --with-mcrypt=/usr/local/lib --with-mhash=/usr/local/lib --enable-mbstring=all --enable-sockets --enable-fastcgi --enable-fpm make make install cp php.ini-dist /usr/local/php/etc/php.ini #修改php-fpm.conf vi /usr/local/php/etc/php-fpm.conf #查找63,66行将注释去掉 <value name="user">nobody</value> <value name="group">nobody</value> #启动php /usr/local/php/sbin/php-fpm start
安装php是遇到 possibly undefined macro: AM_ICONV 错误,是因为没有安装 gettext。

#yum install gettext

安装时候遇见 [sapi/cgi/php-cgi] Error 1

#yum install libtool-ltdl.x86_64 libtool-ltdl-devel.x86_64

19>【nginx配置】

vi /usr/local/nginx/conf/nginx.conf  
#在server下找到location / 修改解析PHP文件存放的路径, 修改为:  
location / {  
        root   /var/www/html;  
        index  index.html index.htm index.php;  
}  
#root表示虚拟目录设置为/var/www/html,增加默认解析index.php  
#在server下找到location ~ /.php$ 修改php解释器FastCGI配置,修改为:  
location ~ \\.php$ {  
            root           /var/www/html;  
            fastcgi_pass   127.0.0.1:9000;  
            fastcgi_index  index.php;  
            fastcgi_param  SCRIPT_FILENAME  /var/www/html$fastcgi_script_name;  
            include        fastcgi_params;  
}  
#将nginx添加到自启动中  
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.d/rc.local  
#将php添加到自启动中  
echo "/usr/local/php/sbin/php-fpm start" >> /etc/rc.d/rc.local  

20>【nginx虚拟主机配置】

vi /usr/local/nginx/conf/nginx.conf
#在http最后一行加入
include    /usr/local/nginx/conf/vhost/dev_localhost.config;
#不带url重写

server {    
    listen 80 default;    
    server_name dev.localhost;    
    access_log logs/dev.access.log;    
  
  
    root /var/www/html/dev;    
  
  
    server_name_in_redirect off;    
  
  
    location / {    
        index index.html index.php;  
    }    
  
  
    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;  
    }  
}    

#带url重写(zend framework)

server {  
    listen 80;  
    server_name dev.localhost;  
    access_log logs/dev.access.log;    
          
    root   /var/www/html/dev/code/html;  
    location / {  
        index index.html index.php;  
  
  
        if (-e $request_filename ) {  
            break;  
        }  
              
        if ( $request_filename ~* \\.(js|ico|gif|jpg|jpeg|xml|swf|txt|png|css|html|htm)$ ) {  
            return 404;  
        }  
              
        rewrite .* index.php;  
    }  
    location ~ .*\\.php$ {  
        include fastcgi_params;  
        fastcgi_param  SCRIPT_FILENAME    $document_root/index.php;  
        #fastcgi_param REQUEST_URI $document_uri?$query_string;  
        fastcgi_read_timeout 120;  
        fastcgi_pass  127.0.0.1:9000;  
        fastcgi_index index.php;  
    }  
}  
#nginx  
/usr/local/nginx/sbin/nginx  -c /usr/local/nginx/conf/nginx.conf
/usr/local/nginx/sbin/nginx -s reload #重启php /usr/local/php/sbin/php-fpm restart

以上是关于如何在linux下搭建nginx服务器的主要内容,如果未能解决你的问题,请参考以下文章

怎么在linux下搭建一个nginx服务器

nginx配置sso登录

windows下搭建nginx-rtmp服务器

Nginx 在 Linux 下安装与搭建集群

LNMP环境搭建(linux+Nginx + Mysql + PHP)

1在linux服务器centos虚拟机搭建nginx网站