Code Optimization

Interesting things in software development and code optimization

NopCommerce customization - Message Token

Hello friends,


this time I will show how to add your own message tokens to NopCommerce. For that you need two things: create a message token class and register it with dependencyregistrar.

So here is the code:

    public class FeederOT : IDependencyRegistrar
    {
        public int Order => 1000;

        public void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config)
        {
            builder.RegisterType<OrderMessageToken>().As<IMessageTokenProvider>().InstancePerLifetimeScope();
        }
    }

    public class OrderMessageToken: Nop.Services.Messages.MessageTokenProvider
    {
        #region Fields

        private readonly CatalogSettings _catalogSettings;
        private readonly CurrencySettings _currencySettings;
        private readonly IActionContextAccessor _actionContextAccessor;
        private readonly IAddressAttributeFormatter _addressAttributeFormatter;
        private readonly ICurrencyService _currencyService;
        private readonly ICustomerAttributeFormatter _customerAttributeFormatter;
        private readonly ICustomerService _customerService;
        private readonly IDateTimeHelper _dateTimeHelper;
        private readonly IDownloadService _downloadService;
        private readonly IEventPublisher _eventPublisher;
        private readonly IGenericAttributeService _genericAttributeService;
        private readonly ILanguageService _languageService;
        private readonly ILocalizationService _localizationService;
        private readonly IOrderService _orderService;
        private readonly IPaymentService _paymentService;
        private readonly IPriceFormatter _priceFormatter;
        private readonly IStoreContext _storeContext;
        private readonly IStoreService _storeService;
        private readonly IUrlHelperFactory _urlHelperFactory;
        private readonly IUrlRecordService _urlRecordService;
        private readonly IVendorAttributeFormatter _vendorAttributeFormatter;
        private readonly IWorkContext _workContext;
        private readonly MessageTemplatesSettings _templatesSettings;
        private readonly PaymentSettings _paymentSettings;
        private readonly StoreInformationSettings _storeInformationSettings;
        private readonly TaxSettings _taxSettings;

        private Dictionary<string, IEnumerable<string>> _allowedTokens;

        #endregion

        #region Ctor

        public OrderMessageToken(CatalogSettings catalogSettings,
            CurrencySettings currencySettings,
            IActionContextAccessor actionContextAccessor,
            IAddressAttributeFormatter addressAttributeFormatter,
            ICurrencyService currencyService,
            ICustomerAttributeFormatter customerAttributeFormatter,
            ICustomerService customerService,
            IDateTimeHelper dateTimeHelper,
            IDownloadService downloadService,
            IEventPublisher eventPublisher,
            IGenericAttributeService genericAttributeService,
            ILanguageService languageService,
            ILocalizationService localizationService,
            IOrderService orderService,
            IPaymentService paymentService,
            IPriceFormatter priceFormatter,
            IStoreContext storeContext,
            IStoreService storeService,
            IUrlHelperFactory urlHelperFactory,
            IUrlRecordService urlRecordService,
            IVendorAttributeFormatter vendorAttributeFormatter,
            IWorkContext workContext,
            MessageTemplatesSettings templatesSettings,
            PaymentSettings paymentSettings,
            StoreInformationSettings storeInformationSettings,
            TaxSettings taxSettings):base(
                catalogSettings,
            currencySettings,
            actionContextAccessor,
            addressAttributeFormatter,
            currencyService,
            customerAttributeFormatter,
            customerService,
            dateTimeHelper,
            downloadService,
            eventPublisher,
            genericAttributeService,
            languageService,
            localizationService,
            orderService,
            paymentService,
            priceFormatter,
            storeContext,
            storeService,
            urlHelperFactory,
            urlRecordService,
            vendorAttributeFormatter,
            workContext,
            templatesSettings,
            paymentSettings,
            storeInformationSettings,
            taxSettings
                )
        {
            this._catalogSettings = catalogSettings;
            this._currencySettings = currencySettings;
            this._actionContextAccessor = actionContextAccessor;
            this._addressAttributeFormatter = addressAttributeFormatter;
            this._currencyService = currencyService;
            this._customerAttributeFormatter = customerAttributeFormatter;
            this._customerService = customerService;
            this._dateTimeHelper = dateTimeHelper;
            this._downloadService = downloadService;
            this._eventPublisher = eventPublisher;
            this._genericAttributeService = genericAttributeService;
            this._languageService = languageService;
            this._localizationService = localizationService;
            this._orderService = orderService;
            this._paymentService = paymentService;
            this._priceFormatter = priceFormatter;
            this._storeContext = storeContext;
            this._storeService = storeService;
            this._urlHelperFactory = urlHelperFactory;
            this._urlRecordService = urlRecordService;
            this._vendorAttributeFormatter = vendorAttributeFormatter;
            this._workContext = workContext;
            this._templatesSettings = templatesSettings;
            this._paymentSettings = paymentSettings;
            this._storeInformationSettings = storeInformationSettings;
            this._taxSettings = taxSettings;
        }

        #endregion

        public override void AddOrderTokens(IList<Token> tokens, Order order, int languageId, int vendorId = 0)
        {
            tokens.Add(new Token("Order.OrderGuid", order.OrderGuid, true));
            base.AddOrderTokens(tokens, order, languageId, vendorId);
        }
    }


