手动安装LNMP运行环境

AKHYui2020-06-22 13:45:02Linux
# LNMP安装

安装Nginx

下载Nginx预编译包

https://nginx.org/en/download.htmlopen in new window

解压压缩包


[root@Hyui-VM ~]# mkdir nginx 

[root@Hyui-VM ~]# mv nginx-1.19.0.tar.gz ./nginx

[root@Hyui-VM ~]# cd nginx/

[root@Hyui-VM nginx]# tar -zxvf nginx-1.19.0.tar.gz

[root@Hyui-VM nginx]# ls

nginx-1.19.0  nginx-1.19.0.tar.gz

[root@Hyui-VM nginx]# cd nginx-1.19.0

[root@Hyui-VM nginx-1.19.0]# ls

auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src

安装依赖软件


yum install gcc gcc-c++ cyrus-sasl-md5 openssl openssl-devel -y

编译安装


# 将ssl模块也编译入nginx

./configure --prefix=/usr/local/nginx/ --with-http_ssl_module

make && make install

运行Nginx


./sbin/nginx

ps -aux | grep nginx

root     17033  0.0  0.0  45976  1120 ?        Ss   16:30   0:00 nginx: master process ./sbin/nginx

nobody   17034  0.0  0.0  46416  2112 ?        S    16:30   0:00 nginx: worker process

root     17060  0.0  0.0 112812   972 pts/1    S+   16:33   0:00 grep --color=auto nginx

将Nginx放入systemd


vim /usr/lib/systemd/system/nginx.service

[Unit]

Description=Nginx # 描述Nginx信息

Documentation=http://nginx.org/ 

After=network.target remote-fs.target nss-lookup.target # 表明依赖服务



[Service]

Type=forking # 启动时关系的定义

PIDFile=/usr/local/nginx/logs/nginx.pid # PID文件

ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf # 启动Nginx使用的命令

ExecReload=/bin/kill -s HUP $MAINPID # 重启Nginx命令

ExecStop=/bin/kill -s QUIT $MAINPID # 停止Nginx命令

PrivateTmp=true # 给服务分配独立的临时空间



[Install]

WantedBy=multi-user.target

安装PHP

下载预编译包

https://www.php.net/downloadsopen in new window

添加www用户


groupadd www

useradd -s /sbin/nologin -g www -M www

解压预编译包


mkdir php

mv php-7.2.31.tar.gz php

tar -zxvf php-7.2.31.tar.gz

安装依赖软件


yum install gcc autoconf gcc-c++ libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel readline readline-devel libxslt libxslt-devel systemd-devel openjpeg-devel

开始编译


mkdir /usr/local/php7.2

./configure --prefix=/usr/local/php7.2 --with-config-file-path=/usr/local/php7.2/etc --with-zlib-dir --with-freetype-dir --enable-mbstring --with-libxml-dir=/usr --enable-xmlreader --enable-xmlwriter --enable-soap --enable-calendar --with-curl --with-zlib --with-gd --with-pdo-sqlite --with-pdo-mysql --with-mysqli --with-mysql-sock --enable-mysqlnd --disable-rpath --enable-inline-optimization --with-bz2 --with-zlib --enable-sockets --enable-sysvsem --enable-sysvshm --enable-pcntl --enable-mbregex --enable-exif --enable-bcmath --with-mhash --enable-zip --with-pcre-regex --with-jpeg-dir=/usr --with-png-dir=/usr --with-openssl --enable-ftp --with-kerberos --with-gettext --with-xmlrpc --with-xsl --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-fpm-systemd --disable-fileinfo

make && make install

配置文件


cd /usr/local/php7.2/etc

mv php-fpm.conf.default php-fpm.conf

# 编辑配置文件

vim php-fpm.conf

# 将pid = run/php-fpm.pid前的“;”注释删除

cd /usr/local/php7.2/etc/php-fpm.d

mv www.conf.default www.conf

vim www.conf

# 修改其中的user和group项user = www,group = www

运行php-fpm


/usr/local/php7.2/sbin/php-fpm

# 检查启动状态

ps -aux | grep php

修改Nginx配置文件


vim /usr/local/nginx/conf/nginx.conf


# 修改user项为www

user  www;


# 去掉以下location的注释 并将fastcgi_param项中的/scripts修改为$document_root

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;

        }



安装MySQL

下载MySQL的rpm包


wget http://repo.mysql.com/mysql57-community-release-el7-11.noarch.rpm

# 安装MySQL5.7的rpm包

rpm -ivh mysql57-community-release-el7-11.noarch.rpm

# 更新源

yum update -y 

# 安装mysql

yum install mysql-server -y

启动MySQL


systemctl start mysqld  # 开启

systemctl restart mysqld  # 重启

systemctl stop mysqld  # 关闭

登录MySQL


cat /var/log/mysqld.log | grep password

mysql -u root -p  # 会要求输入root密码

修改root密码


alter user 'root'@'localhost' identified by 'Aa123456.';  #密码最好要有大小写和符号

设置有密码外部访问


use mysql;  # 选择mysql数据库

update user set host='%' where User='root';  # 修改user表中User="root"的行中的host值为"%"

flush privileges;  # 刷新

Last Updated 9/17/2025, 7:13:55 AM