0

0

ubuntu下搭建nginx+php+mysql

php中文网

php中文网

发布时间:2016-06-06 19:42:20

|

2322人浏览过

|

来源于php中文网

原创

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  即可。

立即学习PHP免费学习笔记(深入)”;

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

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

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

相关专题

更多
Golang gRPC 服务开发与Protobuf实战
Golang gRPC 服务开发与Protobuf实战

本专题系统讲解 Golang 在 gRPC 服务开发中的完整实践,涵盖 Protobuf 定义与代码生成、gRPC 服务端与客户端实现、流式 RPC(Unary/Server/Client/Bidirectional)、错误处理、拦截器、中间件以及与 HTTP/REST 的对接方案。通过实际案例,帮助学习者掌握 使用 Go 构建高性能、强类型、可扩展的 RPC 服务体系,适用于微服务与内部系统通信场景。

8

2026.01.15

公务员递补名单公布时间 公务员递补要求
公务员递补名单公布时间 公务员递补要求

公务员递补名单公布时间不固定,通常在面试前,由招录单位(如国家知识产权局、海关等)发布,依据是原入围考生放弃资格,会按笔试成绩从高到低递补,递补考生需按公告要求限时确认并提交材料,及时参加面试/体检等后续环节。要求核心是按招录单位公告及时响应、提交材料(确认书、资格复审材料)并准时参加面试。

44

2026.01.15

公务员调剂条件 2026调剂公告时间
公务员调剂条件 2026调剂公告时间

(一)符合拟调剂职位所要求的资格条件。 (二)公共科目笔试成绩同时达到拟调剂职位和原报考职位的合格分数线,且考试类别相同。 拟调剂职位设置了专业科目笔试条件的,专业科目笔试成绩还须同时达到合格分数线,且考试类别相同。 (三)未进入原报考职位面试人员名单。

58

2026.01.15

国考成绩查询入口 国考分数公布时间2026
国考成绩查询入口 国考分数公布时间2026

笔试成绩查询入口已开通,考生可登录国家公务员局中央机关及其直属机构2026年度考试录用公务员专题网站http://bm.scs.gov.cn/pp/gkweb/core/web/ui/business/examResult/written_result.html,查询笔试成绩和合格分数线,点击“笔试成绩查询”按钮,凭借身份证及准考证进行查询。

11

2026.01.15

Java 桌面应用开发(JavaFX 实战)
Java 桌面应用开发(JavaFX 实战)

本专题系统讲解 Java 在桌面应用开发领域的实战应用,重点围绕 JavaFX 框架,涵盖界面布局、控件使用、事件处理、FXML、样式美化(CSS)、多线程与UI响应优化,以及桌面应用的打包与发布。通过完整示例项目,帮助学习者掌握 使用 Java 构建现代化、跨平台桌面应用程序的核心能力。

65

2026.01.14

php与html混编教程大全
php与html混编教程大全

本专题整合了php和html混编相关教程,阅读专题下面的文章了解更多详细内容。

36

2026.01.13

PHP 高性能
PHP 高性能

本专题整合了PHP高性能相关教程大全,阅读专题下面的文章了解更多详细内容。

75

2026.01.13

MySQL数据库报错常见问题及解决方法大全
MySQL数据库报错常见问题及解决方法大全

本专题整合了MySQL数据库报错常见问题及解决方法,阅读专题下面的文章了解更多详细内容。

21

2026.01.13

PHP 文件上传
PHP 文件上传

本专题整合了PHP实现文件上传相关教程,阅读专题下面的文章了解更多详细内容。

35

2026.01.13

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Vue 教程
Vue 教程

共42课时 | 6.5万人学习

Django 教程
Django 教程

共28课时 | 3.1万人学习

React 教程
React 教程

共58课时 | 3.7万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号