So, you create your own class, inherit it from the Nop.Services.Messages.MessageTokenProvider and define your own token inside of the AddOrderTokens method.

After that you register it with the IDependencyRegistrar.


Now you can use your own tokens inside of message templates.


See you there: NopCommerce customization - Price Calculation Service


1vqHSTrq1GEoEF7QsL8dhmJfRMDVxhv2y



NopCommerce customization - Plugin

Hello friends,


I'm going to share my experience in NopCommerce customization. I will write about Plugins, Scheduled tasks, Events, Services, and everything you need to know to extend your NopCommerce shop.

This first post will be about NopCommerce plugin for NopCommerce 4.1.

So, to start writing your own plugin for NopCommerce you need to start from the help page of the official documentation

Steps described there are required to start writing your plugin. After that you will just extend it to meet your requirements and here are some useful things:

- Main Plugin class declaration:

public class Feeder : BasePlugin, IMiscPlugin, IAdminMenuPlugin

- Declare all classes you need for work:

public Feeder(IActionContextAccessor actionContextAccessor,
            IDiscountService discountService,
            ILocalizationService localizationService,
            ISettingService settingService,
            IUrlHelperFactory urlHelperFactory,
            IWebHelper webHelper,
            IScheduleTaskService scheduleTaskService)
        {
            this._actionContextAccessor = actionContextAccessor;
            this._discountService = discountService;
            this._localizationService = localizationService;
            this._settingService = settingService;
            this._urlHelperFactory = urlHelperFactory;
            this._webHelper = webHelper;

            this._scheduleTaskService = scheduleTaskService;
        }

- Declare base methods:

        public override string GetConfigurationPageUrl()
        {
            return $"{_webHelper.GetStoreLocation()}Admin/ProductFeederMPlug/Configure";
        }

        public string GetConfigurationUrl(int discountId, int? discountRequirementId)
        {
            return $"{_webHelper.GetStoreLocation()}Admin/ProductFeederMPlug/Configure";
}

- Adding menu item to the admin menu:

        public void ManageSiteMap(SiteMapNode rootNode)
        {
            var menuItem = new SiteMapNode()
            {
                SystemName = "Product Feeder MPlug",
                Title = "Product Feeder MPlug",
ControllerName = "ProductFeederMPlug",
ActionName = "Setup", Visible = true, IconClass = "fa fa-dot-circle-o", RouteValues = new RouteValueDictionary() { { "area", Web.Framework.AreaNames.Admin } }, }; var pluginNode = rootNode.ChildNodes.FirstOrDefault(x => x.SystemName == "Configuration"); if (pluginNode != null) pluginNode.ChildNodes.Add(menuItem); else rootNode.ChildNodes.Add(menuItem); }

