Heim  >  Artikel  >  php教程  >  ubuntu下搭建nginx+php+mysql

ubuntu下搭建nginx+php+mysql

WBOY
WBOYOriginal
2016-06-06 19:42:202170Durchsuche

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/share/nginx/www;

    indexindex.htmlindex.htmindex.php;

 

    # Make site accessible from http://localhost/

    server_namelocalhost;

 

    location/{

        # First attempt to serve request as file, then

        # as directory, then fall back to index.html

        try_files$uri$uri//index.html;

    }

 

    location/doc{

        root/usr/share;

        autoindexon;

        allow127.0.0.1;

        denyall;

    }

 

    location/images{

        root/usr/share;

        autoindexoff;

    }

 

    #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/usr/share/nginx/www;

    }

 

    # 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${

        fastcgi_pass127.0.0.1:9000;

        fastcgi_indexindex.php;

        includefastcgi_params;

    }

 

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

    # concurs with nginx's one

    #

    location~/\.ht{

        denyall;

    }

}

 

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

#

#server {

#   listen 8000;

#   listen somename:8080;

#   server_name somename alias another.alias;

#   root html;

#   index index.html index.htm;

#

#   location / {

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

#   }

#}

 

# HTTPS server

#

#server {

#   listen 443;

#   server_name localhost;

#

#   root html;

#   index index.html index.htm;

#

#   ssl on;

#   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 on;

#

#   location / {

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

#}

现在保存文件并重新启动nginx。保存时可以用“ :wq! ” 写入并强制保存推出。重启nginx的命令是:sudo  /etc/init.d/nginx  restart 。这之后可以test一下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-recode 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

至此,一个lump的环境就搭建好了。


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

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

ubuntu下搭建nginx+php+mysql

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn