从零开始搭建LNMP

从零开始搭建LNMP(linux nginx mysql php)

纵使现在已经有很多的一键搭建环境,但博主还是比较喜欢自己搭建,比较这样会比较安全,本教程在阿里云,腾讯云服务器都进行过测试安装服务器均为centos7.2 64位

介绍一下环境
阿里云 centos7.2 64位
腾讯云 centos7.2 64位

搭建PHP环境
nginx1.10.2
mysql5.6
php7.0

首次登录服务器后

第一步

yum update
#过程需要按 'y' 确认升级

前提工作

第二步

安装epel-release

rpm -ivh http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-10.noarch.rpm

安装PHP7的rpm源

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

安装mysql源

rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

为什么需要安装这些源呢?
因为centos官方提供的源没有收录这些软件 或者 软件比较旧

开始安装

第三步

首先安装 nginx

nginx 并不是centos官方源的包 在虚拟机 安装nginx 可能会找不到该包 所以各位可以自行百度安装nginx的源

yum search nginx
#搜索源中是否有nginx包
yum info nginx
#查看源中nginx的信息
yum install nginx
#安装nginx及其依赖
#过程需要按'y'确认安装
#当安装好了 底部会有以下提示
Complete!
#启动服务
systemctl start nginx
#开机启动
systemctl enable nginx

安装php

因为yum自带php默认是5.4
所以我们需要先找到到我们需要的版本

yum search php

这样 服务器会列出一大堆包

geos-php.x86_64 : PHP modules for GEOS
graphviz-php.x86_64 : PHP extension for graphviz
mlt-php.x86_64 : PHP package to work with MLT
mod_php71w.x86_64 : PHP module for the Apache HTTP Server
perl-PHP-Serialization.noarch : Converts between PHP's serialize() output and the equivalent Perl structure
php.x86_64 : PHP scripting language for creating dynamic web sites
php-Assetic.noarch : Asset Management for PHP
php-EasyRdf.noarch : A PHP library designed to make it easy to consume and produce RDF
php-EasyRdf-doc.noarch : Documentation for php-EasyRdf
php-Faker.noarch : A PHP library that generates fake data
php-JsonSchema.noarch : PHP implementation of JSON schema
php-Metadata.noarch : Class/method/property metadata management in PHP
php-PHPMailer.noarch : PHP email transport class with a lot of features
php-PHPParser.noarch : A PHP parser written in PHP
php-PhpCollection.noarch : General purpose collection library for PHP
php-PhpOption.noarch : Option type for PHP
...
...
php70w-opcache.x86_64 : An opcode cache Zend extension
php70w-pecl-apcu.x86_64 : APCu - APC User Cache
php70w-pecl-apcu-devel.x86_64 : APCu developer files (header)
php70w-pecl-geoip.x86_64 : Extension to map IP addresses to geographic places
php70w-pecl-igbinary-devel.x86_64 : Igbinary developer files (header)
php70w-pecl-imagick.x86_64 : Provides a wrapper to the ImageMagick library
php70w-pecl-imagick-devel.x86_64 : Imagick developer files (header)
php70w-pecl-libsodium.x86_64 : Wrapper for the Sodium cryptographic library
php70w-pecl-memcached.x86_64 : Extension to work with the Memcached caching daemon
php70w-pecl-mongodb.x86_64 : PECL package MongoDB driver
php70w-pecl-redis.x86_64 : Extension for communicating with the Redis key-value store
php71w-opcache.x86_64 : An opcode cache Zend extension
php71w-pecl-apcu.x86_64 : APCu - APC User Cache
php71w-pecl-apcu-devel.x86_64 : APCu developer files (header)
php71w-pecl-geoip.x86_64 : Extension to map IP addresses to geographic places
php71w-pecl-igbinary-devel.x86_64 : Igbinary developer files (header)
php71w-pecl-imagick.x86_64 : Provides a wrapper to the ImageMagick library
php71w-pecl-imagick-devel.x86_64 : Imagick developer files (header)
php71w-pecl-libsodium.x86_64 : Wrapper for the Sodium cryptographic library
php71w-pecl-memcached.x86_64 : Extension to work with the Memcached caching daemon
php71w-pecl-mongodb.x86_64 : PECL package MongoDB driver
php71w-pecl-redis.x86_64 : Extension for communicating with the Redis key-value store
phpMyAdmin.noarch : Handle the administration of MySQL over the World Wide Web
phpPgAdmin.noarch : Web-based PostgreSQL administration
phpldapadmin.noarch : Web-based tool for managing LDAP servers
phpwapmail.noarch : WAP-based e-mail client
psysh.noarch : A runtime developer console, interactive debugger and REPL for PHP
squirrelmail.noarch : webmail client written in php
xhprof.noarch : A Hierarchical Profiler for PHP - Web interface

  Name and summary matches only, use "search all" for everything.