-Installation method:

        public override void Install()
        {
            var task = _scheduleTaskService.GetTaskByType(Services.UpdateStoreTask.TypeName);
            if (task == null)
            {
                _scheduleTaskService.InsertTask(new Core.Domain.Tasks.ScheduleTask()
                {
                    Type = Services.UpdateStoreTask.TypeName,
                    Enabled = true,
                    Name = "MPlug Product Synchronizer",
                    Seconds = 60 * 10,
                    StopOnError = false
                });
            }
            else
            {
                task.Enabled = true;
                task.Seconds = 60 * 10;
                task.StopOnError = false;
                _scheduleTaskService.UpdateTask(task);
            }

            task = _scheduleTaskService.GetTaskByType(Services.UpdateOrderStateTask.TypeName);
            if (task == null)
            {
                _scheduleTaskService.InsertTask(new Core.Domain.Tasks.ScheduleTask()
                {
                    Type = Services.UpdateOrderStateTask.TypeName,
                    Enabled = true,
                    Name = "MPlug Order State Tracker",
Seconds = 60 * 10, StopOnError = false }); } else { task.Enabled = true; task.Seconds = 60 * 15; task.StopOnError = false; _scheduleTaskService.UpdateTask(task); } base.Install(); }

- Uninstalling:

        public override void Uninstall()
        {
            //do whatever you need to uninstall your plugin
            base.Uninstall();
        }


- Adding your own controller - Inherit your plugin controller from the BasePluginController class:

public class ProductFeederMPlugController: BasePluginController

- Declaring views:

        [AuthorizeAdmin]
        [Area(AreaNames.Admin)]
        public IActionResult Configure()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.AccessAdminPanel))
                return AccessDeniedView();

            return View("~/Plugins/ProductFeeder.MPlug/Views/Configure.cshtml");
        }

        [AuthorizeAdmin]
        [Area(AreaNames.Admin)]
        public IActionResult Setup()
        {
            if (!_permissionService.Authorize(StandardPermissionProvider.AccessAdminPanel))
                return AccessDeniedView();

            return View("~/Plugins/ProductFeeder.MPlug/Views/Setup.cshtml");
}

- Store scope detecting:

var storeScope = _storeContext.ActiveStoreScopeConfiguration;

- Store temporary information during session:

_cache.Set<string>("Progress", progress, TimeSpan.FromMinutes(30));

- Declare all instances of classes you need as constructor parameters and store them into fields:

public ProductFeederController(ICustomerService customerService, 
            Nop.Services.Shipping.Date.IDateRangeService dateRangeService,
            ILocalizationService localizationService,
            IPermissionService permissionService,
            ISettingService settingService,
            IStoreContext storeContext,
            Nop.Services.Catalog.ICategoryService categoryService,
            Nop.Services.Catalog.IManufacturerService manufacturerService,
            Nop.Services.Catalog.IProductService productService,
            Nop.Services.Catalog.IProductAttributeService productAttributeService,
            Nop.Services.Catalog.ISpecificationAttributeService specificationAttributeService,
            IGenericAttributeService genericAttributeService,
            Core.Infrastructure.INopFileProvider fileProvider,
            Nop.Services.Media.IPictureService pictureService,
            Nop.Services.Stores.IStoreMappingService storeMappingService,
            Nop.Services.Catalog.IProductTagService productTagService,
            Nop.Services.Seo.IUrlRecordService urlRecordService,
            Microsoft.Extensions.Caching.Memory.IMemoryCache cache)
        {
            this._dateRangeService = dateRangeService;
            this._customerService = customerService;
            this._localizationService = localizationService;
            this._permissionService = permissionService;
            this._settingService = settingService;
            this._storeContext = storeContext;
            this._categoryService = categoryService;
            this._manufacturerService = manufacturerService;
            this._productService = productService;
            this._productAttributeService = productAttributeService;
            this._specificationAttributeService = specificationAttributeService;
            this._genericAttributeService = genericAttributeService;
            this._fileProvider = fileProvider;
            this._pictureService = pictureService;
            this._storeMappingService = storeMappingService;
            this._productTagService = productTagService;
            this._urlRecordService = urlRecordService;
            this._cache = cache;
        }

