百科
LNMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写。L指Linux,N指Nginx,M一般指MySQL,也可以指MariaDB,P一般指PHP,也可以指Perl或Python。
环境介绍
电脑为Windows10,使用vmworkstation创建虚拟机,搭建Linux系统。使用镜像为centos7
安装nginx
1、更新系统软件
[root@localhost ~]# yum update
2、安装C环境
[root@localhost ~]# yum -y install gcc gcc-c++
3、安装nginx依赖
[root@localhost ~]# yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel
4、下载nginx包并编译安装
[root@localhost ~]# wget http://nginx.org/download/nginx-1.12.2.tar.gz
[root@localhost ~]# tar -zxf nginx-1.12.2.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx
[root@localhost nginx-1.12.2]# make && make install
5、为nginx创建用户
[root@localhost nginx-1.12.2]# groupadd nginx
[root@localhost nginx-1.12.2]# useradd -M -g nginx -s /sbin/nologin nginx
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/conf/
[root@localhost conf]# vim nginx.conf
所更改的配置文件配置为:
user nginx nginx;
6、配置nginx系统服务
[root@localhost conf]# vim /lib/systemd/system/nginx.service
nginx.service的文件内容为:
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
6、启动nginx并编辑为开机自启
[root@localhost conf]# systemctl start nginx
[root@localhost conf]# systemctl enable nginx
至此 nginx安装完毕
安装MySQL
我使用的MySQL为5.7.32,通用版。各位同学可以自己到MySQL官网或者淘宝下载。
1、下载并解压包到安装的位子
[root@localhost ~]# tar -zxf mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz
[root@localhost ~]# mv mysql-5.7.32-linux-glibc2.12-x86_64 /usr/local/mysql
[root@localhost ~]# cd /usr/local/mysql/
2、创建目录用户组并初始化
[root@localhost mysql]# mkdir -p /data/mysql
[root@localhost mysql]# groupadd mysql
[root@localhost mysql]# useradd -M -g mysql -s /sbin/nologin nginx
[root@localhost mysql]# chown -R mysql:mysql /usr/local/mysql
[root@localhost mysql]# chown -R mysql:mysql /data/mysql/
[root@localhost mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
[root@localhost mysql]# bin/mysql_ssl_rsa_setup --datadir=/data/mysql
3、配置并启动mysql
[root@localhost mysql]# cp -r support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql]# vim /etc/init.d/mysqld
[root@localhost mysql]# vim /etc/profile
[root@localhost mysql]# source /etc/profile
[root@localhost mysql]# rm -rf /etc/my.cnf
[root@localhost mysql]# /etc/init.d/mysqld start
mysqld中更改为:
datadir=/data/mysql
basedir=/usr/local/mysql
profile更改为:
export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/sbin
4、登录MySQL 并修改初始密码
[root@localhost mysql]# mysql -u root -p
mysql> set password=password('root');
mysql>grant all privileges on *.* to 'root'@'%' identified by 'root';
mysql>flush privileges;
5、说明一下
至此 MySQL安装完成~
值得说的是在MySQL5.7之后,support文件中就不提供my_defult.cnf文件了。没有的同学可以直接touch一个 需要哪里用哪里
要是感觉MySQL这样安太麻烦 可以直接yum安装 菜鸟上有教程
要是有时间也可以编译安装,我嫌编译安装太浪费时间,主要是电脑性能不支持,就直接搞了一个通用包。
安装PHP
1、安装PHP依赖环境
[root@localhost mysql]# yum -y install elrepo-release.noarch
[root@localhost mysql]# yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel
2、下载编译安装php
[root@localhost ~]# tar -zxf php-5.3.28.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/php-5.3.28/
[root@localhost php-5.3.28]# ./configure --prefix=/usr/local/php --disable-fileinfo --enable-fpm --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-openssl --with-zlib --with-curl --enable-ftp --with-gd --with-xmlrpc --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-gd-native-ttf --enable-mbstring --with-mcrypt --enable-zip --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --without-pear --enable-bcmath
[root@localhost php-5.3.28]# make && make install
3、配置启动PHP
[root@localhost php-5.3.28]# cp php.ini-development /etc/php.ini
[root@localhost php-5.3.28]# groupadd zh-php
[root@localhost php-5.3.28]# useradd -M -g zh-php -s /sbin/nologin zh-php
[root@localhost php-5.3.28]# cd /usr/local/php/etc/
[root@localhost etc]# ls
php-fpm.conf.default
[root@localhost etc]# cp php-fpm.conf.default php-fpm.conf
[root@localhost etc]# vim php-fpm.conf
[root@localhost etc]# cd ../sbin/
[root@localhost sbin]# ls
php-fpm
[root@localhost sbin]# ./php-fpm
至此 PHP安装完成
配置nginx连接PHP
location / {
root html;
index index.php index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /document_root$fastcgi_script_name;
include fastcgi_params;
}
此致/敬礼
lnmp搭建完成
各位小伙伴哪里报错可以给我留言了 看到了一般都会回 专业采坑