搜尋
首頁php教程php手册ubuntu下搭建nginx+php+mysql

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页面了。

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)