- Save needed data into settings (DB table):

_settingService.SetSetting<string>("Categories", Newtonsoft.Json.JsonConvert.SerializeObject(finalcategories, Newtonsoft.Json.Formatting.None,
                        new Newtonsoft.Json.JsonSerializerSettings()
                        {
                            ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
                        }), storeScope, true);

- To avoid complexity of existing model extending - use generic attributes:

_genericAttributeService.SaveAttribute<int>(manuf, Feeder.gAttrID, manuid);

- Add store mapping for entities (in this case for Manufacturer):

_storeMappingService.InsertStoreMapping<Core.Domain.Catalog.Manufacturer>(manuf, storeScope);

- Use repositories to speed-up it work with DB and if you do not need all other logic and events to be occured (use with caution and be aware of it):

var productRepository = Core.Infrastructure.EngineContext.Current.Resolve<Core.Data.IRepository<Core.Domain.Catalog.Product>>();

- Adding a manufacturer:

manuf = new Core.Domain.Catalog.Manufacturer()
{
     Name = product.brandName,
     CreatedOnUtc = DateTime.UtcNow,
     ManufacturerTemplateId = 1,
     MetaDescription = product.brandName,
     MetaKeywords = product.brandName,
     MetaTitle = product.brandName,
     PageSize = 20,
     Published = true,
     UpdatedOnUtc = DateTime.UtcNow
};
_manufacturerService.InsertManufacturer(manuf);
_storeMappingService.InsertStoreMapping<Core.Domain.Catalog.Manufacturer>(manuf, storeScope);

- Adding date ranges / delivery dates:

daterange = new Core.Domain.Shipping.DeliveryDate()
{
      Name = deliveryText
};
_dateRangeService.InsertDeliveryDate(daterange);

- Adding product:

pro = new Core.Domain.Catalog.Product()
{
     ProductType = Core.Domain.Catalog.ProductType.SimpleProduct,
     VisibleIndividually = true,
     Sku = product.article,
     IsShipEnabled = true,
     MetaDescription = shortd,
     MetaKeywords = shortd,
     MetaTitle = name,
     Price = price,
     ProductCost = cost,
     AdditionalShippingCharge = Feeder.CalculateShipping(price),
     CreatedOnUtc = DateTime.UtcNow,
     UpdatedOnUtc = ((DateTime)product.modified).ToUniversalTime(),
     Name = name,
     ShortDescription = product.descriptionSnort,
     FullDescription = product.description,
     BackorderMode = Core.Domain.Catalog.BackorderMode.NoBackorders,
     MarkAsNew = true,
     AllowBackInStockSubscriptions = true,
     AllowCustomerReviews = true,
     Published = price != null,
     DeliveryDateId = daterange == null ? 0 : daterange.Id,
     LimitedToStores = true,
     OrderMaximumQuantity=int.MaxValue,
     OrderMinimumQuantity=1, 
     DisableBuyButton = pprice == null,
     ProductTemplateId = 1,
     ProductManufacturers =
     {
           new Core.Domain.Catalog.ProductManufacturer() { Manufacturer = manuf, Product = pro }
     }

};

productRepository.Insert(pro);

- Add pictures/images as files:

string iufn = _fileProvider.GetFileName(iurl);
if (!pro.ProductPictures.Any(a => a.Picture.SeoFilename.ToLowerInvariant() == iufn.ToLowerInvariant()))
{
      Core.Domain.Media.Picture propic;
      _pictureService.StoreInDb = false;
      propic = _pictureService.InsertPicture(data.Item2, "image/jpeg", iufn, pro.Name, pro.Name, true);

      _productService.InsertProductPicture(new Core.Domain.Catalog.ProductPicture()
      {
            Product = pro,
            Picture = propic
      });
}

