今天抽空在阿里云上部署安装了PHP的环境

主要有nginx, php5 php-fpm mysql phpmyadmin

本文来源于:http://www.lonelycoder.be/nginx-php-fpm-mysql-phpmyadmin-on-ubuntu-12-04/

Since 3 years I’m completely into Nginx. For some reason I was always struggling with Apache, and that kept me from running my own server. But then a colleague told me about Nginx, and how great it was. So I looked into it, and the things I read were great. Compared to Apache, Nginx is smaller in size, it multiplies the performance remarkably by recducing the RAM and CPU usage for real time applications and it’s very flexible. Of course Apache will also have its advantages, but the fact I can’t come up with one says enough (about me, or Apache ;-)).

So today I’m going to show you how to setup Nginx with PHP 5 and MySQL on Ubuntu 12.04. It’s really not that difficult. Let’s start with Nginx.

1
sudo apt-get install nginx -y

That’s it. But now we want to configure Nginx. I normally use Sublime Text 2 as a text editor, because VI hates me (or I hate VI, really hard to tell). But on a remote server sublime is not really an option, so I will just use nano. Feel free to use the editor you prefer.

You can download the config here.

1
2
3
cd /etc/nginx
sudo cp nginx.conf nginx.conf.backup
sudo nano nginx.conf
01
02
03
04
05
06
07
08
09
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
user www-data;
 
# As a thumb rule: One per CPU. If you are serving a large amount
# of static files, which requires blocking disk reads, you may want
# to increase this from the number of cpu_cores available on your
# system.
#
# The maximum number of connections for Nginx is calculated by:
# max_clients = worker_processes * worker_connections
worker_processes 1;
 
# Maximum file descriptors that can be opened per process
# This should be > worker_connections
worker_rlimit_nofile 8192;
 
events {
    # When you need > 8000 * cpu_cores connections, you start optimizing
    # your OS, and this is probably the point at where you hire people
    # who are smarter than you, this is *a lot* of requests.
    worker_connections 8000;
}
 
error_log /var/log/nginx/error.log;
 
pid /var/run/nginx.pid;
 
