如果是阿里云服务器的话首先需要到控制台设置安全组的规则,开放80,3306,等常用的端口
查看服务器
# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
安装Nginx
美团云的CentOS系统模板中配置了内网源,下载速度较快,推荐使用yum
安装Nginx:
sudo yum install nginx
按照提示,输入yes
后开始安装。安装完毕后,Nginx的配置文件在/etc/nginx
目录下。使用以下命令启动Nginx:
sudo systemctl start nginx
检查系统中firewalld
防火墙服务是否开启,如果已开启,我们需要修改防火墙配置,开启Nginx外网端口访问。
sudo systemctl status firewalld
如果显示active (running),则需要调整防火墙规则的配置。
修改/etc/firewalld/zones/public.xml
文件,在zone
一节中增加:
<zone>
...
<service name="nginx"/>
<zone>
保存后重新加载firewalld服务:
sudo systemctl reload firewalld
您可以通过浏览器访问 http://<外网ip地址>
来确定Nginx是否已经启动。
最后将Nginx设置为开机启动:
sudo systemctl enable nginx.service
安装MySQL(MariaDB)
MariaDB是MySQL的一个分支,主要由开源社区进行维护和升级,而MySQL被Oracle收购以后,发展较慢。在CentOS 7的软件仓库中,将MySQL更替为了MariaDB。
我们可以使用yum
直接安装MariaDB:
sudo yum install mariadb-server
安装完成之后,执行以下命令重启MariaDB服务:
sudo systemctl start mariadb
MariaDB默认root密码为空,我们需要设置一下,执行脚本:
sudo /usr/bin/mysql_secure_installation
这个脚本会经过一些列的交互问答来进行MariaDB的安全设置。
首先提示输入当前的root密码:
Enter current passWord for root (enter for none):
初始root密码为空,我们直接敲回车进行下一步。
Set root password? [Y/n]
设置root密码,默认选项为Yes
,我们直接回车,提示输入密码,在这里设置您的MariaDB的root账户密码。
Remove anonymous users? [Y/n]
是否移除匿名用户,默认选项为Yes
,建议按默认设置,回车继续。
Disallow root login remotely? [Y/n]
是否禁止root用户远程登录?如果您只在本机内访问MariaDB,建议按默认设置,回车继续。 如果您还有其他云主机需要使用root账号访问该数据库,则需要选择n
。
Remove test database and access to it? [Y/n]
是否删除测试用的数据库和权限? 建议按照默认设置,回车继续。
Reload PRivilege tables now? [Y/n]
是否重新加载权限表?因为我们上面更新了root的密码,这里需要重新加载,回车。
完成后你会看到Success!
的提示,MariaDB的安全设置已经完成。我们可以使用以下命令登录MariaDB:
mysql -uroot -p
按提示输入root密码,就会进入MariaDB的交互界面,说明已经安装成功。
最后我们将MariaDB设置为开机启动。
sudo systemctl enable mariadb
安装PHP
我们可以直接使用yum
安装PHP:
sudo yum install php-fpm php-mysql
安装完成后我们将php-fpm启动:
sudo systemctl start php-fpm
将php-fpm设置为开机启动:
sudo systemctl enable php-fpm
php安装完成之后,需要设置一下php session的目录:
sudo mkdir /var/lib/php/session/
sudo chown -R apache:apache /var/lib/php/session/
这时php-fpm已经安装完毕,但是现在需要配置一下Nginx,在/etc/nginx/conf.d
目录中新建一个名为php.conf
的文件,其内容为:
server {
listen 8000;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /usr/share/php;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
然后执行以下命令使我们的配置生效:
sudo systemctl reload nginx
以上我们配置了Nginx的8000端口用来测试,如果您在美团云控制台创建机器时选择了绑定防火墙,需要检查该防火墙是否允许8000端口,如果不允许的话,您可以在防火墙设置中新增防火墙,并关联到该主机。
我们在/usr/share/php
目录下新建一个名为phpinfo.php
的文件用来展示phpinfo信息,文件内容为:
<?php echo phpinfo(); ?>
我们从浏览器打开 http://<外网IP地址>:8000/phpinfo.php
,您就能看到phpinfo信息了,说明我们php环境已经部署成功:
centos7 直接在conf.d文件夹下创建test.conf
如下
server {
listen 80;
server_name nc.kaixinnc1.cn;
charset utf-8;
#access_log logs/host.access.log main;
root /var/www/nc;
index index.html index.htm index.php;
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$ {
expires 7d;
access_log off;
}
}
配置yum源
以下是CentOS 7.0的源。
# yum install epel-release
# rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
- 1
- 2
使用yum list命令查看可安装的包(Packege)。
# yum list --enablerepo=remi --enablerepo=remi-php56 | grep php
- 1
安装PHP5.6
yum源配置好了,下一步就安装PHP5.6。
yum install --enablerepo=remi --enablerepo=remi-php56 php php-opcache php-devel php-mbstring php-mcrypt php-mysqlnd php-phpunit-PHPUnit php-pecl-xdebug php-pecl-xhprof
用PHP命令查看版本。
- 1
- 2
php --version
PHP 5.6.0 (cli) (built: Sep 3 2014 19:51:31)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies
with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend Technologies
with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans