在centos上通过yum安装PHP+Nginx+Mysql+phpmyadmin

我的服务器环境是DigitalOcean的Contos6.5 64位。

Step 1:首先安装Yum所需要的所有软件。

因为nginx和php-fpm都不能直接通过Yum直接安装,所以我们需要下载两个仓库到我们的VPS上。

sudo rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm

Step 2:安装Mysql

sudo yum install mysql mysql-server

安装完毕后,重启Mysql

sudo /etc/init.d/mysqld restart

重启完后,我们可以启动Mysql的安全配置项

sudo /usr/bin/mysql_secure_installation

这里会要求你输入root密码。

Enter current password for root (enter for none): OK, successfully used password, moving on...

因为是刚装的Mysql,所以没有密码,直接回车即可,代表空。

然后Mysql会要求你为root用户设置密码。

接下来Mysql会要求你输入一系列选项,最简单的方案是全部输入Y。具体内容可以阅读对应的选项。

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y                                            
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

Step 3: 安装nginx

sudo yum install nginx

因为nginx不会自启动,所以需要手动启动,命令如下。

sudo /etc/init.d/nginx start

你可以通过在浏览器上直接输入ip地址来确认你的nginx是否已经成功安装。

通过运行下面的命令来显示你服务器的ip地址。

ifconfig eth0 | grep inet | awk '{ print $2 }'

Step 4:安装php

因为php包是在REMI仓库中的,并且目前是被禁用的。我们首先要做的就是启用REMI仓库并且安装php和php-fpm还有php-mcrypt(phpmyadmin需要php-mcrypt和php-mbstring扩展,不安装可能会在启动phpmyadmin时候导致模块缺失的错误。你还可以选择安装其他所需要的扩展,已经在下面列出)。

sudo yum --enablerepo=remi,remi-php55 install php-fpm php-mysql php-mcrypt php-mbstring

更多扩展项如下:

yum --enablerepo=remi,remi-php55 install php-cli php-gd php-pear php-mysqlnd php-pdo php-pgsql php-pecl-mongo php-sqlite php-pecl-memcached php-pecl-memcache php-mbstrin php-xml php-soap php-mcrypt php-fpm

Step 5:配置php

对于php配置文件我们需要做一些细微的改动。

sudo vi /etc/php.ini

找到有cgi.fix_pathinfo=1的这一行,把1改成0。在用vi编辑器时候,可以通过输入”/”+”你所想查找的内容”进行快速查找。

cgi.fix_pathinfo=0

关于这个选项的配置的原文解释为:

If this number is kept as a 1, the php interpreter will do its best to process the file that is as near to the requested file as possible. This is a possible security risk. If this number is set to 0, conversely, the interpreter will only process the exact file path—a much safer alternative. Save and Exit.

总结一下,就是在设置成0的以后,会执行精确的文件路径,而不是在设置1时候,会查找最相近的文件进行执行,这会导致潜在的安全威胁。

Step 6:配置nginx

打开默认的nginx配置文件

sudo vi /etc/nginx/nginx.conf

将worker processes的数字提升到4,保存并退出。

现在来配置nginx的virtual hosts。

sudo vi /etc/nginx/conf.d/default.conf

相关配置和配置改动(在配置的下面)如下。

#
# The default server
#
server {
    listen 80;
    server_name example.com;
    location / {
         root /usr/share/nginx/html;
         index index.php index.html index.htm;
    }

    error_page 404 /404.html;
    location = /404.html {
        root /usr/share/nginx/html;
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root /usr/share/nginx/html;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

配置的改变内容如下:

  • 在index栏,添加了index.php。注:如果这里没有添加,一旦你的项目目录里只有index.php,没有index.html的话,会出现403 forbidden error。关于403 forbidden error这个常见错误,可以参考403 forbidden error at Nginx这篇文章
  • 改变你的server_name为你的域名或者ip地址(将配置中的example.com替换即可)
  • 将root改为 /usr/share/nginx/html;
  • 将开头为 “location ~ \.php$ {“,的部分去掉注释#
  • 改变root将其指到真实的文件root, /usr/share/nginx/html;
  • 改变fastcgi_param栏,帮助php编译器找到我们存储在文件root home的php脚本。

保存并退出。

打开php-fpm配置

sudo vi /etc/php-fpm.d/www.conf

在用户和组中,用nginx代替apache

[...]
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
; RPM: apache Choosed to be able to access some dir as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
[...]

保存并重启php-fpm。

sudo service php-fpm restart

Setp 7:创建php info页面查看配置结果

首先我们创建一个新的文件。

sudo vi /usr/share/nginx/html/info.php

加入

<!--?php phpinfo(); ?-->;

保存并退出。

重启nginx,使配置生效。(注:将来对配置进行任何更改后,都要重启,配置才会生效。)

访问你在配置中输入的域名或者ip地址对应的php info页面。http://12.34.56.789/info.php或者http://www.yourdomain.com/info.php

显示的页面应该类似于此页面

Step 8:设置自启动。

最后一步就是将刚刚装的所有程序设置自启动。

sudo chkconfig --levels 235 mysqld on
sudo chkconfig --levels 235 nginx on
sudo chkconfig --levels 235 php-fpm on

恭喜,你已经完成了配置部分。接下来我们将要安装phpmyadmin。

Step 9:安装phpmydamin。

去phpmyadmin主页下载最新版本的phpmyadmin,举例如下:

创建temp文件夹接受下载文件

mkdir /root/temp
cd /root/temp

通过wget下载phpmyadmin,并解压。(注:也可以通过其他方式下载到,甚至可以在官网上下载到本地,再通过FTP上传到VPS服务器,根据个人喜好来)

wget http://ufpr.dl.sourceforge.net/project/phpmyadmin/phpMyAdmin/4.0.8/phpMyAdmin-4.0.8-all-languages.zip
unzip phpMyAdmin-4.0.8-all-languages.zip

将phpmyadmin移动至目标文件夹,并生成配置文件config.inc.php。(注:目标文件夹可以根据自己喜好来定)

 mv phpMyAdmin-4.0.8-all-languages phpmyadmin
 mv phpmyadmin /var/www/html/
 cd /var/www/html/phpmyadmin
 mv config.sample.inc.php config.inc.php

 
编辑config.inc.php,将下面这一行

 $cfg['Servers'][$i]['auth_type'] = 'cookie';

替换成:

 $cfg['Servers'][$i]['auth_type'] = 'http';

配置Nginx的server host

sudo vi /etc/nginx/conf.d/phpmyadmin.conf

具体配置如下:

#
# phpmyadmin
#
server {
listen       8090;
server_name  yourdomain.com;

#charset koi8-r;

#access_log  logs/host.access.log  main;

location / {
root   /var/www/html/phpmyadmin;
index  index.php index.html index.htm;
}

error_page  404              /404.html;
location = /404.html {
root   /usr/share/nginx/html;
}

# redirect server error pages to the static page /50x.html</div>
<div>    #</div>
<div>    error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   <a href="http://127.0.0.1">http://127.0.0.1</a>;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {</div>
<div>        root           /var/www/html/phpmyadmin;</div>
<div>        fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
include        fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#    deny  all;
#}

小提示:此类配置文件其实可以通过复制default.conf来减轻输入工作量。

复制后需要修改的内容:

  •  listen       80 default_server; 这个部分。可以将80改成你想要的任何端口号。可以去掉default_server,如果你想重复使用这个端口时候,有default_server会报错。
  • root路径,需要改成你所需要的路径。

最后重启Nginx使配置生效

sudo service nginx reload

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值