- Adding Product Attribute:

pattr = new Core.Domain.Catalog.ProductAttribute()
{
     Name = name

};

productAttributeService.InsertProductAttribute(pattr);

pro.ProductAttributeMappings.Add(new Core.Domain.Catalog.ProductAttributeMapping()
{
     AttributeControlType = Core.Domain.Catalog.AttributeControlType.ColorSquares,
     IsRequired = true,
     ProductAttribute = pattr,
     ProductAttributeValues =
     {
          new Core.Domain.Catalog.ProductAttributeValue
          {
                AttributeValueType = Core.Domain.Catalog.AttributeValueType.Simple,
                Name = val, 
                IsPreSelected = true, 
                ColorSquaresRgb=rgb
          }
     }

- Adding Product Specification Attribute:

spattr = new Core.Domain.Catalog.SpecificationAttribute
{
       Name = name
};
spAttrRepository.Insert(spattr);
spao = new Core.Domain.Catalog.SpecificationAttributeOption()
{
       Name = val,
       SpecificationAttribute = spattr
};
spOAttrRepository.Insert(spao);

pro.ProductSpecificationAttributes.Add(new Core.Domain.Catalog.ProductSpecificationAttribute()
{
       AllowFiltering = true,
       ShowOnProductPage = true,
       SpecificationAttributeOption = spao,
       AttributeType = Core.Domain.Catalog.SpecificationAttributeType.Option
});

- do not forget to update Entity (Product in this case) after all changes:

productRepository.Update(pro);

- Adding Urls/Slugs to entities:

if (string.IsNullOrEmpty(_urlRecordService.GetActiveSlug(p.Id, typeof(Core.Domain.Catalog.Product).Name, 0)))
{
     slug = _urlRecordService.ValidateSeName(_productService.GetProductById(p.Id), p.Name, p.Name, true);
     _urlRecordService.InsertUrlRecord(new Core.Domain.Seo.UrlRecord
     {
           EntityId = p.Id,
           EntityName = typeof(Core.Domain.Catalog.Product).Name,
           LanguageId = 0,
           IsActive = true,
           Slug = slug
     });
}


Next post about adding message tokens...





1vqHSTrq1GEoEF7QsL8dhmJfRMDVxhv2y



C#, Json and PHP Form

Today, I had a task to submit data to an PHP API that takes Json data via HTTP POST using ContentType "application/x-www-form-urlencoded" and format like:

order[phone]=+48733552233&order[name]=First Name Last Name&order[deliveryCost]=50&order[deliveryStockCode]=39931b80-e1c2-11e3-8c4a-0050568002cf&order[comment]=test api&orderItems[0][itemID]=194559-0&orderItems[0][salePrice]=9500&orderItems[0][count]=2&orderItems[1][itemID]=071402-0&orderItems[1][salePrice]=750&orderItems[1][count]=5&key=777777777777777

Hah, crazy right? I have never seen such crazy things before and I had no chance except just take it and create myself.

Google did help me a little bit but in my case there were arrays of objects so I had to modify and extend a code I did find on internet.

So, to transform this:


                    var json = new
                    {
                        key = apiKey,
                        order = new
                        {
                            phone = PhoneNumber,
                            name = LastName + " " + FirstName,
                            comment = Comment,
                            deliveryCost = Math.Round(OrderItems.Sum(s => s.AdditionalShippingCharge), 0, MidpointRounding.ToEven).ToString(),
                            deliveryStockCode = Address
                        },
                        orderItems = OrderItems.Select(s => new
                        {
                            itemID = s.ProdId,
                            salePrice = Math.Round(s.Price, 0, MidpointRounding.ToEven),
                            count = s.Quantity
                        }).ToArray()
                    };

into the form data like I shown before, here is my method (it is rough so I'm sure it is not perfect but I had no time to create it cool and clean and may be will re-write it in the future).


Also, in my case , there were anonymous types so it had to identify them somehow, but as you may know already, anonymous types in C# has no explicit and compilation time type declaration to get its typeof().


        public string JsonToHttpFormString(object request, string separator = ",")
        {
            if (request == null)
                throw new ArgumentNullException("request");

            // Get all properties on the object
            var properties = request.GetType().GetProperties()
                .Where(x => x.CanRead)
                .Where(x => x.GetValue(request, null) != null)
                .ToDictionary(x => x.Name, x => x.GetValue(request, null));

            // Get names for all IEnumerable properties (excl. string)
            var propertyNames = properties
                .Where(x => !(x.Value is string) && ((x.Value is IEnumerable) || (x.Value != null && x.Value.GetType().IsConstructedGenericType && x.Value.GetType().Name.Contains("AnonymousType"))))
                .Select(x => x.Key)
                .ToList();

            // Concat all IEnumerable properties into a comma separated string
            bool isAnonym = false;
            foreach (var key in propertyNames)
            {
                var valueType = properties[key].GetType();
                var valueElemType = valueType.IsGenericType
                                        ? valueType.GetGenericArguments()[0]
                                        : valueType.GetElementType();

                isAnonym = valueType.Name.Contains("AnonymousType");
                if (valueElemType.IsPrimitive || valueElemType == typeof(string) || isAnonym)
                {
                    var enumerable = properties[key] as IEnumerable;
                    if (isAnonym && !valueType.IsArray)
                    {
                        List<string> tempvs = new List<string>();
                        var item = properties[key];
                        // Get all properties on the object
                        var properties2 = item.GetType().GetProperties()
                            .Where(x => x.CanRead)
                            .Where(x => x.GetValue(item, null) != null)
                            .ToDictionary(x => x.Name, x => x.GetValue(item, null));

                        foreach (var kkey in properties2)
                        {
                            var valueType2 = kkey.GetType();
                            var valueElemType2 = valueType2.IsGenericType
                                                    ? valueType2.GetGenericArguments()[0]
                                                    : valueType2.GetElementType();

                            if (valueElemType2.IsPrimitive || valueElemType2 == typeof(string))
                            {
                                tempvs.Add(HttpUtility.UrlEncode(key + "[" + kkey.Key + "]") + "=" + HttpUtility.UrlEncode(kkey.Value.ToString()));
                            }
                        }

                        properties.Remove(key);
                        properties.Add(string.Join("&", tempvs), string.Empty);
                        tempvs.Clear();
                        tempvs = null;
                    }
                    else if (isAnonym && valueType.IsArray)
                    {
                        int i = 0;
                        List<string> tempvs = new List<string>();
                        foreach (var item in enumerable)
                        {
                            // Get all properties on the object
                            var properties2 = item.GetType().GetProperties()
                                .Where(x => x.CanRead)
                                .Where(x => x.GetValue(item, null) != null)
                                .ToDictionary(x => x.Name, x => x.GetValue(item, null));

                            foreach (var kkey in properties2)
                            {
                                var valueType2 = kkey.GetType();
                                var valueElemType2 = valueType2.IsGenericType
                                                        ? valueType2.GetGenericArguments()[0]
                                                        : valueType2.GetElementType();

                                if (valueElemType2.IsPrimitive || valueElemType2 == typeof(string))
                                {
                                    tempvs.Add(HttpUtility.UrlEncode(key + "[" + i + "][" + kkey.Key + "]") + "=" + HttpUtility.UrlEncode(kkey.Value.ToString()));
                                }
                            }
                            i++;
                        }
                        properties.Remove(key);
                        properties.Add(string.Join("&", tempvs), string.Empty);
                        tempvs.Clear();
                        tempvs = null;
                    }
                    else
                    {
                        properties[key] = string.Join(separator, enumerable.Cast<object>());
                    }
                }
            }

            // Concat all key/value pairs into a string separated by ampersand and remove trailing '='
            string res = string.Join("&", properties
                .Select(x => string.Concat(
                    string.IsNullOrEmpty(x.Value.ToString()) ? x.Key : HttpUtility.UrlEncode(x.Key), "=",
                    HttpUtility.UrlEncode(x.Value.ToString())).TrimEnd('='))).TrimEnd('=');

            return res;
        }


Also, it has to do URL encoding to get properly formatted data for form submission style and os we finally could get something like that:

order%5Bphone%5D=%2B+44+%28733%29+55-22-33& order%5Bname%5D=%D0%98%D0%BC%D1%8F+%D0%BF%D0%BE%D0%BB%D1%83% D1%87%D0%B0%D1%82%D0%B5%D0%BB%D1%8F& order%5BdeliveryCost%5D=50&order%5BdeliveryStockCode%5D=39931b80-e1c2-11e3-8c4a-0050568002cf& order%5Bcomment%5D=test+api&orderItems%5B0%5D%5BitemID%5D=194559-0&orderItems%5B0%5D%5BsalePrice%5D=9500&orderItems%5B0%5D%5Bcount%5D=2& orderItems%5B1%5D%5BitemID%5D=071402-0&orderItems%5B1%5D%5BsalePrice%5D=750&orderItems%5B1%5D%5Bcount%5D=5&key=777777777777777


Thank you for reading and see you :)




1vqHSTrq1GEoEF7QsL8dhmJfRMDVxhv2y



AWS, WordPress and File Uploading issue

Some time ago I had a need to modify some small things here and there under WordPress that was deployed under AWS EC2 instance.

As I had never worked with PHP, AWS and WordPress it was just simple step-by-step steps that I did take to get its done.

At some point when i started uploading images I faced with the error like:

The uploaded file could not be moved to wp-content/uploads/2019/02

Sure, it was not hard to understand that something wrong with permissions, access, folders, files, etc. But where and why? Do not forget I had never worked with all of that before.

I did try everything I knew on that time about it but with no luck.

I did google and try to gather everything by pieces, I also did contact AWS support and they did help me a lot as well.

This was due to a permissions/ownership problem. 

Apache httpd serves files that are kept in a directory called the Apache document root. The Amazon Linux Apache document root was /var/www/html, which by default was owned by root.

Below is an example on how to modify the ownership and permissions of this directory. In this example my user was ec2-user but it may be different in your environment. You would replace ec2-user with whatever your user is.

To allow the ec2-user account to manipulate files in this directory, you must modify the ownership and permissions of the directory. There are many ways to accomplish this task. Here we will add ec2-user to the apache group, to give the apache group ownership of the /var/www directory and assign write permissions to the group.


So finally here are steps that did solve my problem with file uploading:



1. Add your user (in this case, ec2-user) to the apache group. [ec2-user ~]$ sudo usermod -a -G apache ec2-user 
 2.Log out and then log back in again to pick up the new group, and then verify your membership. 
                    a. Log out (use the exit command or close the terminal window): [ec2-user ~]$ exit 
                    b. To verify your membership in the apache group, reconnect to your instance, and then run the following command: [ec2-user ~]$ groups ec2-user adm wheel apache systemd-journal 
 3. Change the group ownership of /var/www and its contents to the apache group. [ec2-user ~]$ sudo chown -R ec2-user:apache /var/www 
 4. To add group write permissions and to set the group ID on future subdirectories, change the directory permissions of /var/www and its subdirectories. [ec2-user ~]$ sudo chmod 2775 /var/www && find /var/www -type d -exec sudo chmod 2775 {} \; 
 5. To add group write permissions, recursively change the file permissions of /var/www and its subdirectories: [ec2-user ~]$ find /var/www -type f -exec sudo chmod 0664 {} \;


Now, ec2-user (and any future members of the apache group) can add, delete, and edit files in the Apache document root, enabling you to add content, such as a static website or a PHP application.


I have to mention that this did not solve my problem with plugin updating

An error occurred while updating Akismet Anti-Spam: Could not create directory


but this is different story and hopefully we will see solution as well :)


Thank you


1vqHSTrq1GEoEF7QsL8dhmJfRMDVxhv2y