首页  >  文章  >  php教程  >  ubuntu下搭建nginx php mysql

ubuntu下搭建nginx php mysql

WBOY
WBOY原创
2016-06-06 19:42:202142浏览

nginx是一款轻量级的web服务器软件,它的优点是:免费,开源,高性能,而且稳定,功能强大,配置简单,资源消耗

    nginx是一款轻量级的web服务器软件,它的优点是:免费,开源,高性能,而且稳定,功能强大,配置简单,资源消耗小。

    我的操作系统是ubuntu12.04,32位。安装nginx+php+mysql前,请保证你的apache服务器关闭。

    IP地址假设为:127.0.0.1

1,首先安装mysql,在终端直接输入命令:sudo  apt-get  install  mysql-server  mysql-client ,安装过程中会提示输入两次密码。

2,安装nginx,在终端直接输入命令:sudo  apt-get  install  nginx  即可。

3,启动ngnix,在终端输入命令:sudo  /etc/init.d/nginx  start

       此时可以在浏览器中输入IP地址验证nginx是否正常工作,成功的话如下图:

ubuntu下搭建nginx php mysql

4,安装php5,输入命令:sudo  apt-get  install  php5-fpm ,php5-fpm是一个守护进程,运行FastCGI服务器上的端口9000.

5,配置nginx:

        sudo  vim  /etc/nginx/sites-available/default  ,将原来的内容对照下面的图片配置。

我的配置文件

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

10

# You may add here your

# server {

#   ...

# }

# statements for each of your virtual hosts to this file

 

##

# You should look at the following URL's in order to grasp a solid understanding

# of Nginx configuration files in order to fully unleash the power of Nginx.

# http://wiki.nginx.org/Pitfalls

# http://wiki.nginx.org/QuickStart

# http://wiki.nginx.org/Configuration

#

# Generally, you will want to move this file somewhere, and start with a clean

# file but keep this around for reference. Or just disable in sites-enabled.

#

# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.

##

 

server{

    listen  80;## listen for ipv4; this line is default and implied

    listen  [::]:80defaultipv6only=on;## listen for ipv6

 

    root/usr/共享/nginx/www;

    索引索引.html索引.htm索引.php;

 

    # 使网站可以从 http://localhost/

访问

    服务器名称本地主机;

 

    位置/{

        # 首先尝试将请求作为文件提供服务,然后

        # 作为目录,然后返回到index.html

        try_files$uri$uri//索引.html;

    }

 

    位置/文档{

        root/usr/共享;

        自动索引;

        允许127.0.0.1;

        否认全部;

    }

 

    位置/图像{

        root/usr/共享;

        自动索引关闭;

    }

 

    #error_page 404 /404.html;

 

    # 将服务器错误页面重定向到静态页面 /50x.html

    #

    #error_page 500 502 503 504 /50x.html;

    位置=/50x.html{

    root/usr/共享/nginx/www;

    }

 

    # 将 PHP 脚本代理到 Apache 监听 127.0.0.1:80

    #

    #位置 ~ .php$ {

    #   proxy_pass http://127.0.0.1;

    #}

 

    # 将 PHP 脚本传递给在 127.0.0.1:9000

上侦听的 FastCGI 服务器

    #

    位置~.php$ {

        fastcgi_pass127.0.0.1:9000;

        fastcgi_index索引.php;

        包括fastcgi_params;

    }

 

    # 如果 Apache 的文档根

,则拒绝访问 .htaccess 文件

    # 同意 nginx 的

    #

    位置~/ht {

        否认全部;

    }

}

 

# 另一个混合使用的虚拟主机 基于 IP、名称和端口的配置

#

#server {

#   听 8000;

#   听一些名字:8080;

#   server_name somename alias another.alias;

#   root html;

#   索引index.html index.htm;

#

#   位置 / {

#       try_files $uri $uri/ /index.html;

#   }

#}

 

# HTTPS 服务器

#

#server {

#   听 443;

#   server_name localhost;

#

#   root html;

#   索引index.html index.htm;

#

#   ssl 打开;

#   ssl_certificate cert.pem;

#   ssl_certificate_key cert.key;

#

#   ssl_session_timeout 5m;

#

#   ssl_protocols SSLv3 TLSv1;

#   ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv3:+EXP;

#   ssl_prefer_server_ciphers 开启;

#

#   位置 / {

#       try_files $uri $uri/ /index.html;

#}

现在保存文件并重新启动nginx。保存时可以用“:wq!”写入并强制保存启动。重启nginx的命令是:sudo  /etc/init.d/nginx  restart 。这之后可以测试一下php是否正常运行。具体办法是:1>,创建phpinfo,用到的命令是:sudo  vim  /usr/share/nginx/www/info.php  ,然后输入以下代码段即可:

ubuntu下搭建nginx php mysql

保存退出后,在浏览器输入http://127.0.0.1/info.php测试。

6,让php获得mysql支持,需要一个模块:

       安装php模块:sudo apt-cache  search php5 ,然后安装软件:sudo apt-get install php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-重新编码 php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl。

       现在重新启动php-fpm,利用命令:sudo /etc/init.d/php5-fpm restart .

       然后在浏览器刷新:http://127.0.0.1/info.php,看看是否已经支持安装的模块。效果应该如下:

ubuntu下搭建nginx php mysql

至此,一大堆环境就搭建好了。


这之后,针对具体项目,可以配置服务器根目录,如下:

查看sites-enabled目录下有一个默认文件,按照如下图所示修改:

ubuntu下搭建nginx php mysql

然后重启nginx服务器,打开localhost就可以看到自己项目的web页面了。

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn