欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ubuntu 20.04上搭建LNMP环境的方法步骤

程序员文章站 2022-03-15 08:27:57
简单说明由于之前是用centos7搭建的,后来使用ubuntu 20.04的系统做为个人开发环境,所以想在ubuntu上也搭建一下环境,和centos有一些小区别所以记录一下仅供学习。安装前准备下载软...

简单说明

由于之前是用centos7搭建的,后来使用ubuntu 20.04的系统做为个人开发环境,所以想在ubuntu上也搭建一下环境,和centos有一些小区别所以记录一下仅供学习。

安装前准备

下载软件:
php:7.3.18
nginx:1.18.0
mariadb:10.5.4

解压文件:

tar zxf php-7.3.18.tar.gz
tar zxf mariadb-10.5.4.tar.gz
tar zxf nginx-1.18.0.tar.gz

ubuntu 20.04上搭建LNMP环境的方法步骤

安装nginx

sudo groupadd -r nginx && sudo useradd -r -g nginx -s /sbin/nologin -d /usr/local/nginx nginx
sudo apt install -y libpcre3-dev zlib1g-dev
cd /home/allen/下载/nginx-1.18.0
./configure --user=nginx --group=nginx
make -j 4 && sudo make install
sudo /usr/local/nginx/sbin/nginx -t
#nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
#nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
sudo mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.back
sudo vim /usr/local/nginx/conf/nginx.conf

user nginx;
worker_processes 4;
events {
  worker_connections 1024;
}
http {
  include    mime.types;
  default_type application/octet-stream;
  sendfile   on;
  keepalive_timeout 65;
  log_format main '$remote_addr || $remote_user || $time_local || $request || $status || $body_bytes_sent || $http_referer || $http_user_agent || $http_x_forwarded_for';
  include /data/www/*/*.conf;
} 

sudo chown -r nginx:nginx /usr/local/nginx

sudo vim /lib/systemd/system/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 quit
privatetmp=true

[install]
wantedby=multi-user.target


sudo systemctl enable nginx

#created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /lib/systemd/system/nginx.service.

reboot

重启后看看有没有启动成功

ubuntu 20.04上搭建LNMP环境的方法步骤

安装mariadb

sudo rm -rf /etc/mysql
sudo apt remove -y mysql-common
sudo apt autoremove -y
sudo apt install -y cmake libncurses5-dev libgnutls28-dev
sudo groupadd -r mysql && sudo useradd -r -g mysql -s /sbin/nologin -d /usr/local/mariadb mysql
sudo mkdir -p /data/db /var/log/mariadb
cd /home/allen/下载/mariadb-10.5.4/
cmake . -dcmake_install_prefix=/usr/local/mariadb -dmysql_datadir=/data/db -dsysconfdir=/etc -dwithout_tokudb=1 -dmysql_unix_addr=/tmp/mysql.sock -ddefault_charset=utf8mb4 -ddefault_collation=utf8mb4_general_ci
make -j 4 && sudo make install
sudo /usr/local/mariadb/scripts/mysql_install_db --user=mysql --datadir=/data/db
sudo vim /etc/my.cnf

[mysqld]
datadir = /data/db
socket = /tmp/mysql.sock
# 建议禁用符号链接,防止各类安全风险
symbolic-links = 0
collation-server = utf8mb4_general_ci
init-connect = 'set names utf8mb4'
character-set-server = utf8mb4

[mysql]
default-character-set = utf8mb4

[client]
port = 3306
socket = /tmp/mysql.sock
default-character-set = utf8mb4

[mysqld_safe]
log-error = /var/log/mariadb/mariadb.log
pid-file = /var/run/mariadb/mariadb.pid

sudo cp /usr/local/mariadb/support-files/mysql.server /etc/init.d/mariadb

sudo vim /etc/profile.d/mariadb.sh
export path=$path:/usr/local/mariadb/bin/

sudo chmod 0777 /etc/profile.d/mariadb.sh
source /etc/profile.d/mariadb.sh

sudo /etc/init.d/mariadb start
#starting mariadb (via systemctl): mariadb.service.

sudo /usr/local/mariadb/bin/mysql_secure_installation 	 

#note: running all parts of this script is recommended for all mariadb
#   servers in production use! please read each step carefully!

#in order to log into mariadb to secure it, we'll need the current
#password for the root user. if you've just installed mariadb, and
#haven't set the root password yet, you should just press enter here.

#enter current password for root (enter for none): 
#ok, successfully used password, moving on...

#setting the root password or using the unix_socket ensures that nobody
#can log into the mariadb root user without the proper authorisation.

#you already have your root account protected, so you can safely answer 'n'.

#switch to unix_socket authentication [y/n] y
#enabled successfully!
#reloading privilege tables..
# ... success!


#you already have your root account protected, so you can safely answer 'n'.

#change the root password? [y/n] y
#new password: 
#re-enter new password: 
#password updated successfully!
#reloading privilege tables..
# ... success!


#by default, a mariadb installation has an anonymous user, allowing anyone
#to log into mariadb without having to have a user account created for
#them. this is intended only for testing, and to make the installation
#go a bit smoother. you should remove them before moving into a
#production environment.

#remove anonymous users? [y/n] y
# ... success!

#normally, root should only be allowed to connect from 'localhost'. this
#ensures that someone cannot guess at the root password from the network.

#disallow root login remotely? [y/n] n
# ... skipping.

#by default, mariadb comes with a database named 'test' that anyone can
#access. this is also intended only for testing, and should be removed
#before moving into a production environment.

#remove test database and access to it? [y/n] y
# - dropping test database...
# ... success!
# - removing privileges on test database...
# ... success!

#reloading the privilege tables will ensure that all changes made so far
#will take effect immediately.

#reload privilege tables now? [y/n] y
# ... success!

#cleaning up...

#all done! if you've completed all of the above steps, your mariadb
#installation should now be secure.

#thanks for using mariadb!

sudo systemctl enable mariadb 

sudo chown mysql:mysql -r /usr/local/mariadb /data/db /var/log/mariadb

reboot

重启后看看有没有启动成功

ubuntu 20.04上搭建LNMP环境的方法步骤

安装php

sudo apt install -y libxml2-dev libssl-dev libbz2-dev libcurl4-gnutls-dev libjpeg-dev libpng-dev pkg-config libxslt1-dev libzip-dev libfreetype6-dev libfontconfig1-dev autoconf
sudo groupadd -r php && sudo useradd -r -g php -s /sbin/nologin -d /usr/local/php php
sudo vim /etc/sudoers
php   all=(all:all) all
cd /home/allen/下载/php-7.3.18/


./configure --prefix=/usr/local/php \--exec-prefix=/usr/local/php --with-fpm-user=php --with-fpm-group=php --enable-zip --with-curl --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-mysqli --with-openssl --with-pdo-mysql --with-pdo-sqlite --with-pear --with-xmlrpc --with-xsl --with-zlib --with-bz2 --with-mhash --enable-bcmath --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm --enable-xml --enable-fpm --with-freetype-dir --with-gd --with-libxml-dir --with-pcre-regex --enable-libxml --enable-zip --with-png-dir --with-jpeg-dir

有一个错误:

configure: error: freetype-config not found.

解决办法:
据说:

这是由于在 ubuntu 19.04 中 apt-get 安装的 libfreetype6-dev 版本为 2.9.1-3
http://changelogs.ubuntu.com/changelogs/pool/main/f/freetype/freetype_2.9.1-3/changelog
在 changelog 中写到:

the `freetype-config' script is no longer installed by default
(closes: #871470, #886461). all packages depending on libfreetype6-dev
should use pkg-config to find the relevant cflags and libraries.
freetype-config 被替代成 pkg-config ,新版本使用 pkg-config 管理 cflags 和 库。

所以解决方法如下:
主要的思路就是用pkg-config代替freetype-config。安装pkg-config,我在上面已经安装了

cat ./configure | grep "freetype-config" -n
34847:   if test -f "$i/bin/freetype-config"; then
34849:    freetype2_config="$i/bin/freetype-config"
34855:   as_fn_error $? "freetype-config not found." "$lineno" 5
36568:   if test -f "$i/bin/freetype-config"; then
36570:    freetype2_config="$i/bin/freetype-config"
36576:   as_fn_error $? "freetype-config not found." "$lineno" 5

sed -i "s/freetype-config/pkg-config/g" ./configure
cat ./configure | grep "freetype2_config --cflags" -n
34858:  freetype2_cflags=`$freetype2_config --cflags`
36579:  freetype2_cflags=`$freetype2_config --cflags`

sed -i "s/freetype2_config --cflags/freetype2_config freetype2 --cflags/g" ./configure
cat ./configure | grep "freetype2_config --libs" -n 
34859:  freetype2_libs=`$freetype2_config --libs`
36580:  freetype2_libs=`$freetype2_config --libs`

sed -i "s/freetype2_config --libs/freetype2_config freetype2 --libs/g" ./configure
cat ./ext/gd/config.m4 | grep "freetype-config" -n
188:   if test -f "$i/bin/freetype-config"; then
190:    freetype2_config="$i/bin/freetype-config"
196:   ac_msg_error([freetype-config not found.])

sed -i "s/freetype-config/pkg-config/g" ./ext/gd/config.m4

重新编译安装就ok

thank you for using php.

make -j 4 && sudo make install

sudo cp php.ini-production /usr/local/php/lib/php.ini

sudo cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

sudo cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

vim /usr/local/php/etc/php-fpm.d/www.conf
listen.mode = 0666
pm.max_children = 128
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 10000
slowlog = log/$pool.log.slow
rlimit_files = 1024

sudo vim /etc/profile.d/php.sh
export path=$path:/usr/local/php/bin/

sudo chmod 0777 /etc/profile.d/php.sh && source /etc/profile.d/php.sh

sudo cp sapi/fpm/php-fpm.service /etc/systemd/system/php.service

安装xdebug

下载xdebug

cd /home/allen/下载
tar zxf xdebug-2.9.6.tgz
cd xdebug-2.9.6
phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make -j 4 && sudo make install

安装apcu

下载apcu http://pecl.php.net/get/apcu-5.1.18.tgz

cd /home/allen/下载
tar zxf apcu-5.1.18.tgz
cd apcu-5.1.18
phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make -j 4 && sudo make install

编辑php.ini

sudo 
vim /usr/local/php/lib/php.ini

date.timezone = asia/shanghai
expose_php = off
max_execution_time = 0
memory_limit = 4096m
display_errors = on
cgi.fix_pathinfo=0
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20180731/"
extension=pgsql
extension=apcu

zend_extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20180731/xdebug.so
[xdebug]
xdebug.var_display_max_children=10240
xdebug.var_display_max_data=20480
xdebug.var_display_max_depth=50

启动

sudo systemctl enable php-fpm

sudo chown -r mysql:mysql /usr/local/mariadb
sudo chown -r nginx:nginx /usr/local/nginx
sudo chown -r php:php /usr/local/php
reboot 

ubuntu 20.04上搭建LNMP环境的方法步骤

到此这篇关于ubuntu 20.04上搭建lnmp环境的方法步骤的文章就介绍到这了,更多相关ubuntu 20.04搭建lnmp内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!