http {
    charset utf-8;
 
    # Set the mime-types via the mime.types external file
    include mime.types;
 
    # And the fallback mime-type
    default_type application/octet-stream;
 
    # Click tracking!
    access_log /var/log/nginx/access.log;
 
    # Hide nginx version
    server_tokens off;
 
    # ~2 seconds is often enough for HTML/CSS, but connections in
    # Nginx are cheap, so generally it's safe to increase it
    keepalive_timeout 20;
 
    # You usually want to serve static files with Nginx
    sendfile on;
 
    tcp_nopush on; # off may be better for Comet/long-poll stuff
    tcp_nodelay off; # on may be better for Comet/long-poll stuff
 
    server_name_in_redirect off;
    types_hash_max_size 2048;
 
    gzip on;
    gzip_http_version 1.0;
    gzip_comp_level 5;
    gzip_min_length 512;
    gzip_buffers 4 8k;
    gzip_proxied any;
    gzip_types
        # text/html is always compressed by HttpGzipModule
        text/css
        text/plain
        text/x-component
        application/javascript
        application/json
        application/xml
        application/xhtml+xml
        application/x-font-ttf
        application/x-font-opentype
        application/vnd.ms-fontobject
        image/svg+xml
        image/x-icon;
 
    # This should be turned on if you are going to have pre-compressed copies (.gz) of
    # static files available. If not it should be left off as it will cause extra I/O
    # for the check. It would be better to enable this in a location {} block for
    # a specific directory:
    # gzip_static on;
 
    gzip_disable "msie6";
    gzip_vary on;
 
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

Change the default site config. You can download the config here.

1
2
sudo cp sites-available/default sites-available/default.backup
sudo nano sites-available/default
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
server {
    listen 80 default; ## listen for ipv4; this line is default and implied
    listen [::]:80 default ipv6only=on; ## listen for ipv6
 
    # Make site accessible from http://localhost/ or server IP-address
    server_name localhost;
    server_name_in_redirect off;
 
    charset utf-8;
 
    access_log /usr/share/nginx/access.log;
    error_log /usr/share/nginx/error.log;
 
    root /usr/share/nginx/www;
    index index.php index.html index.htm;
 
    location / {
        # First attempt to serve request as file, then
        # as directory, then trigger 404
        try_files $uri $uri/ =404;
    }
}

Now we need to reload Nginx.

1
sudo service nginx reload

Try http://localhost/ (or http://your-server-ip-address) and hopefully you will see the welcome page of Nginx. Nice! Next, MySQL. Just follow the on screen instructions.

1
sudo apt-get install mysql-server mysql-client -y

To have a secure installation, we execute the following command:

1
sudo mysql_secure_installation

Follow the instructions. Start by entering your MySQL root password. If you did not set one yet, do it! It’s just that easy. So we can continue with PHP.

1
sudo apt-get install php5-fpm php5-cli php5-mysql -y

If you don’t want to run PHP from console, you can skip php5-cli. If you are planning to use the Symfony2 framework I would suggest you keep it, you will need it. By default fpm and cli use their own php.ini configuration file. I usualy want them to use the same one. If you want that too, do the following:

1
2
3
cd /etc/php5/cli
sudo mv php.ini php.ini.backup
sudo ln -s ../fpm/php.ini

Once php-fpm is installed, we need to configure Nginx again.

1
2
cd /etc/nginx
sudo nano nginx.conf

Add the following to the http {} part.

1
2
3
4
# Upstream to abstract back-end connection(s) for PHP
upstream php {
    server unix:/tmp/php5-fpm.sock;
}

Prepare the default site so it can serve PHP pages (needs to be in the server {} part).

1
sudo nano /etc/nginx/sites-available/default
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
# pass the PHP scripts to FPM socket
location ~ \.php$ {
    try_files $uri =404;
 
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
     
    fastcgi_pass php;
 
    fastcgi_index index.php;
 
    fastcgi_param SCRIPT_FILENAME /usr/share/nginx/www$fastcgi_script_name;
    fastcgi_param DOCUMENT_ROOT /usr/share/nginx/www;
 
    # send bad requests to 404
    fastcgi_intercept_errors on;
 
    include fastcgi_params;
}

Open /etc/php5/fpm/pool.d/www.conf and look for the following line ..

1
2
cd /etc/php5/fpm/pool.d
sudo nano www.conf
1
listen = 127.0.0.1:9000

.. and change it into ..

1
listen = /tmp/php5-fpm.sock

Save and restart both Nginx and PHP-FPM.

1
2
sudo service nginx restart
sudo service php5-fpm restart

Create a PHP file in your web root.

1
nano /usr/share/nginx/www/index.php
1
2
3
4
5
<?php
 
phpinfo();
 
?>

Save the file and refresh http://localhost/ (or http://your-server-ip-address). If everything goes well you have a nice page with your PHP configuration explained, if not you can replace “/tmp/php5-fpm.sock” by “/var/run/php5-fpm.sock” in both /etc/nginx/nginx.conf and /etc/php5/fpm/pool.d/www.conf and reload nginx and php5-fpm (possible solution by Flávio Moringa, thnx Flávio!).

The final thing to do is install phpMyAdmin.

1
sudo apt-get install phpmyadmin -y

Go to your web root and link phpMyAdmin.

1
2
cd /usr/share/nginx/www
sudo ln -s /usr/share/phpmyadmin

Now you should be able to go to http://localhost/phpmyadmin (or http://your-server-ip-address/phpmyadmin). In a production environment I try not to use phpMyAdmin, and if I really need it I use another alias. For example poiul. It makes it harder for others to find it.

Nginx + PHP-FPM + MySQL + phpMyAdmin on Ubuntu (aliyun)的更多相关文章

  1. 记录一次自己对nginx+fastcgi(fpm)+mysql压力测试结果

    nginx + fastcgi(fpm) 压力测试: CentOS release 5.9 16核12G内存 静态页面: 并发1000,压测200秒,测试结果: 系统最大负载5.47 成功响应: 25 ...

  2. Ubuntu 麒麟版下安装:Apache+php5+mysql+phpmyadmin.

    摘要 LAMP是Linux web服务器组合套装的缩写,分别是Apache+MySQL+PHP.此文记录在Ubuntu上安装Apache2服务器,包括PHP5(mod_php)+MySQL+phpmy ...

  3. Ubuntu 16.04 LTS nodejs+pm2+nginx+git 基础安装及配置环境(未完,未整理)

    -.Ubuntu 安装nodejs 以下内容均在命令行,完成,首先你要去你电脑的home目录:cd ~. [sudo] apt-get update [sudo] apt-get upgrade ap ...

  4. 使用Docker快速搭建Nginx+PHP-FPM+MySQL+phpMyAdmin环境

    一.概述 环境介绍 操作系统:centos 7.6 docker版本:19.03.8 ip地址:192.168.31.34 本文将介绍如何使用单机部署Nginx+PHP-FPM环境 二.Nginx+P ...

  5. mysql数据库创建database(实例),和用户,并授权

    前言:mysql创建用户的方法分成三种:INSERT USER表的方法.CREATE USER的方法.GRANT的方法. 一.账号名称的构成方式 账号的组成方式:用户名+主机(所以可以出现重复的用户名 ...

  6. 把Asp.net Core程序代码部署到Ubuntu(不含数据库)

    今天记录一下第一次把.net core 程序发布到linux系统.linux用的是ubuntu Server 18.04版本.运行的IDE是vs 2019,发布出来是.net core 2.2版本. ...

  7. Linux环境Nginx安装、调试以及PHP安装(转)

      linux版本:64位CentOS 6.4 Nginx版本:nginx1.8.0 php版本:php5.5 1.编译安装Nginx 官网:http://wiki.nginx.org/Install ...

  8. MySQL数据库的优化(上)单机MySQL数据库的优化

    MySQL数据库的优化(上)单机MySQL数据库的优化 2011-03-08 08:49 抚琴煮酒 51CTO 字号:T | T 公司网站访问量越来越大,导致MySQL的压力越来越大,让我们自然想到的 ...

  9. Nginx反向代理+负载均衡简单实现(https方式)

    背景:A服务器(192.168.1.8)作为nginx代理服务器B服务器(192.168.1.150)作为后端真实服务器 现在需要访问https://testwww.huanqiu.com请求时从A服 ...

随机推荐

  1. nodejs之路-[0]安装及简易配置

    题外话: 之前写过ubuntu下编译nodejs- 传送门:Ubuntu15.04编译安装nodejsV0.12.3 只是如今基本在win下做开发了-. 就以这篇帖子为开头,作为我踏上nodejs之路 ...

  2. redis学习笔记——客户端

    Redis服务器是典型的一对多服务器程序:一个服务器可以与多个客户端建立网络连接,每个客户端可以向服务器发送命令请求,而服务器则接收并处理客户端发送的命令请求,并向客户端返回命令回复. 对于每个与服务 ...

  3. Android Studio 项目中集成百度地图SDK报Native method not found: com.baidu.platform.comjni.map.commonmemcache.JNICommonMemCache.Create:()I错误

    Android Studio 项目中集成百度地图SDK报以下错误: java.lang.UnsatisfiedLinkError: Native method not found: com.baidu ...

  4. Android 底部TabActivity(1)——FragmentActivity

    先看看效果图: 第一篇Tab系列的文章首先实现这样的风格的底部Tab:背景条颜色不变,我们是用了深灰的颜色,图标会发生对应的变化.当选中某个标签后该标签的背板会由正常的颜色变为不正常,哈哈,是变为加深 ...

  5. java设计模式之模板方法

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8744002 今天你还是像往常一样来上班,一如既往地开始了你的编程工作. 项目经理告 ...

  6. mysql 创建函数或者存储过程,定义变量报错

    报错的原因是因为在过程或者函数中存在分隔符 分号(:),而mysql中默认分隔符也是 :,这就导致存储过程分开了 在存储过程外面包一层 delimiter //   code  //就行了

  7. Why do Antennas Radiate?

    Obtaining an intuitive idea for why antennas radiate is helpful in understanding the fundamentals of ...

  8. js设置百分比保留两位小数

      CreateTime--2017年8月23日11:03:31Author:Marydon js设置百分比保留两位小数 错误用法: var percent = (num1/num2) * 100%; ...

  9. JAVA小项目实例源码—学习娱乐小助手

    代码地址如下:http://www.demodashi.com/demo/11456.html 一.程序实现 项目目录: MyJFrame:实现项目界面样式: AppProcess:实现调用api或爬 ...

  10. (七)Oracle学习笔记—— 游标

    1.游标简介 游标用来处理从数据库中检索的多行记录(使用SELECT语句).利用游标,程序可以逐个地处理和遍历一次检索返回的整个记录集. 为了处理SQL语句,Oracle将在内存中分配一个区域,这就是 ...