这里包括php几个版本,以及其扩展
我们需要按的是7.0

yum info php70w.x86_64
yum info php70w-fpm.x86_64
yum info php70w-mysql.x86_64
#查看 以上包的信息
yum install php70w.x86_64 php70w-fpm.x86_64 php70w-mysql.x86_64
#安装php7.0以及mysql扩展
#按'y'确认安装
#安装完成 会在底部出现以下字样
#Complete!
#启动php
systemctl start php-fpm
#开机启动
systemctl enable php-fpm

最后安装数据库

在centos7后 mysql在yum中被改名为 mariadb 自带的版本也是相对比较低 好像是5.4

yum search mysql

以下是列出的yum包

MySQL-python.x86_64 : An interface to MySQL
MySQL-zrm.noarch : MySQL backup manager
akonadi-mysql.x86_64 : Akonadi MySQL backend support
apr-util-mysql.x86_64 : APR utility library MySQL DBD driver
collectd-mysql.x86_64 : MySQL plugin for collectd
dmlite-plugins-mysql.x86_64 : MySQL plugin for dmlite
dovecot-mysql.x86_64 : MySQL back end for dovecot
dpm-copy-server-mysql.x86_64 : DPM copy server with MySQL database back-end
dpm-name-server-mysql.x86_64 : DPM name server with MySQL database back-end
dpm-server-mysql.x86_64 : Disk Pool Manager (DPM) server with MySQL database back-end
dpm-srm-server-mysql.x86_64 : DPM SRM server with MySQL database back-end
dspam-mysql.x86_64 : MySQL storage driver for libdspam
exim-mysql.x86_64 : MySQL lookup support for Exim
freeradius-mysql.x86_64 : MySQL support for freeradius
fts-mysql.x86_64 : File Transfer Service V3 mysql plug-in
gnokii-smsd-mysql.x86_64 : MySQL support for Gnokii SMS daemon
holland-mysql.noarch : MySQL library functionality for Holland Plugins
holland-mysqldump.noarch : Logical mysqldump backup plugin for Holland
holland-mysqllvm.noarch : Holland LVM snapshot backup plugin for MySQL
lfc-server-mysql.x86_64 : LCG File Catalog (LFC) server with MySQL database back-end
libdbi-dbd-mysql.x86_64 : MySQL plugin for libdbi
libgda-mysql.x86_64 : Mysql provider for libgda
libnss-mysql.x86_64 : NSS library for MySQL
libodb-mysql.x86_64 : MySQL ODB runtime library from Code Synthesis
libodb-mysql-devel.x86_64 : Development files for libodb-mysql
lighttpd-mod_authn_mysql.x86_64 : Authentication module for lighttpd that uses a MySQL database
lighttpd-mod_mysql_vhost.x86_64 : Virtual host module for lighttpd that uses a MySQL database
mysql++.x86_64 : C++ wrapper for the MySQL C API
mysql++-devel.x86_64 : MySQL++ developer files (headers, examples, etc.)
mysql++-manuals.x86_64 : MySQL++ user and reference manuals
mysql-community-bench.x86_64 : MySQL benchmark suite
mysql-community-client.i686 : MySQL database client applications and tools
mysql-community-client.x86_64 : MySQL database client applications and tools
mysql-community-common.i686 : MySQL database common files for server and client libs
mysql-community-common.x86_64 : MySQL database common files for server and client libs
mysql-community-devel.i686 : Development header files and libraries for MySQL database client applications
mysql-community-devel.x86_64 : Development header files and libraries for MySQL database client applications
mysql-community-embedded.x86_64 : MySQL embedded library
mysql-community-embedded.i686 : MySQL embedded library
mysql-community-embedded-devel.x86_64 : Development header files and libraries for MySQL as an embeddable library
mysql-community-embedded-devel.i686 : Development header files and libraries for MySQL as an embeddable library
mysql-community-libs.i686 : Shared libraries for MySQL database client applications
mysql-community-libs.x86_64 : Shared libraries for MySQL database client applications
mysql-community-release.noarch : MySQL repository configuration for yum
mysql-community-test.x86_64 : Test suite for the MySQL database server
mysql-connector-java.noarch : Official JDBC driver for MySQL
mysql-connector-odbc.x86_64 : An ODBC 5.3 driver for MySQL - driver package
mysql-connector-odbc-debuginfo.x86_64 : Debug information for package mysql-connector-odbc
mysql-connector-odbc-setup.x86_64 : An ODBC 5.3 driver for MySQL - setup library
mysql-connector-python.noarch : Standardized MySQL database driver for Python
mysql-connector-python.x86_64 : Standardized MySQL database driver for Python
mysql-connector-python-cext.x86_64 : Standardized MySQL driver for Python with C Extension
mysql-connector-python-debuginfo.x86_64 : Debug information for package mysql-connector-python
mysql-mmm.noarch : Multi-Master Replication Manager for MySQL
mysql-proxy.x86_64 : A proxy for the MySQL Client/Server protocol
mysql-proxy-devel.x86_64 : Development files for mysql-proxy
mysql-ref-manual-5.6-en-html-chapter.noarch : The MySQL Reference Manual (HTML, English)
mysql-ref-manual-5.6-en-pdf.noarch : The MySQL Reference Manual (PDF, English)
mysql-router.x86_64 : MySQL Router
mysql-router-debuginfo.x86_64 : Debug information for package mysql-router
mysql-shell.x86_64 : Command line shell and scripting environment for MySQL
mysql-shell-debuginfo.x86_64 : Debug information for package mysql-shell
mysql-utilities.noarch : Collection of utilities used for maintaining and administering MySQL servers
mysql-utilities-extra.noarch : Additional files for mysql-utilities
mysql-workbench-community.x86_64 : A MySQL visual database modeling, administration, development and migration tool
mysql-workbench-community-debuginfo.x86_64 : Debug information for package mysql-workbench-community
mysqlreport.noarch : A friendly report of important MySQL status values
mysqltuner.noarch : MySQL configuration assistant
nagios-plugins-mysql.x86_64 : Nagios Plugin - check_mysql
nextcloud-mysql.noarch : MySQL database support for NextCloud
nodejs-mysql.noarch : A node.js driver for mysql
opendbx-mysql.x86_64 : MySQL backend - provides mysql support in opendbx
opensips-mysql.x86_64 : MySQL storage support for the OpenSIPS
owncloud-mysql.noarch : MySQL database support for ownCloud
pcp-pmda-mysql.x86_64 : Performance Co-Pilot (PCP) metrics for MySQL
pdns-backend-mysql.x86_64 : MySQL backend for pdns
perdition-mysql.x86_64 : Library to allow perdition to access MySQL based pop maps
perl-DBD-MySQL.x86_64 : A MySQL interface for Perl
perl-DateTime-Format-MySQL.noarch : Parse and format MySQL dates and times
perl-Test-mysqld.noarch : Mysqld runner for tests
php-ZendFramework-Db-Adapter-Mysqli.noarch : Zend Framework database adapter for mysqli
php-ZendFramework-Db-Adapter-Pdo-Mysql.noarch : Zend Framework database adapter for MySQL PDO
php-mysql.x86_64 : A module for PHP applications that use MySQL databases
php-mysqlnd.x86_64 : A module for PHP applications that use MySQL databases
php-pear-MDB2-Driver-mysql.noarch : MySQL MDB2 driver
php-pear-MDB2-Driver-mysqli.noarch : MySQL Improved MDB2 driver
php55w-mysql.x86_64 : A module for PHP applications that use MySQL databases
php55w-mysqlnd.x86_64 : A module for PHP applications that use MySQL databases
php56w-mysql.x86_64 : A module for PHP applications that use MySQL databases
php56w-mysqlnd.x86_64 : A module for PHP applications that use MySQL databases
php70w-mysql.x86_64 : A module for PHP applications that use MySQL databases
php70w-mysqlnd.x86_64 : A module for PHP applications that use MySQL databases
php71w-mysql.x86_64 : A module for PHP applications that use MySQL databases
php71w-mysqlnd.x86_64 : A module for PHP applications that use MySQL databases
poco-mysql.x86_64 : The Data/MySQL POCO component
preludedb-mysql.x86_64 : Plugin to use prelude with a MySQL database
proftpd-mysql.x86_64 : Module to add MySQL support to the ProFTPD FTP server
qt-mysql.i686 : MySQL driver for Qt's SQL classes
qt-mysql.x86_64 : MySQL driver for Qt's SQL classes
qt3-MySQL.i686 : MySQL drivers for Qt 3's SQL classes
qt3-MySQL.x86_64 : MySQL drivers for Qt 3's SQL classes
qt5-qtbase-mysql.x86_64 : MySQL driver for Qt5's SQL classes
qt5-qtbase-mysql.i686 : MySQL driver for Qt5's SQL classes
redland-mysql.x86_64 : MySQL storage support for Redland
root-sql-mysql.x86_64 : MySQL client plugin for ROOT
rsyslog-mysql.x86_64 : MySQL support for rsyslog
soci-mysql.x86_64 : MySQL back-end for soci
soci-mysql-devel.x86_64 : MySQL back-end for soci
tcl-mysqltcl.x86_64 : MySQL interface for Tcl
voms-mysql-plugin.x86_64 : VOMS server plugin for MySQL
zabbix20-proxy-mysql.x86_64 : Zabbix proxy compiled to use MySQL
zabbix20-server-mysql.x86_64 : Zabbix server compiled to use MySQL
zabbix20-web-mysql.noarch : Zabbix web frontend for MySQL
zabbix22-proxy-mysql.x86_64 : Zabbix proxy compiled to use MySQL
zabbix22-server-mysql.x86_64 : Zabbix server compiled to use MySQL
zabbix22-web-mysql.noarch : Zabbix web frontend for MySQL
calligra-kexi-driver-mysql.x86_64 : Mysqwl driver for kexi
dmlite-dpmhead.x86_64 : EMI DPM Head Node (MySQL)
dmlite-dpmhead-dome.x86_64 : DPM Head Node (MySQL)
innotop.noarch : A MySQL and InnoDB monitor program
mariadb.x86_64 : A community developed branch of MySQL
mariadb-devel.i686 : Files for development of MariaDB/MySQL applications
mariadb-devel.x86_64 : Files for development of MariaDB/MySQL applications
mariadb-libs.i686 : The shared libraries required for MariaDB/MySQL clients
mariadb-libs.x86_64 : The shared libraries required for MariaDB/MySQL clients
mylvmbackup.noarch : Utility for creating MySQL backups via LVM snapshots
mysql-community-server.x86_64 : A very fast and reliable SQL database server
mysql-mmm-agent.noarch : MMM Database Server Agent Daemon and Libraries
mysql-mmm-monitor.noarch : MMM Monitor Server Daemon and Libraries
mysql-mmm-tools.noarch : MMM Control Scripts and Libraries
mytop.noarch : A top clone for MySQL
percona-xtrabackup.x86_64 : Online backup for InnoDB/XtraDB in MySQL, Percona Server and MariaDB
php-phpmyadmin-sql-parser.noarch : A validating SQL lexer and parser with a focus on MySQL dialect
php-udan11-sql-parser.noarch : A validating SQL lexer and parser with a focus on MySQL dialect
phpMyAdmin.noarch : Handle the administration of MySQL over the World Wide Web
zabbix22-dbfiles-mysql.noarch : Zabbix database schemas, images, data and patches

  Name and summary matches only, use "search all" for everything.
