您现在的位置是:首页 > 文章详情

阿里云centos7 LNMP开发环境搭建

日期:2019-01-16点击:304

我购买的阿里云是centos7.3的.在Linux下,搭建服务最好当然是LNMP了,毕竟PHP是世界上最好的语言(开玩笑了 0.0)。当然LAMP也很好,我就是Apache从转投Nginx的,就因为Nginx配置简单,资源占用低.当然Apache也有他的优点,孰优孰劣,还是要根据自己的需要取舍吧。
言归正传,LNMP是Linux、Nginx、MySQL(Mariadb)、PHP。在centos上,默认的数据不再是MySQL了,成了Mariadb,这是MySQL的社区版,基本上是完全兼容MySQL的,对普通开发者而言,基本感觉得不到差别的。
开始搭建环境

安装Nginx

安装Nginx,在Linux上可是使用yum安装Nginx,也可以编译源码安装,鉴于我购买的服务器性能的原因,我还是用yum安装吧.

  1. 使用yum search nginx 查看是否Nginx包,阿里云的centos默认是有Nginx包的.
  2. 使用yum -y install nginx 使用-y会自动解决依赖问题
  3. 等待安装完成,没有报错的话,安装成功了。使用service nginx start开启Nginx服务,使用 systemctl enable nginx把Nginx设为开机启动。
    使用curl 127.0.0.1看一下,返回html代码,说明成功了

安装Mariadb或者MySQL

安装Mariadb或者MySQL,在centos中,Mariadb可以直接使用yum安装。由于centos的yum中默认没有MySQL的源,必须手动添加MySQL源,才能安装。

安装Mariadb:

  1. yum -y install mariadb mariadb-server 不出意外的话,会成功安装.
  2. service mariadb start开启Nginx服务,使用 systemctl enable mariadb把mariadb设为开机启动。
  3. 简单配置Mariadb 输入mysql_secure_installation
    1.显示 Enter current password for root (enter for none): 第一次运行直接回车

2.Set root password? [Y/n] 是否设置root用户密码,输入y并回车或直接回车
3.New password: 设置root用户的密码
4.Re-enter new password: 再输入一次你设置的密码
5.Remove anonymous users? [Y/n] 是否删除匿名用户,回车
6.Disallow root login remotely? [Y/n] 是否禁止root远程登录,回车,
7.Remove test database and access to it? [Y/n] 是否删除test数据库,回车
8.Reload privilege tables now? [Y/n] 是否重新加载权限表,回车

  1. mysql -uroot -ppassword 登录测试一下

centos中的Mariadb版本是5.5,对标的MySQL5.5版本,最新的稳定版Mariadb是10.3,怎么安装最新版的Mariadb等以后有时间再更新

安装MySQL:

  1. 添加MySQL的yum源wget https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
  2. rpm -Uvh platform-and-version-specific-package-name.rpm添加MySQL
  3. 现在默认会安装MySQL8.0版本,如果想要5.7大的话   1.yum-config-manager --disable mysql80-community 2.yum-config-manager --enable mysql57-community
  4. 开始安装yum install mysql-community-server
  5. service mysqld start开启MySQL服务,
  6. systemctl enable mysql 设为开机启动
  7. 这时候MySQL是没有没密码的,需要找到密码sudo grep 'temporary password' /var/log/mysqld.log能看到密码
    8.登录,mysql -uroot -p 看到的密码
  8. 修改密码 ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';

附上MySQL官网的安装教程传送门

安装php

php可以使用源码安装,也可以使用yum安装,鉴于我的服务器的性能,还是选择了yum安装。yun安装首先要添加源。

  1. 配置yum源 rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

  1. 安装php7.2和需要的拓展 yum -y install php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-fpm php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml
  2. 开启php-fpm 服务, service php-fpm start systemctl enable php-fpm设为开机启动

好了LNMP需要的软件都安装好了,简单配置一下

配置

Nginx配置

  1. cd /etc/nginx/
  2. vim nginx.conf
  3. 修改配置文件
server { listen 80 default_server; listen [::]:80 default_server; server_name _; root /home/www/default; return 500;#我禁用了ip访问 # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } location ~ \.php$ { root /home/www/default; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }

php相关的部分是这里

location ~ \.php$ { root /home/www/default; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }

重启Nginx,service nginx restart
好了去 Nginx的的root目录里新建一个info.php文件,测试一下

<?php phpinfo(); ?>

访问一下你的服务器看一下,能看到php的信息,说明已经配置好了

到这里,LNMP开发环境已经搭建好了,可以愉快的开发了。

在实际应用中,一台服务上一般不会只有一个网站,一台服务器运行多个网站,最靠谱的办法就是建立多个虚拟主机,使用不同的域名,访问不同的网站。
这个问题,等有空再更新,

下次更新,Nginx虚拟主机的配置

原文链接:https://yq.aliyun.com/articles/687984
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章