yum install mysql-community-server.x86_64
#我们需要安装的是这个包 mysql5.6
#启动服务
systemctl start mysqld
#开机启动
systemctl enable mysqld
#这样安装 默认是没有密码的
#我们需要设置mysql的密码
#输入:(进入mysql命令界面)
mysql
mysql>use mysql;
mysql>UPDATE user SET Password = PASSWORD('你的密码') WHERE user = 'root';
mysql>exit;
systemctl restart mysqld
#重启mysql

非必要安装

软件包 git,composer,redis
这些软件包 都在yum中都有
所以可以直接安装

yum install redis
#安装redis
systemctl start  redis.service
#启动redis
systemctl enable redis.service
#开机启动

yum install git
#安装git
yum install composer
#安装composer

安装完成 接下来配置一下环境

composer

#首先切换composer的源(可不切换,建议切换,不言会很慢)
composer config -g repo.packagist composer https://packagist.phpcomposer.com

nginx

vim /etc/nginx/nginx.conf
#删除掉没有注释的
server {

}
#这段代码
#不会用vim的 建议使用ftp连接用文本编辑工具打开删除
cd /etc/nginx/conf.d
touch xxx.conf
vim xxx.conf
#输入以下配置
server {
    listen      80;
    root /var/www/项目;
    server_name 域名;
    charset     utf-8;
    index index.php index.html;
    location / {
        try_files $uri /index.php?s=$uri&$args;
    }
    location ~ .php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}
systemctl restart nginx
#重启nginx

项目地址

由以上nginx配置可见
项目是放在/var/www/下
以下已laravel为例

#laravel下的storage目录需要读写权限
chmod -R 777 /var/www/laravel/storage

这样就能正常访问了

关于git

如果没有ssh key可自行百度git生成ssk key的教程这里不作详细介绍

如果已经在别的电脑上已经有ssh key 想公用这个key
只需要把key复制到 ~/.ssh/下即可
此时还需要重新定义这个目录的权限

chmod -R 600 ~/.ssh

如果对您有帮助,您又乐意的话,请多多支持!